Skip to content

Commit 21b2efc

Browse files
committed
Improve code-placement, simplify argument names
1 parent e50e85b commit 21b2efc

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

plugwise/helper.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -778,19 +778,7 @@ 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_locations()
782-
783-
thermo_matching: dict[str, int] = {
784-
"thermostat": 2,
785-
"zone_thermometer": 2,
786-
"zone_thermostat": 2,
787-
"thermostatic_radiator_valve": 1,
788-
}
789-
790-
for loc_id in self._thermo_locs:
791-
for entity_id, entity in self.gw_entities.items():
792-
self._rank_thermostat(thermo_matching, loc_id, entity_id, entity)
793-
781+
self._thermo_locs = self._match_and_rank_thermostats()
794782
for loc_id, loc_data in self._thermo_locs.items():
795783
if loc_data["primary_prio"] != 0:
796784
self._zones[loc_id] = {
@@ -805,40 +793,52 @@ def _scan_thermostats(self) -> None:
805793
}
806794
self._count += 5
807795

808-
def _match_locations(self) -> dict[str, ThermoLoc]:
796+
def _match_and_rank_thermostats(self) -> dict[str, ThermoLoc]:
809797
"""Helper-function for _scan_thermostats().
810798
811-
Match appliances with locations.
799+
Match thermostat-appliances with locations, rank them for locations with multiple thermostats.
812800
"""
813801
matched_locations: dict[str, ThermoLoc] = {}
802+
thermo_matching: dict[str, int] = {
803+
"thermostat": 2,
804+
"zone_thermometer": 2,
805+
"zone_thermostat": 2,
806+
"thermostatic_radiator_valve": 1,
807+
}
814808
for location_id, location_details in self._loc_data.items():
815-
for appliance_details in self.gw_entities.values():
816-
if appliance_details["location"] == location_id:
809+
# for gw_entity_details in self.gw_entities.values():
810+
for entity_id, entity in self.gw_entities.items():
811+
if entity["location"] == location_id:
817812
location_details.update(
818813
{"primary": [], "primary_prio": 0, "secondary": []}
819814
)
820815
matched_locations[location_id] = location_details
821816

817+
for loc_id in matched_locations:
818+
for entity_id, entity in self.gw_entities.items():
819+
self._rank_thermostat(entity_id, entity, loc_id, matched_locations, thermo_matching)
820+
822821
return matched_locations
823822

824823
def _rank_thermostat(
825824
self,
826-
thermo_matching: dict[str, int],
825+
entity_id: str,
826+
entity: GwEntityData,
827827
loc_id: str,
828-
appliance_id: str,
829-
appliance_details: GwEntityData,
828+
matched_locations: dict[str, ThermoLoc],
829+
thermo_matching: dict[str, int],
830830
) -> None:
831831
"""Helper-function for _scan_thermostats().
832832
833-
Rank the thermostat based on appliance_details: primary or secondary.
834-
Note: there can be several primary and secondary thermostats.
833+
Rank the thermostat based on entity-thermostat-type: primary or secondary.
834+
There can be one primary and several secondary thermostats per location.
835835
"""
836-
appl_class = appliance_details["dev_class"]
837-
appl_d_loc = appliance_details["location"]
838-
thermo_loc = self._thermo_locs[loc_id]
836+
appl_class = entity["dev_class"]
837+
appl_d_loc = entity["location"]
838+
thermo_loc = matched_locations[loc_id]
839839
if loc_id == appl_d_loc and appl_class in thermo_matching:
840840
if thermo_matching[appl_class] == thermo_loc["primary_prio"]:
841-
thermo_loc["primary"].append(appliance_id)
841+
thermo_loc["primary"].append(entity_id)
842842
# Pre-elect new primary
843843
elif (thermo_rank := thermo_matching[appl_class]) > thermo_loc[
844844
"primary_prio"
@@ -850,9 +850,9 @@ def _rank_thermostat(
850850
thermo_loc["primary"] = []
851851

852852
# Crown primary
853-
thermo_loc["primary"].append(appliance_id)
853+
thermo_loc["primary"].append(entity_id)
854854
else:
855-
thermo_loc["secondary"].append(appliance_id)
855+
thermo_loc["secondary"].append(entity_id)
856856

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

0 commit comments

Comments
 (0)