Skip to content

Commit b60b979

Browse files
committed
Improve
1 parent 53a2688 commit b60b979

File tree

2 files changed

+81
-73
lines changed

2 files changed

+81
-73
lines changed

plugwise/common.py

Lines changed: 53 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
SensorType,
1717
)
1818
from plugwise.util import (
19-
check_alternative_locations,
19+
check_alternative_location,
2020
check_heater_central,
2121
check_model,
2222
get_vendor_name,
@@ -132,31 +132,12 @@ def _collect_power_values(self, data: DeviceData, loc: Munch, tariff: str, legac
132132
key = cast(SensorType, loc.key_string)
133133
data["sensors"][key] = loc.f_val
134134

135-
136135
def _power_data_peak_value(self, loc: Munch, legacy: bool) -> Munch:
137136
"""Helper-function for _power_data_from_location() and _power_data_from_modules()."""
138137
loc.found = True
139138
if loc.logs.find(loc.locator) is None:
140-
if check_alternative_locations(loc, legacy):
141-
# Avoid double processing by skipping one peak-list option
142-
if loc.peak_select == "nl_offpeak":
143-
loc.found = False
144-
return loc
145-
146-
loc.locator = (
147-
f'./{loc.log_type}[type="{loc.measurement}"]/period/measurement'
148-
)
149-
if legacy:
150-
loc.locator = (
151-
f"./{loc.meas_list[0]}_{loc.log_type}/"
152-
f'measurement[@directionality="{loc.meas_list[1]}"]'
153-
)
154-
155-
if loc.logs.find(loc.locator) is None:
156-
loc.found = False
157-
return loc
158-
else:
159-
loc.found = False
139+
loc = check_alternative_location(loc, legacy)
140+
if not loc.found:
160141
return loc
161142

162143
if (peak := loc.peak_select.split("_")[1]) == "offpeak":
@@ -175,6 +156,56 @@ def _power_data_peak_value(self, loc: Munch, legacy: bool) -> Munch:
175156

176157
return loc
177158

159+
def _power_data_energy_diff(
160+
self,
161+
measurement: str,
162+
net_string: SensorType,
163+
f_val: float | int,
164+
direct_data: DeviceData,
165+
) -> DeviceData:
166+
"""Calculate differential energy."""
167+
if (
168+
"electricity" in measurement
169+
and "phase" not in measurement
170+
and "interval" not in net_string
171+
):
172+
diff = 1
173+
if "produced" in measurement:
174+
diff = -1
175+
if net_string not in direct_data["sensors"]:
176+
tmp_val: float | int = 0
177+
else:
178+
tmp_val = direct_data["sensors"][net_string]
179+
180+
if isinstance(f_val, int):
181+
tmp_val += f_val * diff
182+
else:
183+
tmp_val += float(f_val * diff)
184+
tmp_val = float(f"{round(tmp_val, 3):.3f}")
185+
186+
direct_data["sensors"][net_string] = tmp_val
187+
188+
return direct_data
189+
190+
def _create_gw_devices(self, appl: Munch) -> None:
191+
"""Helper-function for creating/updating gw_devices."""
192+
self.gw_devices[appl.dev_id] = {"dev_class": appl.pwclass}
193+
self._count += 1
194+
for key, value in {
195+
"firmware": appl.firmware,
196+
"hardware": appl.hardware,
197+
"location": appl.location,
198+
"mac_address": appl.mac,
199+
"model": appl.model,
200+
"name": appl.name,
201+
"zigbee_mac_address": appl.zigbee_mac,
202+
"vendor": appl.vendor_name,
203+
}.items():
204+
if value is not None or key == "location":
205+
appl_key = cast(ApplianceType, key)
206+
self.gw_devices[appl.dev_id][appl_key] = value
207+
self._count += 1
208+
178209
def _device_data_switching_group(
179210
self, device: DeviceData, data: DeviceData
180211
) -> None:
@@ -296,53 +327,3 @@ def _get_zigbee_data(self, module: etree, model_data: ModelData, legacy: bool) -
296327
elif (zb_node := module.find("./protocols/zig_bee_node")) is not None:
297328
model_data["zigbee_mac_address"] = zb_node.find("mac_address").text
298329
model_data["reachable"] = zb_node.find("reachable").text == "true"
299-
300-
def _power_data_energy_diff(
301-
self,
302-
measurement: str,
303-
net_string: SensorType,
304-
f_val: float | int,
305-
direct_data: DeviceData,
306-
) -> DeviceData:
307-
"""Calculate differential energy."""
308-
if (
309-
"electricity" in measurement
310-
and "phase" not in measurement
311-
and "interval" not in net_string
312-
):
313-
diff = 1
314-
if "produced" in measurement:
315-
diff = -1
316-
if net_string not in direct_data["sensors"]:
317-
tmp_val: float | int = 0
318-
else:
319-
tmp_val = direct_data["sensors"][net_string]
320-
321-
if isinstance(f_val, int):
322-
tmp_val += f_val * diff
323-
else:
324-
tmp_val += float(f_val * diff)
325-
tmp_val = float(f"{round(tmp_val, 3):.3f}")
326-
327-
direct_data["sensors"][net_string] = tmp_val
328-
329-
return direct_data
330-
331-
def _create_gw_devices(self, appl: Munch) -> None:
332-
"""Helper-function for creating/updating gw_devices."""
333-
self.gw_devices[appl.dev_id] = {"dev_class": appl.pwclass}
334-
self._count += 1
335-
for key, value in {
336-
"firmware": appl.firmware,
337-
"hardware": appl.hardware,
338-
"location": appl.location,
339-
"mac_address": appl.mac,
340-
"model": appl.model,
341-
"name": appl.name,
342-
"zigbee_mac_address": appl.zigbee_mac,
343-
"vendor": appl.vendor_name,
344-
}.items():
345-
if value is not None or key == "location":
346-
appl_key = cast(ApplianceType, key)
347-
self.gw_devices[appl.dev_id][appl_key] = value
348-
self._count += 1

plugwise/util.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,34 @@
2222
from munch import Munch
2323

2424

25-
def check_alternative_locations(loc: Munch, legacy: bool) -> bool:
25+
def check_alternative_location(loc: Munch, legacy: bool) -> Munch:
26+
"""Try."""
27+
if in_alternative_location(loc, legacy):
28+
# Avoid double processing by skipping one peak-list option
29+
if loc.peak_select == "nl_offpeak":
30+
loc.found = False
31+
return loc
32+
33+
loc.locator = (
34+
f'./{loc.log_type}[type="{loc.measurement}"]/period/measurement'
35+
)
36+
if legacy:
37+
loc.locator = (
38+
f"./{loc.meas_list[0]}_{loc.log_type}/"
39+
f'measurement[@directionality="{loc.meas_list[1]}"]'
40+
)
41+
42+
if loc.logs.find(loc.locator) is None:
43+
loc.found = False
44+
return loc
45+
46+
return loc
47+
48+
loc.found = False
49+
return loc
50+
51+
52+
def in_alternative_location(loc: Munch, legacy: bool) -> bool:
2653
"""Look for P1 gas_consumed or phase data (without tariff).
2754
2855
For legacy look for P1 legacy electricity_point_meter or gas_*_meter data.

0 commit comments

Comments
 (0)