Skip to content

Commit 62ac03a

Browse files
committed
Further cleaning
1 parent 6e34793 commit 62ac03a

File tree

4 files changed

+54
-52
lines changed

4 files changed

+54
-52
lines changed

plugwise/common.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@
1616
SWITCH_GROUP_TYPES,
1717
ApplianceType,
1818
GwEntityData,
19-
ModuleData,
20-
)
21-
from plugwise.util import (
22-
check_heater_central,
23-
check_model,
24-
get_vendor_name,
25-
return_valid,
19+
# ModuleData,
2620
)
21+
from plugwise.util import check_heater_central, check_model, return_valid
2722

2823
from defusedxml import ElementTree as etree
2924
from munch import Munch
3025

26+
from .model import ModuleData
27+
3128

3229
def get_zigbee_data(
3330
module: etree.Element, module_data: ModuleData, legacy: bool
@@ -120,7 +117,7 @@ def _appl_heater_central_info(
120117
return appl
121118

122119
def _appl_thermostat_info(
123-
self, appl: Munch, xml_1: etree.Element, xml_2: etree.Element = None
120+
self, appl: Appliance, xml_1: etree.Element, xml_2: etree.Element = None
124121
) -> Munch:
125122
"""Helper-function for _appliance_info_finder()."""
126123
locator = "./logs/point_log[type='thermostat']/thermostat"
@@ -144,21 +141,21 @@ def _appl_thermostat_info(
144141

145142
return appl
146143

147-
def _create_gw_entities(self, appl: Munch) -> None:
144+
def _create_gw_entities(self, appl: Appliance) -> None:
148145
"""Helper-function for creating/updating gw_entities."""
149-
self.gw_entities[appl.entity_id] = {"dev_class": appl.pwclass}
146+
self.gw_entities[appl.id] = {"dev_class": appl.type}
150147
self._count += 1
151148
for key, value in {
152149
"available": appl.available,
153-
"firmware": appl.firmware,
154-
"hardware": appl.hardware,
150+
"firmware": appl.firmware_version,
151+
"hardware": appl.hardware_version,
155152
"location": appl.location,
156-
"mac_address": appl.mac,
153+
"mac_address": appl.mac_address,
157154
"model": appl.model,
158155
"model_id": appl.model_id,
159156
"name": appl.name,
160157
"vendor": appl.vendor_name,
161-
"zigbee_mac_address": appl.zigbee_mac,
158+
"zigbee_mac_address": appl.zigbee_mac_address,
162159
}.items():
163160
if value is not None or key == "location":
164161
appl_key = cast(ApplianceType, key)
@@ -237,31 +234,36 @@ def _get_lock_state(
237234

238235
def _get_module_data(
239236
self,
240-
xml_1: etree.Element,
241-
locator: str,
242237
key: str | None = None,
243-
xml_2: etree.Element | None = None,
244238
legacy: bool = False,
245239
) -> ModuleData:
246240
"""Helper-function for _energy_device_info_finder() and _appliance_info_finder().
247241
248242
Collect requested info from MODULES.
249243
"""
250-
module_data: ModuleData = {
251-
"contents": False,
252-
"firmware_version": None,
253-
"hardware_version": None,
254-
"reachable": None,
255-
"vendor_name": None,
256-
"vendor_model": None,
257-
"zigbee_mac_address": None,
258-
}
259-
260-
for appl_search in xml_1.findall(locator):
261-
link_tag = appl_search.tag
262-
if key is not None and key not in link_tag:
244+
module = self.data.get_module(link_id)
245+
246+
for service_type, services in appliance.services.iter_services():
247+
if key and key not in service_type:
248+
continue
249+
for service in services:
250+
module = self.data.get_module(service.id)
251+
if not module:
263252
continue
264253

254+
return ModuleData(
255+
contents=True,
256+
firmware_version=None,
257+
hardware_version=None,
258+
reachable=None,
259+
vendor_name=None,
260+
vendor_model=None,
261+
zigbee_mac_address=None,
262+
)
263+
return ModuleData()
264+
265+
# TODO legacy
266+
"""
265267
link_id = appl_search.get("id")
266268
loc = f".//services/{link_tag}[@id='{link_id}']...."
267269
# Not possible to walrus for some reason...
@@ -277,5 +279,4 @@ def _get_module_data(
277279
get_zigbee_data(module, module_data, legacy)
278280
279281
break
280-
281-
return module_data
282+
"""

plugwise/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@
419419
)
420420

421421

422+
# TODO recreate or obsolete
422423
class ModuleData(TypedDict):
423424
"""The Module data class."""
424425

plugwise/helper.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,6 @@ def _get_appliances(self) -> None:
105105
self._get_locations()
106106

107107
for appliance in self.data.appliance:
108-
appl = Munch()
109-
appl.available = None
110-
appl.entity_id = appliance.id
111-
appl.firmware = None
112-
appl.hardware = None
113-
appl.location = None
114-
appl.mac = None
115-
appl.model = None
116-
appl.model_id = None
117-
appl.module_id = None
118-
appl.name = appliance.name
119-
appl.pwclass = appliance.type
120-
appl.zigbee_mac = None
121-
appl.vendor_name = None
122-
123108
# Don't collect data for the OpenThermGateway appliance, skip thermostat(s)
124109
# without actuator_functionalities, should be an orphaned device(s) (Core #81712)
125110
if appliance.type == ApplianceType.OPENTHERMGW or (
@@ -129,7 +114,7 @@ def _get_appliances(self) -> None:
129114
continue
130115

131116
if appliance.location is not None:
132-
appl.fixed_location = appliance.id
117+
appliance.fixed_location = appliance.id
133118
# Set location to the _home_loc_id when the appliance-location is not found,
134119
# except for thermostat-devices without a location, they are not active
135120
elif appliance.type not in THERMOSTAT_CLASSES:
@@ -151,8 +136,6 @@ def _get_appliances(self) -> None:
151136
# if not (appl := self._appliance_info_finder(appl, appliance)):
152137
# continue
153138

154-
self._create_gw_entities(appl)
155-
156139
# A smartmeter is not present as an appliance, add it specifically
157140
if self.smile.type == "power" or self.smile.anna_p1:
158141
self._get_p1_smartmeter_info()
@@ -166,7 +149,6 @@ def _get_p1_smartmeter_info(self) -> None:
166149
Note: For P1, the entity_id for the gateway and smartmeter are switched to maintain
167150
backward compatibility. For Anna P1, the smartmeter uses the home location_id directly.
168151
"""
169-
appl = Munch()
170152
locator = MODULE_LOCATOR
171153
tag = "electricity"
172154
module_data = self._get_module_data(self._home_location, locator, key=tag)

plugwise/model.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ class DomainObjects(PWBase):
318318
# Runtime-only cache
319319
_appliance_index: dict[str, Appliance] = {}
320320
_location_index: dict[str, Location] = {}
321+
_module_index: dict[str, Module] = {}
321322

322323
def model_post_init(self, __context):
323324
"""Build index for referencing by ID.
@@ -326,15 +327,20 @@ def model_post_init(self, __context):
326327
"""
327328
self._appliance_index = {a.id: a for a in self.appliance}
328329
self._location_index = {a.id: a for a in self.location}
330+
self._module_index = {a.id: a for a in self.module}
329331

330332
def get_appliance(self, id: str) -> Appliance | None:
331333
"""Get Appliance by ID."""
332334
return self._appliance_index.get(id)
333335

334336
def get_location(self, id: str) -> Location | None:
335-
"""Get Location by ID."""
337+
"""Get Location by ID."""
336338
return self._location_index.get(id)
337339

340+
def get_module(self, id: str) -> Module | None:
341+
"""Get Module by ID."""
342+
return self._module_index.get(id)
343+
338344

339345
class PlugwiseData(PWBase):
340346
"""Main XML definition."""
@@ -395,3 +401,15 @@ class GatewayData(BaseModel):
395401
def model_post_init(self, __context):
396402
"""Init arbitrary types."""
397403
self.version = Version(self.version)
404+
405+
406+
class ModuleData(BaseModel):
407+
"""Module model."""
408+
409+
contents: bool = False
410+
firmware_version: str | None = None
411+
hardware_version: str | None = None
412+
reachable: bool | None = None
413+
vendor_name: str | None = None
414+
vendor_model: str | None = None
415+
zigbee_mac_address: str | None = None

0 commit comments

Comments
 (0)