Skip to content

Commit 7112b86

Browse files
committed
Move _get_module_data() to common.py
1 parent f02c778 commit 7112b86

File tree

4 files changed

+81
-96
lines changed

4 files changed

+81
-96
lines changed

plugwise/common.py

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
from __future__ import annotations
66

7+
from plugwise.constants import ModelData
78
from plugwise.util import check_heater_central, check_model
89

910
from defusedxml import ElementTree as etree
@@ -16,6 +17,7 @@ class SmileCommon:
1617
def __init__(self) -> None:
1718
"""Init."""
1819
self._appliances: etree
20+
self._cooling_present: bool
1921
self._heater_id: str
2022
self._on_off_device: bool
2123
self._opentherm_device: bool
@@ -25,12 +27,11 @@ def smile(self, name: str) -> bool:
2527
"""Helper-function checking the smile-name."""
2628
return self.smile_name == name
2729

28-
def _appl_thermostat_info(self, xml: etree, appl: Munch) -> Munch:
30+
def _appl_thermostat_info(self, xml_1: etree, xml_2: etree, appl: Munch) -> Munch:
2931
"""Helper-function for _appliance_info_finder()."""
30-
# Collect thermostat device info
3132
locator = "./logs/point_log[type='thermostat']/thermostat"
3233
mod_type = "thermostat"
33-
module_data = self._get_module_data(xml, locator, mod_type)
34+
module_data = self._get_module_data(xml_1, xml_2, locator, mod_type)
3435
appl.vendor_name = module_data["vendor_name"]
3536
appl.model = check_model(module_data["vendor_model"], appl.vendor_name)
3637
appl.hardware = module_data["hardware_version"]
@@ -39,13 +40,20 @@ def _appl_thermostat_info(self, xml: etree, appl: Munch) -> Munch:
3940

4041
return appl
4142

42-
def _appl_heater_central_info(self, xml_1: etree, xml_2: etree, appl: Munch) -> Munch:
43+
def _appl_heater_central_info(
44+
self,
45+
xml_1: etree,
46+
xml_2: etree,
47+
xml_3: etree,
48+
appl: Munch
49+
) -> Munch:
4350
"""Helper-function for _appliance_info_finder()."""
4451
# Remove heater_central when no active device present
4552
if not self._opentherm_device and not self._on_off_device:
4653
return None
4754

4855
# Find the valid heater_central
56+
# xml_1 = self._appliances for legacy, self._domain_objects for actual
4957
self._heater_id = check_heater_central(xml_1)
5058

5159
# Info for On-Off device
@@ -57,16 +65,70 @@ def _appl_heater_central_info(self, xml_1: etree, xml_2: etree, appl: Munch) ->
5765

5866
# Info for OpenTherm device
5967
appl.name = "OpenTherm"
60-
locator1 = "./logs/point_log[type='flame_state']/boiler_state"
61-
locator2 = "./services/boiler_state"
68+
locator_1 = "./logs/point_log[type='flame_state']/boiler_state"
69+
locator_2 = "./services/boiler_state"
6270
mod_type = "boiler_state"
63-
module_data = self._get_module_data(xml_2, locator1, mod_type)
71+
# xml_2 = appliance
72+
# xml_3 = self._modules for legacy, self._domain_objects for actual
73+
module_data = self._get_module_data(xml_2, xml_3, locator_1, mod_type)
6474
if not module_data["contents"]:
65-
module_data = self._get_module_data(xml_2, locator2, mod_type)
75+
module_data = self._get_module_data(xml_2, xml_3, locator_2, mod_type)
6676
appl.vendor_name = module_data["vendor_name"]
6777
appl.hardware = module_data["hardware_version"]
6878
appl.model = module_data["vendor_model"]
6979
if appl.model is None:
70-
appl.model = "Generic heater"
80+
appl.model = (
81+
"Generic heater/cooler"
82+
if self._cooling_present
83+
else "Generic heater"
84+
)
7185

