Skip to content

Commit 87a580c

Browse files
committed
Further optimize _match_and_rank_thermostats()
1 parent 74e71c0 commit 87a580c

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

plugwise/helper.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,8 @@ def _scan_thermostats(self) -> None:
778778
Update locations with thermostat ranking results and use
779779
the result to update the device_class of secondary thermostats.
780780
"""
781-
self._thermo_locs = self._match_and_rank_thermostats()
782-
for loc_id, loc_data in self._thermo_locs.items():
781+
self._match_and_rank_thermostats()
782+
for loc_id, loc_data in self._loc_data.items():
783783
if loc_data["primary_prio"] != 0:
784784
self._zones[loc_id] = {
785785
"dev_class": "climate",
@@ -798,65 +798,49 @@ def _match_and_rank_thermostats(self) -> dict[str, ThermoLoc]:
798798
799799
Match thermostat-appliances with locations, rank them for locations with multiple thermostats.
800800
"""
801-
matched_locations: dict[str, ThermoLoc] = {}
802801
thermo_matching: dict[str, int] = {
803802
"thermostat": 2,
804803
"zone_thermometer": 2,
805804
"zone_thermostat": 2,
806805
"thermostatic_radiator_valve": 1,
807806
}
808-
LOGGER.debug("HOI _loc_data: %s", self._loc_data)
809-
for location_id, location_details in self._loc_data.items():
810-
for entity in self.gw_entities.values():
811-
if entity["location"] == location_id:
812-
location_details.update(
813-
{"primary": [], "primary_prio": 0, "secondary": []}
814-
)
815-
matched_locations[location_id] = location_details
816-
817-
LOGGER.debug("HOI matched_locations before: %s", matched_locations)
818-
for loc_id in matched_locations:
807+
for location_id, location in self._loc_data.items():
808+
location.update(
809+
{"primary": [], "primary_prio": 0, "secondary": []}
810+
)
819811
for entity_id, entity in self.gw_entities.items():
820812
self._rank_thermostat(
821-
entity_id, entity, loc_id, matched_locations, thermo_matching
813+
entity_id, entity, location_id, location, thermo_matching
822814
)
823815

824-
LOGGER.debug("HOI matched_locations after: %s", matched_locations)
825-
return matched_locations
826-
827816
def _rank_thermostat(
828817
self,
829818
entity_id: str,
830819
entity: GwEntityData,
831-
loc_id: str,
832-
matched_locations: dict[str, ThermoLoc],
820+
location_id: str,
821+
location: dict[str, ThermoLoc],
833822
thermo_matching: dict[str, int],
834823
) -> None:
835824
"""Helper-function for _scan_thermostats().
836825
837826
Rank the thermostat based on entity-thermostat-type: primary or secondary.
838-
There can be one primary and several secondary thermostats per location.
827+
There can be several primary and secondary thermostats per location.
839828
"""
840829
appl_class = entity["dev_class"]
841-
appl_d_loc = entity["location"]
842-
thermo_loc = matched_locations[loc_id]
843-
if loc_id == appl_d_loc and appl_class in thermo_matching:
844-
if thermo_matching[appl_class] == thermo_loc["primary_prio"]:
845-
thermo_loc["primary"].append(entity_id)
830+
if location_id == entity["location"] and appl_class in thermo_matching:
846831
# Pre-elect new primary
847-
elif (thermo_rank := thermo_matching[appl_class]) > thermo_loc[
848-
"primary_prio"
849-
]:
850-
thermo_loc["primary_prio"] = thermo_rank
832+
if thermo_matching[appl_class] == location["primary_prio"]:
833+
location["primary"].append(entity_id)
834+
elif (thermo_rank := thermo_matching[appl_class]) > location["primary_prio"]:
835+
location["primary_prio"] = thermo_rank
851836
# Demote former primary
852-
if tl_primary := thermo_loc["primary"]:
853-
thermo_loc["secondary"] += tl_primary
854-
thermo_loc["primary"] = []
855-
837+
if tl_primary := location["primary"]:
838+
location["secondary"] += tl_primary
839+
location["primary"] = []
856840
# Crown primary
857-
thermo_loc["primary"].append(entity_id)
841+
location["primary"].append(entity_id)
858842
else:
859-
thermo_loc["secondary"].append(entity_id)
843+
location["secondary"].append(entity_id)
860844

861845
def _control_state(self, data: GwEntityData) -> str | bool:
862846
"""Helper-function for _get_location_data().

0 commit comments

Comments
 (0)