Skip to content

Commit 12e4b01

Browse files
committed
Optimize energy_device-related code
1 parent 1519d70 commit 12e4b01

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

plugwise/helper.py

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ def _all_appliances(self) -> None:
269269
for appliance in self._domain_objects.findall("./appliance"):
270270
appl = Munch()
271271
appl.pwclass = appliance.find("type").text
272+
# Don't collect data for the OpenThermGateway appliance
273+
if appl.pwclass == "open_therm_gateway":
274+
continue
275+
272276
# Extend device_class name of Plugs (Plugwise and Aqara) - Pw-Beta Issue #739
273277
description = appliance.find("description").text
274278
if description is not None and (
@@ -291,6 +295,10 @@ def _all_appliances(self) -> None:
291295
elif appl.pwclass not in THERMOSTAT_CLASSES:
292296
appl.location = self._home_location
293297

298+
# Don't show orphaned thermostat-types
299+
if appl.pwclass in THERMOSTAT_CLASSES and appl.location is None:
300+
continue
301+
294302
appl.dev_id = appliance.attrib["id"]
295303
appl.name = appliance.find("name").text
296304
appl.model = None
@@ -301,24 +309,22 @@ def _all_appliances(self) -> None:
301309
appl.zigbee_mac = None
302310
appl.vendor_name = None
303311

304-
# Determine class for this appliance
305-
# Skip on heater_central when no active device present
306-
if not (appl := self._appliance_info_finder(appl, appliance)):
307-
continue
312+
# Collect appliance info
313+
appl = self._appliance_info_finder(appl, appliance)
308314

309315
# Skip orphaned heater_central (Core Issue #104433)
310316
if appl.pwclass == "heater_central" and appl.dev_id != self._heater_id:
311317
continue
312318

319+
# Skip orphaned/removed plug-type
320+
if "_plug" in appl.pwclass and appl.zigbee_mac is None:
321+
continue
322+
313323
# P1: for gateway and smartmeter switch device_id - part 1
314324
# This is done to avoid breakage in HA Core
315325
if appl.pwclass == "gateway" and self.smile_type == "power":
316326
appl.dev_id = appl.location
317327

318-
# Don't show orphaned thermostat-types or the OpenTherm Gateway.
319-
if appl.pwclass in THERMOSTAT_CLASSES and appl.location is None:
320-
continue
321-
322328
self._create_gw_devices(appl)
323329

324330
# For P1 collect the connected SmartMeter info
@@ -356,16 +362,22 @@ def _all_locations(self) -> None:
356362
def _p1_smartmeter_info_finder(self, appl: Munch) -> None:
357363
"""Collect P1 DSMR Smartmeter info."""
358364
loc_id = next(iter(self.loc_data.keys()))
365+
location = self._domain_objects.find(f'./location[@id="{loc_id}"]')
366+
locator = "./logs/point_log/electricity_point_meter"
367+
mod_type = "electricity_point_meter"
368+
module_data = self._get_module_data(location, locator, mod_type)
369+
359370
appl.dev_id = self.gateway_id
371+
appl.firmware = module_data["firmware_version"]
372+
appl.hardware = module_data["hardware_version"]
360373
appl.location = loc_id
361374
appl.mac = None
362-
appl.model = None
375+
appl.model = module_data["vendor_model"] # don't use model_id for Smartmeter
363376
appl.model_id = None
364377
appl.name = "P1"
365378
appl.pwclass = "smartmeter"
379+
appl.vendor_name = module_data["vendor_name"]
366380
appl.zigbee_mac = None
367-
location = self._domain_objects.find(f'./location[@id="{loc_id}"]')
368-
appl = self._energy_device_info_finder(appl, location)
369381

370382
self._create_gw_devices(appl)
371383

@@ -383,44 +395,24 @@ def _appliance_info_finder(self, appl: Munch, appliance: etree) -> Munch:
383395
self._appl_heater_central_info(appl, appliance, False) # False means non-legacy device
384396
self._appl_dhw_mode_info(appl, appliance)
385397
return appl
386-
case _:
387-
# Collect info from power-related devices (Plug, Aqara Smart Plug)
388-
return self._energy_device_info_finder(appl, appliance)
389-
390-
def _energy_device_info_finder(self, appl: Munch, appliance: etree) -> Munch:
391-
"""Helper-function for _appliance_info_finder().
392-
393-
Collect energy device info (Smartmeter): firmware, model and vendor name.
394-
"""
395-
if self.smile_type == "power":
396-
locator = "./logs/point_log/electricity_point_meter"
397-
mod_type = "electricity_point_meter"
398-
module_data = self._get_module_data(appliance, locator, mod_type)
399-
appl.hardware = module_data["hardware_version"]
400-
appl.model = module_data["vendor_model"] # don't use model_id for Smartmeter
401-
appl.vendor_name = module_data["vendor_name"]
402-
appl.firmware = module_data["firmware_version"]
403-
404-
return appl
405-
406-
if self.smile(ADAM):
407-
locator = "./logs/interval_log/electricity_interval_meter"
408-
mod_type = "electricity_interval_meter"
409-
module_data = self._get_module_data(appliance, locator, mod_type)
410-
# Filter appliance without zigbee_mac, it's an orphaned device
411-
appl.zigbee_mac = module_data["zigbee_mac_address"]
412-
if appl.zigbee_mac is None:
413-
return None
414-
415-
appl.vendor_name = module_data["vendor_name"]
416-
appl.model_id = module_data["vendor_model"]
417-
appl.model = check_model(appl.model_id, appl.vendor_name)
418-
appl.hardware = module_data["hardware_version"]
419-
appl.firmware = module_data["firmware_version"]
420-
421-
return appl
422-
423-
return appl # pragma: no cover
398+
case _ as s if s.endswith("_plug"):
399+
# Collect info from plug-types (Plug, Aqara Smart Plug)
400+
locator = "./logs/interval_log/electricity_interval_meter"
401+
mod_type = "electricity_interval_meter"
402+
module_data = self._get_module_data(appliance, locator, mod_type)
403+
# A plug without module-data is orphaned/ no present
404+
if not module_data["contents"]:
405+
return appl
406+
407+
appl.firmware = module_data["firmware_version"]
408+
appl.hardware = module_data["hardware_version"]
409+
appl.model_id = module_data["vendor_model"]
410+
appl.vendor_name = module_data["vendor_name"]
411+
appl.model = check_model(appl.model_id, appl.vendor_name)
412+
appl.zigbee_mac = module_data["zigbee_mac_address"]
413+
return appl
414+
case _: # pragma: no cover
415+
return appl
424416

425417
def _appl_gateway_info(self, appl: Munch, appliance: etree) -> Munch:
426418
"""Helper-function for _appliance_info_finder()."""

plugwise/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def check_model(name: str | None, vendor_name: str | None) -> str | None:
115115
if name is not None and "lumi.plug" in name:
116116
return "Aqara Smart Plug"
117117

118-
return name
118+
return name # pragma: no cover
119119

120120

121121
def common_match_cases(

0 commit comments

Comments
 (0)