7286
return appl
87+
88+
def _get_module_data(
89+
self, xml_1: etree, xml_2: etree, locator: str, mod_type: str, legacy=False,
90+
) -> ModelData:
91+
"""Helper-function for _energy_device_info_finder() and _appliance_info_finder().
92+
93+
Collect requested info from MODULES.
94+
"""
95+
model_data: ModelData = {
96+
"contents": False,
97+
"firmware_version": None,
98+
"hardware_version": None,
99+
"reachable": None,
100+
"vendor_name": None,
101+
"vendor_model": None,
102+
"zigbee_mac_address": None,
103+
}
104+
# xml_1 = appliance
105+
if (appl_search := xml_1.find(locator)) is not None:
106+
link_id = appl_search.attrib["id"]
107+
loc = f".//services/{mod_type}[@id='{link_id}']...."
108+
if legacy:
109+
loc = f".//{mod_type}[@id='{link_id}']...."
110+
# Not possible to walrus for some reason...
111+
# xml_2 = self._modules for legacy, self._domain_objects for actual
112+
module = xml_2.find(loc)
113+
if module is not None: # pylint: disable=consider-using-assignment-expr
114+
model_data["contents"] = True
115+
if (vendor_name := module.find("vendor_name").text) is not None:
116+
model_data["vendor_name"] = vendor_name
117+
if "Plugwise" in vendor_name:
118+
model_data["vendor_name"] = vendor_name.split(" ", 1)[0]
119+
model_data["vendor_model"] = module.find("vendor_model").text
120+
model_data["hardware_version"] = module.find("hardware_version").text
121+
model_data["firmware_version"] = module.find("firmware_version").text
122+
if legacy:
123+
# Stretches
124+
if (router := module.find("./protocols/network_router")) is not None:
125+
model_data["zigbee_mac_address"] = router.find("mac_address").text
126+
# Also look for the Circle+/Stealth M+
127+
if (coord := module.find("./protocols/network_coordinator")) is not None:
128+
model_data["zigbee_mac_address"] = coord.find("mac_address").text
129+
# Adam
130+
elif (zb_node := module.find("./protocols/zig_bee_node")) is not None:
131+
model_data["zigbee_mac_address"] = zb_node.find("mac_address").text
132+
model_data["reachable"] = zb_node.find("reachable").text == "true"
133+
134+
return model_data

plugwise/helper.py

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
BinarySensorType,
4747
DeviceData,
4848
GatewayData,
49-
ModelData,
5049
SensorType,
5150
SpecialType,
5251
SwitchType,
@@ -261,43 +260,6 @@ def _all_locations(self) -> None:
261260

262261
self.loc_data[loc.loc_id] = {"name": loc.name}
263262

