Skip to content

Commit 3d1e921

Browse files
committed
Move the power-related functions to util
1 parent 8f8aed2 commit 3d1e921

File tree

4 files changed

+84
-85
lines changed

4 files changed

+84
-85
lines changed

plugwise/common.py

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,18 @@
1515
ApplianceType,
1616
GwEntityData,
1717
ModuleData,
18-
SensorType,
1918
)
2019
from plugwise.util import (
21-
check_alternative_location,
2220
check_heater_central,
2321
check_model,
2422
get_vendor_name,
25-
power_data_local_format,
2623
return_valid,
2724
)
2825

2926
from defusedxml import ElementTree as etree
3027
from munch import Munch
3128

3229

33-
def collect_power_values(
34-
data: GwEntityData, loc: Munch, tariff: str, legacy: bool = False
35-
) -> None:
36-
"""Something."""
37-
for loc.peak_select in ("nl_peak", "nl_offpeak"):
38-
loc.locator = (
39-
f'./{loc.log_type}[type="{loc.measurement}"]/period/'
40-
f'measurement[@{tariff}="{loc.peak_select}"]'
41-
)
42-
if legacy:
43-
loc.locator = (
44-
f"./{loc.meas_list[0]}_{loc.log_type}/measurement"
45-
f'[@directionality="{loc.meas_list[1]}"][@{tariff}="{loc.peak_select}"]'
46-
)
47-
48-
loc = power_data_peak_value(loc, legacy)
49-
if not loc.found:
50-
continue
51-
52-
data = power_data_energy_diff(loc.measurement, loc.net_string, loc.f_val, data)
53-
key = cast(SensorType, loc.key_string)
54-
data["sensors"][key] = loc.f_val
55-
56-
5730
def get_zigbee_data(module: etree, module_data: ModuleData, legacy: bool) -> None:
5831
"""Helper-function for _get_module_data()."""
5932
if legacy:
@@ -69,62 +42,6 @@ def get_zigbee_data(module: etree, module_data: ModuleData, legacy: bool) -> Non
6942
module_data["reachable"] = zb_node.find("reachable").text == "true"
7043

7144

72-
def power_data_peak_value(loc: Munch, legacy: bool) -> Munch:
73-
"""Helper-function for _power_data_from_location() and _power_data_from_modules()."""
74-
loc.found = True
75-
if loc.logs.find(loc.locator) is None:
76-
loc = check_alternative_location(loc, legacy)
77-
if not loc.found:
78-
return loc
79-
80-
if (peak := loc.peak_select.split("_")[1]) == "offpeak":
81-
peak = "off_peak"
82-
log_found = loc.log_type.split("_")[0]
83-
loc.key_string = f"{loc.measurement}_{peak}_{log_found}"
84-
if "gas" in loc.measurement or loc.log_type == "point_meter":
85-
loc.key_string = f"{loc.measurement}_{log_found}"
86-
# Only for P1 Actual -------------------#
87-
if "phase" in loc.measurement:
88-
loc.key_string = f"{loc.measurement}"
89-
# --------------------------------------#
90-
loc.net_string = f"net_electricity_{log_found}"
91-
val = loc.logs.find(loc.locator).text
92-
loc.f_val = power_data_local_format(loc.attrs, loc.key_string, val)
93-
94-
return loc
95-
96-
97-
def power_data_energy_diff(
98-
measurement: str,
99-
net_string: SensorType,
100-
f_val: float | int,
101-
data: GwEntityData,
102-
) -> GwEntityData:
103-
"""Calculate differential energy."""
104-
if (
105-
"electricity" in measurement
106-
and "phase" not in measurement
107-
and "interval" not in net_string
108-
):
109-
diff = 1
110-
if "produced" in measurement:
111-
diff = -1
112-
if net_string not in data["sensors"]:
113-
tmp_val: float | int = 0
114-
else:
115-
tmp_val = data["sensors"][net_string]
116-
117-
if isinstance(f_val, int):
118-
tmp_val += f_val * diff
119-
else:
120-
tmp_val += float(f_val * diff)
121-
tmp_val = float(f"{round(tmp_val, 3):.3f}")
122-
123-
data["sensors"][net_string] = tmp_val
124-
125-
return data
126-
127-
12845
class SmileCommon:
12946
"""The SmileCommon class."""
13047

plugwise/helper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import datetime as dt
99
from typing import cast
1010

