Skip to content

Commit 25177a7

Browse files
committed
Combine entity_id-switch in one function
1 parent 5e35ae8 commit 25177a7

File tree

1 file changed

+34
-44
lines changed

1 file changed

+34
-44
lines changed

plugwise/helper.py

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,6 @@ def _all_appliances(self) -> None:
283283
284284
Also, collect the P1 smartmeter info from a location
285285
as this one is not available as an appliance.
286-
Note: For P1, the entity_id for the gateway and smartmeter are
287-
switched to maintain backward compatibility with existing implementations.
288286
"""
289287
self._count = 0
290288
self._all_locations()
@@ -337,55 +335,20 @@ def _all_appliances(self) -> None:
337335
if not (appl := self._appliance_info_finder(appl, appliance)):
338336
continue
339337

340-
# P1: for gateway and smartmeter switch entity_id - part 1
341-
# This is done to avoid breakage in HA Core
342-
if appl.pwclass == "gateway" and self.smile_type == "power":
343-
appl.entity_id = appl.location
344-
345338
self._create_gw_entities(appl)
346339

347-
# Collect P1 Smartmeter info
348-
self._get_smartmeter_info()
340+
if self.smile_type == "power":
341+
self._get_p1_smartmeter_info()
349342

350343
# Sort the gw_entities
351344
self._sort_gw_entities()
352345

353-
def _get_smartmeter_info(self) -> None:
354-
"""For P1 collect the connected SmartMeter info from the Home/building location."""
355-
if self.smile_type == "power":
356-
self._p1_smartmeter_info_finder()
357-
# P1: for gateway and smartmeter switch entity_id - part 2
358-
for item in self.gw_entities:
359-
if item != self.gateway_id:
360-
self.gateway_id = item
361-
# Leave for-loop to avoid a 2nd entity_id switch
362-
break
363-
364-
def _sort_gw_entities(self) -> None:
365-
"""Place the gateway and optional heater_central entities as 1st and 2nd."""
366-
for dev_class in PRIORITY_DEVICE_CLASSES:
367-
for entity_id, entity in dict(self.gw_entities).items():
368-
if entity["dev_class"] == dev_class:
369-
priority_entity = entity
370-
self.gw_entities.pop(entity_id)
371-
other_entities = self.gw_entities
372-
priority_entities = {entity_id: priority_entity}
373-
self.gw_entities = {**priority_entities, **other_entities}
374-
375-
def _all_locations(self) -> None:
376-
"""Collect all locations."""
377-
loc = Munch()
378-
locations = self._domain_objects.findall("./location")
379-
for location in locations:
380-
loc.name = location.find("name").text
381-
loc.loc_id = location.attrib["id"]
382-
if loc.name == "Home":
383-
self._home_location = loc.loc_id
384-
385-
self._loc_data[loc.loc_id] = {"name": loc.name}
346+
def _get_p1_smartmeter_info(self) -> None:
347+
"""For P1 collect the connected SmartMeter info from the Home/building location.
386348
387-
def _p1_smartmeter_info_finder(self) -> None:
388-
"""Collect P1 DSMR SmartMeter info."""
349+
Note: For P1, the entity_id for the gateway and smartmeter are
350+
switched to maintain backward compatibility with existing implementations.
351+
"""
389352
appl = Munch()
390353
loc_id = next(iter(self._loc_data.keys()))
391354
if (
@@ -411,8 +374,35 @@ def _p1_smartmeter_info_finder(self) -> None:
411374
appl.vendor_name = module_data["vendor_name"]
412375
appl.zigbee_mac = None
413376

377+
# Replace the entity_id of the gateway by the smartmeter location_id
378+
self.gw_entities[loc_id] = self.gw_entities.pop(self.gateway_id)
379+
self.gateway_id = loc_id
380+
414381
self._create_gw_entities(appl)
415382

383+
def _sort_gw_entities(self) -> None:
384+
"""Place the gateway and optional heater_central entities as 1st and 2nd."""
385+
for dev_class in PRIORITY_DEVICE_CLASSES:
386+
for entity_id, entity in dict(self.gw_entities).items():
387+
if entity["dev_class"] == dev_class:
388+
priority_entity = entity
389+
self.gw_entities.pop(entity_id)
390+
other_entities = self.gw_entities
391+
priority_entities = {entity_id: priority_entity}
392+
self.gw_entities = {**priority_entities, **other_entities}
393+
394+
def _all_locations(self) -> None:
395+
"""Collect all locations."""
396+
loc = Munch()
397+
locations = self._domain_objects.findall("./location")
398+
for location in locations:
399+
loc.name = location.find("name").text
400+
loc.loc_id = location.attrib["id"]
401+
if loc.name == "Home":
402+
self._home_location = loc.loc_id
403+
404+
self._loc_data[loc.loc_id] = {"name": loc.name}
405+
416406
def _appliance_info_finder(self, appl: Munch, appliance: etree) -> Munch:
417407
"""Collect info for all appliances found."""
418408
match appl.pwclass:

0 commit comments

Comments
 (0)