Skip to content

Commit f02c778

Browse files
committed
Move _appliance_info_finder() common parts to common.py
1 parent 1140a35 commit f02c778

File tree

3 files changed

+61
-88
lines changed

3 files changed

+61
-88
lines changed

plugwise/common.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,69 @@
44
"""
55
from __future__ import annotations
66

7+
from plugwise.util import check_heater_central, check_model
8+
9+
from defusedxml import ElementTree as etree
10+
from munch import Munch
11+
712

813
class SmileCommon:
914
"""The SmileCommon class."""
1015

1116
def __init__(self) -> None:
1217
"""Init."""
18+
self._appliances: etree
19+
self._heater_id: str
20+
self._on_off_device: bool
21+
self._opentherm_device: bool
1322
self.smile_name: str
1423

1524
def smile(self, name: str) -> bool:
1625
"""Helper-function checking the smile-name."""
1726
return self.smile_name == name
27+
28+
def _appl_thermostat_info(self, xml: etree, appl: Munch) -> Munch:
29+
"""Helper-function for _appliance_info_finder()."""
30+
# Collect thermostat device info
31+
locator = "./logs/point_log[type='thermostat']/thermostat"
32+
mod_type = "thermostat"
33+
module_data = self._get_module_data(xml, locator, mod_type)
34+
appl.vendor_name = module_data["vendor_name"]
35+
appl.model = check_model(module_data["vendor_model"], appl.vendor_name)
36+
appl.hardware = module_data["hardware_version"]
37+
appl.firmware = module_data["firmware_version"]
38+
appl.zigbee_mac = module_data["zigbee_mac_address"]
39+
40+
return appl
41+
42+
def _appl_heater_central_info(self, xml_1: etree, xml_2: etree, appl: Munch) -> Munch:
43+
"""Helper-function for _appliance_info_finder()."""
44+
# Remove heater_central when no active device present
45+
if not self._opentherm_device and not self._on_off_device:
46+
return None
47+
48+
# Find the valid heater_central
49+
self._heater_id = check_heater_central(xml_1)
50+
51+
# Info for On-Off device
52+
if self._on_off_device:
53+
appl.name = "OnOff" # pragma: no cover
54+
appl.vendor_name = None # pragma: no cover
55+
appl.model = "Unknown" # pragma: no cover
56+
return appl # pragma: no cover
57+
58+
# Info for OpenTherm device
59+
appl.name = "OpenTherm"
60+
locator1 = "./logs/point_log[type='flame_state']/boiler_state"
61+
locator2 = "./services/boiler_state"
62+
mod_type = "boiler_state"
63+
module_data = self._get_module_data(xml_2, locator1, mod_type)
64+
if not module_data["contents"]:
65+
module_data = self._get_module_data(xml_2, locator2, mod_type)
66+
appl.vendor_name = module_data["vendor_name"]
67+
appl.hardware = module_data["hardware_version"]
68+
appl.model = module_data["vendor_model"]
69+
if appl.model is None:
70+
appl.model = "Generic heater"
71+
72+
return appl

plugwise/helper.py

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
ResponseError,
6161
)
6262
from plugwise.util import (
63-
check_heater_central,
6463
check_model,
6564
escape_illegal_xml_characters,
6665
format_measure,
@@ -373,51 +372,11 @@ def _appliance_info_finder(self, appliance: etree, appl: Munch) -> Munch:
373372

374373
# Collect thermostat device info
375374
if appl.pwclass in THERMOSTAT_CLASSES:
376-
locator = "./logs/point_log[type='thermostat']/thermostat"
377-
mod_type = "thermostat"
378-
module_data = self._get_module_data(appliance, locator, mod_type)
379-
appl.vendor_name = module_data["vendor_name"]
380-
appl.model = check_model(module_data["vendor_model"], appl.vendor_name)
381-
appl.hardware = module_data["hardware_version"]
382-
appl.firmware = module_data["firmware_version"]
383-
appl.zigbee_mac = module_data["zigbee_mac_address"]
375+
return self._appl_thermostat_info(appliance, appl)
384376

385-
return appl
386-
387-
# Collect heater_central device info
377+
# Collect extra heater_central device info
388378
if appl.pwclass == "heater_central":
389-
# Remove heater_central when no active device present
390-
if not self._opentherm_device and not self._on_off_device:
391-
return None # pragma: no cover
392-
393-
# Find the valid heater_central
394-
self._heater_id = check_heater_central(self._domain_objects)
395-
396-
# Info for On-Off device
397-
if self._on_off_device:
398-
appl.name = "OnOff"
399-
appl.vendor_name = None
400-
appl.model = "Unknown"
401-
return appl
402-
403-
# Info for OpenTherm device
404-
appl.name = "OpenTherm"
405-
locator1 = "./logs/point_log[type='flame_state']/boiler_state"
406-
locator2 = "./services/boiler_state"
407-
mod_type = "boiler_state"
408-
module_data = self._get_module_data(appliance, locator1, mod_type)
409-
if not module_data["contents"]:
410-
module_data = self._get_module_data(appliance, locator2, mod_type)
411-
appl.vendor_name = module_data["vendor_name"]
412-
appl.hardware = module_data["hardware_version"]
413-
appl.model = check_model(module_data["vendor_model"], appl.vendor_name)
414-
if appl.model is None:
415-
appl.model = (
416-
"Generic heater/cooler"
417-
if self._cooling_present
418-
else "Generic heater"
419-
)
420-
379+
appl = self._appl_heater_central_info(self._domain_objects, appliance, appl)
421380
# Anna + Loria: collect dhw control operation modes
422381
dhw_mode_list: list[str] = []
423382
locator = "./actuator_functionalities/domestic_hot_water_mode_control_functionality"

plugwise/legacy/helper.py

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@
4747
SwitchType,
4848
ThermoLoc,
4949
)
50-
from plugwise.util import (
51-
check_heater_central,
52-
format_measure,
53-
power_data_local_format,
54-
version_to_model,
55-
)
50+
from plugwise.util import format_measure, power_data_local_format, version_to_model
5651

5752
# This way of importing aiohttp is because of patch/mocking in testing (aiohttp timeouts)
5853
from defusedxml import ElementTree as etree
@@ -203,47 +198,11 @@ def _appliance_info_finder(self, appliance: etree, appl: Munch) -> Munch:
203198
"""Collect device info (Smile/Stretch, Thermostats, OpenTherm/On-Off): firmware, model and vendor name."""
204199
# Collect thermostat device info
205200
if appl.pwclass in THERMOSTAT_CLASSES:
206-
locator = "./logs/point_log[type='thermostat']/thermostat"
207-
mod_type = "thermostat"
208-
module_data = self._get_module_data(appliance, locator, mod_type)
209-
appl.vendor_name = module_data["vendor_name"]
210-
appl.model = module_data["vendor_model"]
211-
appl.hardware = module_data["hardware_version"]
212-
appl.firmware = module_data["firmware_version"]
213-
appl.zigbee_mac = module_data["zigbee_mac_address"]
214-
return appl
201+
return self._appl_thermostat_info(appliance, appl)
215202

216203
# Collect heater_central device info
217204
if appl.pwclass == "heater_central":
218-
# Remove heater_central when no active device present
219-
if not self._opentherm_device and not self._on_off_device:
220-
return None
221-
222-
# Find the valid heater_central
223-
self._heater_id = check_heater_central(self._appliances)
224-
225-
# Info for On-Off device
226-
if self._on_off_device:
227-
appl.name = "OnOff" # pragma: no cover
228-
appl.vendor_name = None # pragma: no cover
229-
appl.model = "Unknown" # pragma: no cover
230-
return appl # pragma: no cover
231-
232-
# Info for OpenTherm device
233-
appl.name = "OpenTherm"
234-
locator1 = "./logs/point_log[type='flame_state']/boiler_state"
235-
locator2 = "./services/boiler_state"
236-
mod_type = "boiler_state"
237-
module_data = self._get_module_data(appliance, locator1, mod_type)
238-
if not module_data["contents"]:
239-
module_data = self._get_module_data(appliance, locator2, mod_type)
240-
appl.vendor_name = module_data["vendor_name"]
241-
appl.hardware = module_data["hardware_version"]
242-
appl.model = module_data["vendor_model"]
243-
if appl.model is None:
244-
appl.model = "Generic heater"
245-
246-
return appl
205+
return self._appl_heater_central_info(self._appliances, appliance, appl)
247206

248207
# Collect info from Stretches
249208
appl = self._energy_device_info_finder(appliance, appl)

0 commit comments

Comments
 (0)