11-
from plugwise.common import SmileCommon, collect_power_values
11+
from plugwise.common import SmileCommon
1212
from plugwise.constants import (
1313
ACTIVE_ACTUATORS,
1414
ACTUATOR_CLASSES,
@@ -44,6 +44,7 @@
4444
)
4545
from plugwise.util import (
4646
check_model,
47+
collect_power_values,
4748
common_match_cases,
4849
count_data_items,
4950
format_measure,

plugwise/legacy/helper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from typing import cast
99

10-
from plugwise.common import SmileCommon, collect_power_values
10+
from plugwise.common import SmileCommon
1111
from plugwise.constants import (
1212
ACTIVE_ACTUATORS,
1313
ACTUATOR_CLASSES,
@@ -35,6 +35,7 @@
3535
ThermoLoc,
3636
)
3737
from plugwise.util import (
38+
collect_power_values,
3839
common_match_cases,
3940
count_data_items,
4041
format_measure,

plugwise/util.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@ def check_model(name: str | None, vendor_name: str | None) -> str | None:
116116
return None
117117

118118

119+
def collect_power_values(
120+
data: GwEntityData, loc: Munch, tariff: str, legacy: bool = False
121+
) -> None:
122+
"""Something."""
123+
for loc.peak_select in ("nl_peak", "nl_offpeak"):
124+
loc.locator = (
125+
f'./{loc.log_type}[type="{loc.measurement}"]/period/'
126+
f'measurement[@{tariff}="{loc.peak_select}"]'
127+
)
128+
if legacy:
129+
loc.locator = (
130+
f"./{loc.meas_list[0]}_{loc.log_type}/measurement"
131+
f'[@directionality="{loc.meas_list[1]}"][@{tariff}="{loc.peak_select}"]'
132+
)
133+
134+
loc = power_data_peak_value(loc, legacy)
135+
if not loc.found:
136+
continue
137+
138+
data = power_data_energy_diff(loc.measurement, loc.net_string, loc.f_val, data)
139+
key = cast(SensorType, loc.key_string)
140+
data["sensors"][key] = loc.f_val
141+
142+
119143
def common_match_cases(
120144
measurement: str,
121145
attrs: DATA | UOM,
@@ -199,6 +223,37 @@ def get_vendor_name(module: etree, model_data: ModuleData) -> ModuleData:
199223
return model_data
200224

201225

226+
def power_data_energy_diff(
227+
measurement: str,
228+
net_string: SensorType,
229+
f_val: float | int,
230+
data: GwEntityData,
231+
) -> GwEntityData:
232+
"""Calculate differential energy."""
233+
if (
234+
"electricity" in measurement
235+
and "phase" not in measurement
236+
and "interval" not in net_string
237+
):
238+
diff = 1
239+
if "produced" in measurement:
240+
diff = -1
241+
if net_string not in data["sensors"]:
242+
tmp_val: float | int = 0
243+
else:
244+
tmp_val = data["sensors"][net_string]
245+
246+
if isinstance(f_val, int):
247+
tmp_val += f_val * diff
248+
else:
249+
tmp_val += float(f_val * diff)
250+
tmp_val = float(f"{round(tmp_val, 3):.3f}")
251+
252+
data["sensors"][net_string] = tmp_val
253+
254+
return data
255+
256+
202257
def power_data_local_format(
203258
attrs: dict[str, str], key_string: str, val: str
204259
) -> float | int:
@@ -212,6 +267,31 @@ def power_data_local_format(
212267
return format_measure(val, attrs_uom)
213268

214269

270+
def power_data_peak_value(loc: Munch, legacy: bool) -> Munch:
271+
"""Helper-function for _power_data_from_location() and _power_data_from_modules()."""
272+
loc.found = True
273+
if loc.logs.find(loc.locator) is None:
274+
loc = check_alternative_location(loc, legacy)
275+
if not loc.found:
276+
return loc
277+
278+
if (peak := loc.peak_select.split("_")[1]) == "offpeak":
279+
peak = "off_peak"
280+
log_found = loc.log_type.split("_")[0]
281+
loc.key_string = f"{loc.measurement}_{peak}_{log_found}"
282+
if "gas" in loc.measurement or loc.log_type == "point_meter":
283+
loc.key_string = f"{loc.measurement}_{log_found}"
284+
# Only for P1 Actual -------------------#
285+
if "phase" in loc.measurement:
286+
loc.key_string = f"{loc.measurement}"
287+
# --------------------------------------#
288+
loc.net_string = f"net_electricity_{log_found}"
289+
val = loc.logs.find(loc.locator).text
290+
loc.f_val = power_data_local_format(loc.attrs, loc.key_string, val)
291+
292+
return loc
293+
294+
215295
def remove_empty_platform_dicts(data: GwEntityData) -> None:
216296
"""Helper-function for removing any empty platform dicts."""
217297
if not data["binary_sensors"]:

0 commit comments

Comments
 (0)