Skip to content

Commit 54d9245

Browse files
committed
Move check_heater_central() function to util.py
1 parent 45840e5 commit 54d9245

File tree

3 files changed

+40
-59
lines changed

3 files changed

+40
-59
lines changed

plugwise/helper.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
ResponseError,
6060
)
6161
from plugwise.util import (
62+
check_heater_central,
6263
check_model,
6364
escape_illegal_xml_characters,
6465
format_measure,
@@ -393,7 +394,7 @@ def _appliance_info_finder(self, appliance: etree, appl: Munch) -> Munch:
393394
return None # pragma: no cover
394395

395396
# Find the valid heater_central
396-
self._heater_id = self._check_heater_central()
397+
self._heater_id = check_heater_central(self._domain_objects)
397398

398399
# Info for On-Off device
399400
if self._on_off_device:
@@ -436,34 +437,6 @@ def _appliance_info_finder(self, appliance: etree, appl: Munch) -> Munch:
436437

437438
return appl
438439

439-
def _check_heater_central(self) -> str:
440-
"""Find the valid heater_central, helper-function for _appliance_info_finder().
441-
442-
Solution for Core Issue #104433,
443-
for a system that has two heater_central appliances.
444-
"""
445-
locator = "./appliance[type='heater_central']"
446-
hc_count = 0
447-
hc_list: list[dict[str, bool]] = []
448-
for heater_central in self._domain_objects.findall(locator):
449-
hc_count += 1
450-
hc_id: str = heater_central.attrib["id"]
451-
has_actuators: bool = (
452-
heater_central.find("actuator_functionalities/") is not None
453-
)
454-
hc_list.append({hc_id: has_actuators})
455-
456-
heater_central_id = list(hc_list[0].keys())[0]
457-
if hc_count > 1:
458-
for item in hc_list:
459-
for key, value in item.items():
460-
if value:
461-
heater_central_id = key
462-
# Stop when a valid id is found
463-
break
464-
465-
return heater_central_id
466-
467440
def _p1_smartmeter_info_finder(self, appl: Munch) -> None:
468441
"""Collect P1 DSMR Smartmeter info."""
469442
loc_id = next(iter(self.loc_data.keys()))

plugwise/legacy/helper.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@
4646
SwitchType,
4747
ThermoLoc,
4848
)
49-
from plugwise.util import format_measure, power_data_local_format, version_to_model
49+
from plugwise.util import (
50+
check_heater_central,
51+
format_measure,
52+
power_data_local_format,
53+
version_to_model,
54+
)
5055

5156
# This way of importing aiohttp is because of patch/mocking in testing (aiohttp timeouts)
5257
from defusedxml import ElementTree as etree
@@ -218,7 +223,7 @@ def _appliance_info_finder(self, appliance: etree, appl: Munch) -> Munch:
218223
return None
219224

220225
# Find the valid heater_central
221-
self._heater_id = self._check_heater_central()
226+
self._heater_id = check_heater_central(self._appliances)
222227

223228
# Info for On-Off device
224229
if self._on_off_device:
@@ -248,34 +253,6 @@ def _appliance_info_finder(self, appliance: etree, appl: Munch) -> Munch:
248253

249254
return appl
250255

251-
def _check_heater_central(self) -> str:
252-
"""Find the valid heater_central, helper-function for _appliance_info_finder().
253-
254-
Solution for Core Issue #104433,
255-
for a system that has two heater_central appliances.
256-
"""
257-
locator = "./appliance[type='heater_central']"
258-
hc_count = 0
259-
hc_list: list[dict[str, bool]] = []
260-
for heater_central in self._appliances.findall(locator):
261-
hc_count += 1
262-
hc_id: str = heater_central.attrib["id"]
263-
has_actuators: bool = (
264-
heater_central.find("actuator_functionalities/") is not None
265-
)
266-
hc_list.append({hc_id: has_actuators})
267-
268-
heater_central_id = list(hc_list[0].keys())[0]
269-
if hc_count > 1:
270-
for item in hc_list: # pragma: no cover
271-
for key, value in item.items(): # pragma: no cover
272-
if value: # pragma: no cover
273-
heater_central_id = key # pragma: no cover
274-
# Stop when a valid id is found
275-
break # pragma: no cover
276-
277-
return heater_central_id
278-
279256
def _p1_smartmeter_info_finder(self, appl: Munch) -> None:
280257
"""Collect P1 DSMR Smartmeter info."""
281258
loc_id = next(iter(self.loc_data.keys()))

plugwise/util.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,37 @@
1515
DeviceData,
1616
)
1717

18+
from defusedxml import ElementTree as etree
19+
20+
21+
def check_heater_central(xml: etree) -> str:
22+
"""Find the valid heater_central, helper-function for _appliance_info_finder().
23+
24+
Solution for Core Issue #104433,
25+
for a system that has two heater_central appliances.
26+
"""
27+
locator = "./appliance[type='heater_central']"
28+
hc_count = 0
29+
hc_list: list[dict[str, bool]] = []
30+
for heater_central in xml.findall(locator):
31+
hc_count += 1
32+
hc_id: str = heater_central.attrib["id"]
33+
has_actuators: bool = (
34+
heater_central.find("actuator_functionalities/") is not None
35+
)
36+
hc_list.append({hc_id: has_actuators})
37+
38+
heater_central_id = list(hc_list[0].keys())[0]
39+
if hc_count > 1:
40+
for item in hc_list: # pragma: no cover
41+
for key, value in item.items(): # pragma: no cover
42+
if value: # pragma: no cover
43+
heater_central_id = key # pragma: no cover
44+
# Stop when a valid id is found
45+
break # pragma: no cover
46+
47+
return heater_central_id
48+
1849

1950
def check_model(name: str | None, vendor_name: str | None) -> str | None:
2051
"""Model checking before using version_to_model."""

0 commit comments

Comments
 (0)