Skip to content

Commit df39d4c

Browse files
committed
Breakout _collect_power_values() and related
1 parent 4c64ca3 commit df39d4c

File tree

3 files changed

+76
-110
lines changed

3 files changed

+76
-110
lines changed

plugwise/common.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
check_heater_central,
2020
check_model,
2121
get_vendor_name,
22+
power_data_local_format,
2223
return_valid,
2324
)
2425

@@ -107,6 +108,77 @@ def _appl_thermostat_info(self, appl: Munch, xml_1: etree, xml_2: etree = None)
107108

108109
return appl
109110

111+
def _collect_power_values(self, data: DeviceData, loc: Munch, tariff: str, legacy: bool = False) -> None:
112+
"""Something."""
113+
for loc.peak_select in ("nl_peak", "nl_offpeak"):
114+
loc.locator = (
115+
f'./{loc.log_type}[type="{loc.measurement}"]/period/'
116+
f'measurement[@{tariff}="{loc.peak_select}"]'
117+
)
118+
if legacy:
119+
loc.locator = (
120+
f"./{loc.meas_list[0]}_{loc.log_type}/measurement"
121+
f'[@directionality="{loc.meas_list[1]}"][@{tariff}="{loc.peak_select}"]'
122+
)
123+
124+
loc = self._power_data_peak_value(loc, legacy)
125+
if not loc.found:
126+
continue
127+
128+
data = self._power_data_energy_diff(
129+
loc.measurement, loc.net_string, loc.f_val, data
130+
)
131+
key = cast(SensorType, loc.key_string)
132+
data["sensors"][key] = loc.f_val
133+
134+
def _power_data_peak_value(self, loc: Munch, legacy: bool) -> Munch:
135+
"""Helper-function for _power_data_from_location() and _power_data_from_modules()."""
136+
loc.found = True
137+
combination = "log" in loc.log_type and ("gas" in loc.measurement or "phase" in loc.measurement)
138+
if legacy:
139+
combination = "meter" in loc.log_type and ("point" in loc.log_type or "gas" in loc.measurement)
140+
141+
if loc.logs.find(loc.locator) is None:
142+
# If locator not found look for P1 gas_consumed or phase data (without tariff)
143+
# For legacy look for P1 legacy electricity_point_meter or gas_*_meter data
144+
if combination:
145+
# Avoid double processing by skipping one peak-list option
146+
if loc.peak_select == "nl_offpeak":
147+
loc.found = False
148+
return loc
149+
150+
loc.locator = (
151+
f'./{loc.log_type}[type="{loc.measurement}"]/period/measurement'
152+
)
153+
if legacy:
154+
loc.locator = (
155+
f"./{loc.meas_list[0]}_{loc.log_type}/"
156+
f'measurement[@directionality="{loc.meas_list[1]}"]'
157+
)
158+
159+
if loc.logs.find(loc.locator) is None:
160+
loc.found = False
161+
return loc
162+
else:
163+
loc.found = False
164+
return loc
165+
166+
if (peak := loc.peak_select.split("_")[1]) == "offpeak":
167+
peak = "off_peak"
168+
log_found = loc.log_type.split("_")[0]
169+
loc.key_string = f"{loc.measurement}_{peak}_{log_found}"
170+
if "gas" in loc.measurement or loc.log_type == "point_meter":
171+
loc.key_string = f"{loc.measurement}_{log_found}"
172+
# Only for P1 Actual -------------------#
173+
if "phase" in loc.measurement:
174+
loc.key_string = f"{loc.measurement}"
175+
# --------------------------------------#
176+
loc.net_string = f"net_electricity_{log_found}"
177+
val = loc.logs.find(loc.locator).text
178+
loc.f_val = power_data_local_format(loc.attrs, loc.key_string, val)
179+
180+
return loc
181+
110182
def _device_data_switching_group(
111183
self, device: DeviceData, data: DeviceData
112184
) -> None:

plugwise/helper.py

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@
5555
InvalidXMLError,
5656
ResponseError,
5757
)
58-
from plugwise.util import (
59-
check_model,
60-
escape_illegal_xml_characters,
61-
format_measure,
62-
power_data_local_format,
63-
)
58+
from plugwise.util import check_model, escape_illegal_xml_characters, format_measure
6459

6560
# This way of importing aiohttp is because of patch/mocking in testing (aiohttp timeouts)
6661
from aiohttp import BasicAuth, ClientError, ClientResponse, ClientSession, ClientTimeout
@@ -558,68 +553,17 @@ def _power_data_from_location(self, loc_id: str) -> DeviceData:
558553
direct_data: DeviceData = {"sensors": {}}
559554
loc = Munch()
560555
log_list: list[str] = ["point_log", "cumulative_log", "interval_log"]
561-
peak_list: list[str] = ["nl_peak", "nl_offpeak"]
562556
t_string = "tariff"
563557

564558
search = self._domain_objects
565559
loc.logs = search.find(f'./location[@id="{loc_id}"]/logs')
566560
for loc.measurement, loc.attrs in P1_MEASUREMENTS.items():
567561
for loc.log_type in log_list:
568-
for loc.peak_select in peak_list:
569-
loc.locator = (
570-
f'./{loc.log_type}[type="{loc.measurement}"]/period/'
571-
f'measurement[@{t_string}="{loc.peak_select}"]'
572-
)
573-
loc = self._power_data_peak_value(loc)
574-
if not loc.found:
575-
continue
576-
577-
direct_data = self._power_data_energy_diff(
578-
loc.measurement, loc.net_string, loc.f_val, direct_data
579-
)
580-
key = cast(SensorType, loc.key_string)
581-
direct_data["sensors"][key] = loc.f_val
562+
self._collect_power_values(direct_data, loc, t_string)
582563

