-
Notifications
You must be signed in to change notification settings - Fork 3
/
domoticz_utils.py
165 lines (135 loc) · 5.27 KB
/
domoticz_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# == domoticz_utils.py Author: Zuinige Rijder =========
""" domoticz utils """
# pylint:disable=logging-fstring-interpolation, consider-using-enumerate
import configparser
import logging
import logging.config
from monitor_utils import (
dbg,
execute_request,
get,
d,
get_bool,
get_filepath,
get_items_dailystat_trip,
get_items_dailystats_day,
get_items_monitor_csv,
get_items_monitor_dailystats_csv,
get_items_monitor_tripinfo_csv,
get_items_summary,
get_summary_headers,
)
PARSER = configparser.ConfigParser()
PARSER.read(get_filepath("monitor.cfg"))
# == read [Domoticz] settings in monitor.cfg ===========================
domoticz_settings = dict(PARSER.items("Domoticz"))
SEND_TO_DOMOTICZ = get_bool(domoticz_settings, "send_to_domoticz", False)
DOMOTICZ_URL = get(domoticz_settings, "domot_url")
if SEND_TO_DOMOTICZ:
ITEMS_MONITOR_CSV = get_items_monitor_csv()
for IDX in range(len(ITEMS_MONITOR_CSV)):
ITEMS_MONITOR_CSV[IDX] = f"monitor_monitor_{ITEMS_MONITOR_CSV[IDX]}"
ITEMS_TRIPINFO_CSV = get_items_monitor_tripinfo_csv()
for IDX in range(len(ITEMS_TRIPINFO_CSV)):
ITEMS_TRIPINFO_CSV[IDX] = f"monitor_tripinfo_{ITEMS_TRIPINFO_CSV[IDX]}"
ITEMS_DAILYSTATS_CSV = get_items_monitor_dailystats_csv()
for IDX in range(len(ITEMS_DAILYSTATS_CSV)):
ITEMS_DAILYSTATS_CSV[IDX] = f"monitor_dailystats_{ITEMS_DAILYSTATS_CSV[IDX]}"
ITEMS_SUMMARY = get_items_summary()
ITEMS_DAILYSTATS_DAY = get_items_dailystats_day()
ITEMS_DAILYSTATS_TRIP = get_items_dailystat_trip()
# == send to Domoticz ========================================================
def send_to_domoticz(header: str, value: str) -> None:
"""send_to_Domoticz"""
reference_test = DOMOTICZ_URL == "domoticz_reference_test"
idx = get(domoticz_settings, header.lower(), "0")
_ = d() and dbg(f"send_to_domoticz: idx = {idx}, {header} = {value}")
if idx == "0":
return # nothing to do
url = (
DOMOTICZ_URL
+ "/json.htm?type=command¶m=udevice&idx="
+ idx
+ "&svalue="
+ value
)
_ = d() and dbg(f"send_to_domoticz: {url}")
retry = 0
while not reference_test:
retry += 1
content = execute_request(url, "", {})
if content != "ERROR" or retry > 30:
if content == "ERROR":
logging.error(f"number of retries exceeded: {url}")
return
def send_splitted_line(
headers: list, splitted: list, replace_empty_by_0: bool, skip_first: bool
) -> None:
"""send_splitted_line"""
if len(splitted) < len(headers):
logging.warning(
f"line does not have all elements: {splitted}\nHEADERS={headers}"
)
return
skipped_first = not skip_first
for i in range(len(splitted)): # pylint:disable=consider-using-enumerate
if i < len(headers):
if skipped_first:
value = splitted[i].strip()
if replace_empty_by_0 and value == "":
value = "0"
send_to_domoticz(headers[i], value)
else:
skipped_first = True
def send_line(
headers: list, line: str, replace_empty_by_0: bool = True, skip_first: bool = False
) -> None:
"""send_line"""
splitted = line.split(",")
send_splitted_line(headers, splitted, replace_empty_by_0, skip_first)
def send_monitor_csv_line_to_domoticz(line: str) -> None:
"""send_monitor_csv_line_to_domoticz"""
if SEND_TO_DOMOTICZ:
send_line(ITEMS_MONITOR_CSV, line)
def send_tripinfo_csv_line_to_domoticz(line: str) -> None:
"""send_tripinfo_csv_line_to_domoticz"""
if SEND_TO_DOMOTICZ:
send_line(ITEMS_TRIPINFO_CSV, line)
def send_dailystats_csv_line_to_domoticz(line: str) -> None:
"""send_dailystats_csv_line_to_domoticz"""
if SEND_TO_DOMOTICZ:
send_line(ITEMS_DAILYSTATS_CSV, line)
def get_items(subtopic: str, items: list[str]) -> list[str]:
"""get_items"""
new_items: list[str] = []
for item in items:
new_items.append(f"{subtopic}_{item}")
return new_items
def send_summary_line_to_domoticz(line: str) -> None:
"""send_summary_line_to_domoticz"""
if SEND_TO_DOMOTICZ:
splitted = [x.strip() for x in line.split(",")]
period = splitted[0].replace(" ", "")
summary_headers_dict = get_summary_headers()
if period in summary_headers_dict:
key = summary_headers_dict[period]
send_splitted_line(
get_items(f"summary_{key}", ITEMS_SUMMARY), splitted, True, True
)
else:
_ = d() and dbg(
f"Skipping: period={period}, headers={summary_headers_dict}"
)
def send_dailystats_day_line_to_domoticz(postfix: str, line: str) -> None:
"""send_dailystats_day_line_to_domoticz"""
if SEND_TO_DOMOTICZ:
send_line(get_items(f"dailystats_day_{postfix}", ITEMS_DAILYSTATS_DAY), line)
def send_dailystats_trip_line_to_domoticz(
postfix: str, line: str, skip_first_two: bool = False
) -> None:
"""send_dailystats_trip_line_to_domoticz"""
if SEND_TO_DOMOTICZ:
items = get_items(f"dailystats_trip_{postfix}", ITEMS_DAILYSTATS_TRIP)
if skip_first_two:
items = items[2:]
send_line(items, line, replace_empty_by_0=False)