264-
def _get_module_data(
265-
self, appliance: etree, locator: str, mod_type: str
266-
) -> ModelData:
267-
"""Helper-function for _energy_device_info_finder() and _appliance_info_finder().
268-
269-
Collect requested info from MODULES.
270-
"""
271-
model_data: ModelData = {
272-
"contents": False,
273-
"firmware_version": None,
274-
"hardware_version": None,
275-
"reachable": None,
276-
"vendor_name": None,
277-
"vendor_model": None,
278-
"zigbee_mac_address": None,
279-
}
280-
if (appl_search := appliance.find(locator)) is not None:
281-
link_id = appl_search.attrib["id"]
282-
loc = f".//services/{mod_type}[@id='{link_id}']...."
283-
# Not possible to walrus for some reason...
284-
module = self._domain_objects.find(loc)
285-
if module is not None: # pylint: disable=consider-using-assignment-expr
286-
model_data["contents"] = True
287-
if (vendor_name := module.find("vendor_name").text) is not None:
288-
model_data["vendor_name"] = vendor_name
289-
if "Plugwise" in vendor_name:
290-
model_data["vendor_name"] = vendor_name.split(" ", 1)[0]
291-
model_data["vendor_model"] = module.find("vendor_model").text
292-
model_data["hardware_version"] = module.find("hardware_version").text
293-
model_data["firmware_version"] = module.find("firmware_version").text
294-
# Adam
295-
if (zb_node := module.find("./protocols/zig_bee_node")) is not None:
296-
model_data["zigbee_mac_address"] = zb_node.find("mac_address").text
297-
model_data["reachable"] = zb_node.find("reachable").text == "true"
298-
299-
return model_data
300-
301263
def _energy_device_info_finder(self, appliance: etree, appl: Munch) -> Munch:
302264
"""Helper-function for _appliance_info_finder().
303265
@@ -306,7 +268,7 @@ def _energy_device_info_finder(self, appliance: etree, appl: Munch) -> Munch:
306268
if self.smile_type == "power":
307269
locator = "./logs/point_log/electricity_point_meter"
308270
mod_type = "electricity_point_meter"
309-
module_data = self._get_module_data(appliance, locator, mod_type)
271+
module_data = self._get_module_data(appliance, self._domain_objects, locator, mod_type)
310272
appl.hardware = module_data["hardware_version"]
311273
appl.model = module_data["vendor_model"]
312274
appl.vendor_name = module_data["vendor_name"]
@@ -317,7 +279,7 @@ def _energy_device_info_finder(self, appliance: etree, appl: Munch) -> Munch:
317279
if self.smile(ADAM):
318280
locator = "./logs/interval_log/electricity_interval_meter"
319281
mod_type = "electricity_interval_meter"
320-
module_data = self._get_module_data(appliance, locator, mod_type)
282+
module_data = self._get_module_data(appliance, self._domain_objects, locator, mod_type)
321283
# Filter appliance without zigbee_mac, it's an orphaned device
322284
appl.zigbee_mac = module_data["zigbee_mac_address"]
323285
if appl.zigbee_mac is None:
@@ -372,11 +334,11 @@ def _appliance_info_finder(self, appliance: etree, appl: Munch) -> Munch:
372334

373335
# Collect thermostat device info
374336
if appl.pwclass in THERMOSTAT_CLASSES:
375-
return self._appl_thermostat_info(appliance, appl)
337+
return self._appl_thermostat_info(appliance, self._domain_objects, appl)
376338

377339
# Collect extra heater_central device info
378340
if appl.pwclass == "heater_central":
379-
appl = self._appl_heater_central_info(self._domain_objects, appliance, appl)
341+
appl = self._appl_heater_central_info(self._domain_objects, appliance, self._domain_objects, appl)
380342
# Anna + Loria: collect dhw control operation modes
381343
dhw_mode_list: list[str] = []
382344
locator = "./actuator_functionalities/domestic_hot_water_mode_control_functionality"
@@ -672,12 +634,12 @@ def _wireless_availability(self, appliance: etree, data: DeviceData) -> None:
672634
# Collect for Plugs
673635
locator = "./logs/interval_log/electricity_interval_meter"
674636
mod_type = "electricity_interval_meter"
675-
module_data = self._get_module_data(appliance, locator, mod_type)
637+
module_data = self._get_module_data(appliance, self._domain_objects, locator, mod_type)
676638
if module_data["reachable"] is None:
677639
# Collect for wireless thermostats
678640
locator = "./logs/point_log[type='thermostat']/thermostat"
679641
mod_type = "thermostat"
680-
module_data = self._get_module_data(appliance, locator, mod_type)
642+
module_data = self._get_module_data(appliance, self._domain_objects, locator, mod_type)
681643

682644
if module_data["reachable"] is not None:
683645
data["available"] = module_data["reachable"]

plugwise/legacy/helper.py

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
BinarySensorType,
4242
DeviceData,
4343
GatewayData,
44-
ModelData,
4544
SensorType,
4645
SpecialType,
4746
SwitchType,
@@ -128,45 +127,6 @@ def _all_locations(self) -> None:
128127

129128
self.loc_data[loc.loc_id] = {"name": loc.name}
130129

131-
def _get_module_data(
132-
self, appliance: etree, locator: str, mod_type: str
133-
) -> ModelData:
134-
"""Helper-function for _energy_device_info_finder() and _appliance_info_finder().
135-
136-
Collect requested info from MODULES.
137-
"""
138-
model_data: ModelData = {
139-
"contents": False,
140-
"firmware_version": None,
141-
"hardware_version": None,
142-
"reachable": None,
143-
"vendor_name": None,
144-
"vendor_model": None,
145-
"zigbee_mac_address": None,
146-
}
147-
if (appl_search := appliance.find(locator)) is not None:
148-
link_id = appl_search.attrib["id"]
149-
loc = f".//{mod_type}[@id='{link_id}']...."
150-
# Not possible to walrus for some reason...
151-
module = self._modules.find(loc)
152-
if module is not None: # pylint: disable=consider-using-assignment-expr
153-
model_data["contents"] = True
154-
if (vendor_name := module.find("vendor_name").text) is not None:
155-
model_data["vendor_name"] = vendor_name
156-
if "Plugwise" in vendor_name:
157-
model_data["vendor_name"] = vendor_name.split(" ", 1)[0]
158-
model_data["vendor_model"] = module.find("vendor_model").text
159-
model_data["hardware_version"] = module.find("hardware_version").text
160-
model_data["firmware_version"] = module.find("firmware_version").text
161-
# Stretches
162-
if (router := module.find("./protocols/network_router")) is not None:
163-
model_data["zigbee_mac_address"] = router.find("mac_address").text
164-
# Also look for the Circle+/Stealth M+
165-
if (coord := module.find("./protocols/network_coordinator")) is not None:
166-
model_data["zigbee_mac_address"] = coord.find("mac_address").text
167-
168-
return model_data
169-
170130
def _energy_device_info_finder(self, appliance: etree, appl: Munch) -> Munch:
171131
"""Helper-function for _appliance_info_finder().
172132
@@ -176,7 +136,7 @@ def _energy_device_info_finder(self, appliance: etree, appl: Munch) -> Munch:
176136
locator = "./services/electricity_point_meter"
177137
mod_type = "electricity_point_meter"
178138