583564
self._count += len(direct_data["sensors"])
584565
return direct_data
585566

586-
def _power_data_peak_value(self, loc: Munch) -> Munch:
587-
"""Helper-function for _power_data_from_location() and _power_data_from_modules()."""
588-
loc.found = True
589-
# If locator not found look for P1 gas_consumed or phase data (without tariff)
590-
if loc.logs.find(loc.locator) is None:
591-
if "log" in loc.log_type and (
592-
"gas" in loc.measurement or "phase" in loc.measurement
593-
):
594-
# Avoid double processing by skipping one peak-list option
595-
if loc.peak_select == "nl_offpeak":
596-
loc.found = False
597-
return loc
598-
599-
loc.locator = (
600-
f'./{loc.log_type}[type="{loc.measurement}"]/period/measurement'
601-
)
602-
if loc.logs.find(loc.locator) is None:
603-
loc.found = False
604-
return loc
605-
else:
606-
loc.found = False # pragma: no cover
607-
return loc # pragma: no cover
608-
609-
if (peak := loc.peak_select.split("_")[1]) == "offpeak":
610-
peak = "off_peak"
611-
log_found = loc.log_type.split("_")[0]
612-
loc.key_string = f"{loc.measurement}_{peak}_{log_found}"
613-
if "gas" in loc.measurement or loc.log_type == "point_meter":
614-
loc.key_string = f"{loc.measurement}_{log_found}"
615-
if "phase" in loc.measurement:
616-
loc.key_string = f"{loc.measurement}"
617-
loc.net_string = f"net_electricity_{log_found}"
618-
val = loc.logs.find(loc.locator).text
619-
loc.f_val = power_data_local_format(loc.attrs, loc.key_string, val)
620-
621-
return loc
622-
623567
def _appliance_measurements(
624568
self,
625569
appliance: etree,

plugwise/legacy/helper.py

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
SwitchType,
4444
ThermoLoc,
4545
)
46-
from plugwise.util import format_measure, power_data_local_format, version_to_model
46+
from plugwise.util import format_measure, version_to_model
4747

4848
# This way of importing aiohttp is because of patch/mocking in testing (aiohttp timeouts)
4949
from defusedxml import ElementTree as etree
@@ -310,7 +310,6 @@ def _power_data_from_modules(self) -> DeviceData:
310310
direct_data: DeviceData = {"sensors": {}}
311311
loc = Munch()
312312
mod_list: list[str] = ["interval_meter", "cumulative_meter", "point_meter"]
313-
peak_list: list[str] = ["nl_peak", "nl_offpeak"]
314313
t_string = "tariff_indicator"
315314

316315
search = self._modules
@@ -319,60 +318,11 @@ def _power_data_from_modules(self) -> DeviceData:
319318
loc.meas_list = loc.measurement.split("_")
320319
for loc.logs in mod_logs:
321320
for loc.log_type in mod_list:
322-
for loc.peak_select in peak_list:
323-
loc.locator = (
324-
f"./{loc.meas_list[0]}_{loc.log_type}/measurement"
325-
f'[@directionality="{loc.meas_list[1]}"][@{t_string}="{loc.peak_select}"]'
326-
)
327-
loc = self._power_data_peak_value(loc)
328-
if not loc.found:
329-
continue
330-
331-
direct_data = self._power_data_energy_diff(
332-
loc.measurement, loc.net_string, loc.f_val, direct_data
333-
)
334-
key = cast(SensorType, loc.key_string)
335-
direct_data["sensors"][key] = loc.f_val
321+
self._collect_power_values(direct_data, loc, t_string, legacy=True)
336322

337323
self._count += len(direct_data["sensors"])
338324
return direct_data
339325

340-
def _power_data_peak_value(self, loc: Munch) -> Munch:
341-
"""Helper-function for _power_data_from_location() and _power_data_from_modules()."""
342-
loc.found = True
343-
# If locator not found for P1 legacy electricity_point_meter or gas_*_meter data
344-
if loc.logs.find(loc.locator) is None:
345-
if "meter" in loc.log_type and (
346-
"point" in loc.log_type or "gas" in loc.measurement
347-
):
348-
# Avoid double processing by skipping one peak-list option
349-
if loc.peak_select == "nl_offpeak":
350-
loc.found = False
351-
return loc
352-
353-
loc.locator = (
354-
f"./{loc.meas_list[0]}_{loc.log_type}/"
355-
f'measurement[@directionality="{loc.meas_list[1]}"]'
356-
)
357-
if loc.logs.find(loc.locator) is None:
358-
loc.found = False
359-
return loc
360-
else:
361-
loc.found = False
362-
return loc
363-
364-
if (peak := loc.peak_select.split("_")[1]) == "offpeak":
365-
peak = "off_peak"
366-
log_found = loc.log_type.split("_")[0]
367-
loc.key_string = f"{loc.measurement}_{peak}_{log_found}"
368-
if "gas" in loc.measurement or loc.log_type == "point_meter":
369-
loc.key_string = f"{loc.measurement}_{log_found}"
370-
loc.net_string = f"net_electricity_{log_found}"
371-
val = loc.logs.find(loc.locator).text
372-
loc.f_val = power_data_local_format(loc.attrs, loc.key_string, val)
373-
374-
return loc
375-
376326
def _appliance_measurements(
377327
self,
378328
appliance: etree,

0 commit comments

Comments
 (0)