Skip to content

Commit 6f3d772

Browse files
committed
Further optimize _match_and_rank_thermostats()
1 parent 2a0014d commit 6f3d772

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
@@ -776,8 +776,8 @@ def _scan_thermostats(self) -> None:
776776
Update locations with thermostat ranking results and use
777777
the result to update the device_class of secondary thermostats.
778778
"""
779-
self._thermo_locs = self._match_and_rank_thermostats()
780-
for loc_id, loc_data in self._thermo_locs.items():
779+
self._match_and_rank_thermostats()
780+
for loc_id, loc_data in self._loc_data.items():
781781
if loc_data["primary_prio"] != 0:
782782
self._zones[loc_id] = {
783783
"dev_class": "climate",
@@ -796,65 +796,49 @@ def _match_and_rank_thermostats(self) -> dict[str, ThermoLoc]:
796796
797797
Match thermostat-appliances with locations, rank them for locations with multiple thermostats.
798798
"""
799-
matched_locations: dict[str, ThermoLoc] = {}
800799
thermo_matching: dict[str, int] = {
801800
"thermostat": 2,
802801
"zone_thermometer": 2,
803802
"zone_thermostat": 2,
804803
"thermostatic_radiator_valve": 1,
805804
}
806-
LOGGER.debug("HOI _loc_data: %s", self._loc_data)
807-
for location_id, location_details in self._loc_data.items():
808-
for entity in self.gw_entities.values():
809-
if entity["location"] == location_id:
810-
location_details.update(
811-
{"primary": [], "primary_prio": 0, "secondary": []}
812-
)
813-
matched_locations[location_id] = location_details
814-
815-
LOGGER.debug("HOI matched_locations before: %s", matched_locations)
816-
for loc_id in matched_locations:
805+
for location_id, location in self._loc_data.items():
806+
location.update(
807+
{"primary": [], "primary_prio": 0, "secondary": []}
808+
)
817809
for entity_id, entity in self.gw_entities.items():
818810
self._rank_thermostat(
819-
entity_id, entity, loc_id, matched_locations, thermo_matching
811+
entity_id, entity, location_id, location, thermo_matching
820812
)
821813

822-
LOGGER.debug("HOI matched_locations after: %s", matched_locations)
823-
return matched_locations
824-
825814
def _rank_thermostat(
826815
self,
827816
entity_id: str,
828817
entity: GwEntityData,
829-
loc_id: str,
830-
matched_locations: dict[str, ThermoLoc],
818+
location_id: str,
819+
location: dict[str, ThermoLoc],
831820
thermo_matching: dict[str, int],
832821
) -> None:
833822
"""Helper-function for _scan_thermostats().
834823
835824
Rank the thermostat based on entity-thermostat-type: primary or secondary.
836-
There can be one primary and several secondary thermostats per location.
825+
There can be several primary and secondary thermostats per location.
837826
"""
838827
appl_class = entity["dev_class"]
839-
appl_d_loc = entity["location"]
840-
thermo_loc = matched_locations[loc_id]
841-
if loc_id == appl_d_loc and appl_class in thermo_matching:
842-
if thermo_matching[appl_class] == thermo_loc["primary_prio"]:
843-
thermo_loc["primary"].append(entity_id)
828+
if location_id == entity["location"] and appl_class in thermo_matching:
844829
# Pre-elect new primary
845-
elif (thermo_rank := thermo_matching[appl_class]) > thermo_loc[
846-
"primary_prio"
847-
]:
848-
thermo_loc["primary_prio"] = thermo_rank
830+
if thermo_matching[appl_class] == location["primary_prio"]:
831+
location["primary"].append(entity_id)
832+
elif (thermo_rank := thermo_matching[appl_class]) > location["primary_prio"]:
833+
location["primary_prio"] = thermo_rank
849834
# Demote former primary
850-
if tl_primary := thermo_loc["primary"]:
851-
thermo_loc["secondary"] += tl_primary
852-
thermo_loc["primary"] = []
853-
835+
if tl_primary := location["primary"]:
836+
location["secondary"] += tl_primary
837+
location["primary"] = []
854838
# Crown primary
855-
thermo_loc["primary"].append(entity_id)
839+
location["primary"].append(entity_id)
856840
else:
857-
thermo_loc["secondary"].append(entity_id)
841+
location["secondary"].append(entity_id)
858842

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

0 commit comments

Comments
 (0)