Skip to content

Commit

Permalink
Combine entity_id-switch in one function
Browse files Browse the repository at this point in the history
  • Loading branch information
bouwew committed Dec 12, 2024
1 parent 5e35ae8 commit 971de0b
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions plugwise/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,6 @@ def _all_appliances(self) -> None:
Also, collect the P1 smartmeter info from a location
as this one is not available as an appliance.
Note: For P1, the entity_id for the gateway and smartmeter are
switched to maintain backward compatibility with existing implementations.
"""
self._count = 0
self._all_locations()
Expand Down Expand Up @@ -337,11 +335,6 @@ def _all_appliances(self) -> None:
if not (appl := self._appliance_info_finder(appl, appliance)):
continue

# P1: for gateway and smartmeter switch entity_id - part 1
# This is done to avoid breakage in HA Core
if appl.pwclass == "gateway" and self.smile_type == "power":
appl.entity_id = appl.location

self._create_gw_entities(appl)

# Collect P1 Smartmeter info
Expand All @@ -351,15 +344,25 @@ def _all_appliances(self) -> None:
self._sort_gw_entities()

def _get_smartmeter_info(self) -> None:
"""For P1 collect the connected SmartMeter info from the Home/building location."""
"""For P1 collect the connected SmartMeter info from the Home/building location.
Note: For P1, the entity_id for the gateway and smartmeter are
switched to maintain backward compatibility with existing implementations.
"""
if self.smile_type == "power":
self._p1_smartmeter_info_finder()
# P1: for gateway and smartmeter switch entity_id - part 2
for item in self.gw_entities:
if item != self.gateway_id:
self.gateway_id = item
# Leave for-loop to avoid a 2nd entity_id switch
break
# Switch entity_ids
entity_id_0 = list(self.gw_entities)[0]
entity_0 = list(self.gw_entities.values())[0]
entity_id_1 = list(self.gw_entities)[1]
entity_1 = list(self.gw_entities.values())[1]
self.gw_entities[entity_id_0] = entity_1
self.gw_entities[entity_id_1] = entity_0
# Update gateway_id
if entity_id_0 == self.gateway_id:
self.gateway_id = entity_id_1
else:
self.gateway_id = entity_id_0

def _sort_gw_entities(self) -> None:
"""Place the gateway and optional heater_central entities as 1st and 2nd."""
Expand Down

0 comments on commit 971de0b

Please sign in to comment.