Skip to content

Commit 5311198

Browse files
committed
Rework _scan_thermostat() and related
1 parent 6c71928 commit 5311198

File tree

2 files changed

+51
-54
lines changed

2 files changed

+51
-54
lines changed

plugwise/helper.py

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,19 @@ def _get_locations(self) -> None:
199199
loc = Munch()
200200
locations = self._domain_objects.findall("./location")
201201
for location in locations:
202-
loc.name = location.find("name").text
203202
loc.loc_id = location.attrib["id"]
204-
self._loc_data[loc.loc_id] = {"name": loc.name}
205-
if loc.name != "Home":
206-
continue
207-
208-
self._home_loc_id = loc.loc_id
209-
self._home_location = self._domain_objects.find(
210-
f"./location[@id='{loc.loc_id}']"
211-
)
203+
loc.name = location.find("name").text
204+
self._loc_data[loc.loc_id] = {
205+
"name": loc.name,
206+
"primary": [],
207+
"primary_prio": 0,
208+
"secondary": [],
209+
}
210+
if loc.name == "Home":
211+
self._home_loc_id = loc.loc_id
212+
self._home_location = self._domain_objects.find(
213+
f"./location[@id='{loc.loc_id}']"
214+
)
212215

213216
def _appliance_info_finder(self, appl: Munch, appliance: etree.Element) -> Munch:
214217
"""Collect info for all appliances found."""
@@ -739,76 +742,72 @@ def _cleanup_data(self, data: GwEntityData) -> None:
739742
def _scan_thermostats(self) -> None:
740743
"""Helper-function for smile.py: get_all_entities().
741744
742-
Update locations with thermostat ranking results and use
745+
Adam only: update locations with thermostat ranking results and use
743746
the result to update the device_class of secondary thermostats.
744747
"""
745-
self._thermo_locs = self._match_locations()
746-
for loc_id in self._thermo_locs:
747-
for entity_id, entity in self.gw_entities.items():
748-
self._rank_thermostat(THERMO_MATCHING, loc_id, entity_id, entity)
748+
if not self.check_name(ADAM):
749+
return
749750

750-
for loc_id, loc_data in self._thermo_locs.items():
751-
if loc_data["primary_prio"] != 0:
752-
self._zones[loc_id] = {
751+
self._match_and_rank_thermostats()
752+
for location_id, location in self._loc_data.items():
753+
if location["primary_prio"] != 0:
754+
self._zones[location_id] = {
753755
"dev_class": "climate",
754756
"model": "ThermoZone",
755-
"name": loc_data["name"],
757+
"name": location["name"],
756758
"thermostats": {
757-
"primary": loc_data["primary"],
758-
"secondary": loc_data["secondary"],
759+
"primary": location["primary"],
760+
"secondary": location["secondary"],
759761
},
760762
"vendor": "Plugwise",
761763
}
762764
self._count += 5
763765

764-
def _match_locations(self) -> dict[str, ThermoLoc]:
766+
def _match_and_rank_thermostats(self) -> None:
765767
"""Helper-function for _scan_thermostats().
766768
767-
Match appliances with locations.
769+
Match thermostat-appliances with locations, rank them for locations with multiple thermostats.
768770
"""
769-
matched_locations: dict[str, ThermoLoc] = {}
770-
for location_id, location_details in self._loc_data.items():
771-
for appliance_details in self.gw_entities.values():
772-
if appliance_details["location"] == location_id:
773-
location_details.update(
774-
{"primary": [], "primary_prio": 0, "secondary": []}
775-
)
776-
matched_locations[location_id] = location_details
777-
778-
return matched_locations
771+
for location_id, location in self._loc_data.items():
772+
for entity_id, entity in self.gw_entities.items():
773+
self._rank_thermostat(
774+
entity_id, entity, location_id, location, THERMO_MATCHING
775+
)
779776

780777
def _rank_thermostat(
781778
self,
779+
entity_id: str,
780+
entity: GwEntityData,
781+
location_id: str,
782+
location: ThermoLoc,
782783
thermo_matching: dict[str, int],
783-
loc_id: str,
784-
appliance_id: str,
785-
appliance_details: GwEntityData,
786784
) -> None:
787785
"""Helper-function for _scan_thermostats().
788786
789-
Rank the thermostat based on appliance_details: primary or secondary.
790-
Note: there can be several primary and secondary thermostats.
787+
Rank the thermostat based on entity-thermostat-type: primary or secondary.
788+
There can be several primary and secondary thermostats per location.
791789
"""
792-
appl_class = appliance_details["dev_class"]
793-
appl_d_loc = appliance_details["location"]
794-
thermo_loc = self._thermo_locs[loc_id]
795-
if loc_id == appl_d_loc and appl_class in thermo_matching:
796-
if thermo_matching[appl_class] == thermo_loc["primary_prio"]:
797-
thermo_loc["primary"].append(appliance_id)
790+
appl_class = entity["dev_class"]
791+
if (
792+
"location" in entity
793+
and location_id == entity["location"]
794+
and appl_class in thermo_matching
795+
):
798796
# Pre-elect new primary
799-
elif (thermo_rank := thermo_matching[appl_class]) > thermo_loc[
797+
if thermo_matching[appl_class] == location["primary_prio"]:
798+
location["primary"].append(entity_id)
799+
elif (thermo_rank := thermo_matching[appl_class]) > location[
800800
"primary_prio"
801801
]:
802-
thermo_loc["primary_prio"] = thermo_rank
802+
location["primary_prio"] = thermo_rank
803803
# Demote former primary
804-
if tl_primary := thermo_loc["primary"]:
805-
thermo_loc["secondary"] += tl_primary
806-
thermo_loc["primary"] = []
807-
804+
if tl_primary := location["primary"]:
805+
location["secondary"] += tl_primary
806+
location["primary"] = []
808807
# Crown primary
809-
thermo_loc["primary"].append(appliance_id)
808+
location["primary"].append(entity_id)
810809
else:
811-
thermo_loc["secondary"].append(appliance_id)
810+
location["secondary"].append(entity_id)
812811

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

plugwise/smile.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from typing import Any, cast
1111

1212
from plugwise.constants import (
13-
ADAM,
1413
ALLOWED_ZONE_PROFILES,
1514
ANNA,
1615
APPLIANCES,
@@ -112,8 +111,7 @@ def get_all_gateway_entities(self) -> None:
112111
self.therms_with_offset_func = (
113112
self._get_appliances_with_offset_functionality()
114113
)
115-
if self.check_name(ADAM):
116-
self._scan_thermostats()
114+
self._scan_thermostats()
117115

118116
if group_data := self._get_groups():
119117
self.gw_entities.update(group_data)

0 commit comments

Comments
 (0)