179-
module_data = self._get_module_data(appliance, locator, mod_type)
139+
module_data = self._get_module_data(appliance, self._modules, locator, mod_type, legacy=True)
180140
appl.zigbee_mac = module_data["zigbee_mac_address"]
181141
# Filter appliance without zigbee_mac, it's an orphaned device
182142
if appl.zigbee_mac is None and self.smile_type != "power":
@@ -198,11 +158,11 @@ def _appliance_info_finder(self, appliance: etree, appl: Munch) -> Munch:
198158
"""Collect device info (Smile/Stretch, Thermostats, OpenTherm/On-Off): firmware, model and vendor name."""
199159
# Collect thermostat device info
200160
if appl.pwclass in THERMOSTAT_CLASSES:
201-
return self._appl_thermostat_info(appliance, appl)
161+
return self._appl_thermostat_info(appliance, self._modules, appl)
202162

203163
# Collect heater_central device info
204164
if appl.pwclass == "heater_central":
205-
return self._appl_heater_central_info(self._appliances, appliance, appl)
165+
return self._appl_heater_central_info(self._appliances, appliance, self._modules, appl)
206166

207167
# Collect info from Stretches
208168
appl = self._energy_device_info_finder(appliance, appl)

plugwise/legacy/smile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def __init__(
6969
)
7070
SmileLegacyData.__init__(self)
7171

72+
self._cooling_present = False
7273
self._is_thermostat = _is_thermostat
7374
self._on_off_device = _on_off_device
7475
self._opentherm_device = _opentherm_device

0 commit comments

Comments
 (0)