Skip to content

Commit 47ae5df

Browse files
committed
Add more typing hints
1 parent f5645ef commit 47ae5df

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

plugwise/helper.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -720,15 +720,17 @@ def _rule_ids_by_tag(self, tag, loc_id) -> dict[str]:
720720

721721
return schema_ids
722722

723-
def _appliance_measurements(self, appliance, data, measurements):
723+
def _appliance_measurements(self, appliance, data, measurements) -> dict[str, Any]:
724724
"""Helper-function for _get_appliance_data() - collect appliance measurement data."""
725725
for measurement, attrs in measurements:
726-
p_locator = f'.//logs/point_log[type="{measurement}"]/period/measurement'
726+
p_locator: str = (
727+
f'.//logs/point_log[type="{measurement}"]/period/measurement'
728+
)
727729
if appliance.find(p_locator) is not None:
728730
if self._smile_legacy and measurement == "domestic_hot_water_state":
729731
continue
730732

731-
measure = appliance.find(p_locator).text
733+
measure: str = appliance.find(p_locator).text
732734
# Fix for Adam + Anna: there is a pressure-measurement with an unrealistic value,
733735
# this measurement appears at power-on and is never updated, therefore remove.
734736
if (
@@ -750,9 +752,11 @@ def _appliance_measurements(self, appliance, data, measurements):
750752
if measurement == "outdoor_temperature":
751753
self._outdoor_temp = data[measurement]
752754

753-
i_locator = f'.//logs/interval_log[type="{measurement}"]/period/measurement'
755+
i_locator: str = (
756+
f'.//logs/interval_log[type="{measurement}"]/period/measurement'
757+
)
754758
if appliance.find(i_locator) is not None:
755-
name = f"{measurement}_interval"
759+
name: str = f"{measurement}_interval"
756760
measure = appliance.find(i_locator).text
757761
data[name] = format_measure(measure, ENERGY_WATT_HOUR)
758762

@@ -763,13 +767,13 @@ def _get_appliance_data(self, d_id) -> dict[str, Any]:
763767
Collect the appliance-data based on device id.
764768
Determined from APPLIANCES, for legacy from DOMAIN_OBJECTS.
765769
"""
766-
data = {}
770+
data: dict[str, Any] = {}
767771
# P1 legacy has no APPLIANCES, also not present in DOMAIN_OBJECTS
768772
if self._smile_legacy and self.smile_type == "power":
769773
return data
770774

771-
appliance = self._appliances.find(f'.//appliance[@id="{d_id}"]')
772-
measurements = DEVICE_MEASUREMENTS.items()
775+
appliance: etree | None = self._appliances.find(f'.//appliance[@id="{d_id}"]')
776+
measurements: dict[str, Any] = DEVICE_MEASUREMENTS.items()
773777
if self._opentherm_device or self._on_off_device:
774778
measurements = {
775779
**DEVICE_MEASUREMENTS,
@@ -810,10 +814,10 @@ def _get_appliance_data(self, d_id) -> dict[str, Any]:
810814

811815
def _rank_thermostat(
812816
self, thermo_matching, loc_id, appliance_id, appliance_details
813-
):
817+
) -> str:
814818
"""Helper-function for _scan_thermostats().
815819
Rank the thermostat based on appliance_details: master or slave."""
816-
appl_class = appliance_details["class"]
820+
appl_class: str = appliance_details["class"]
817821

818822
if (
819823
loc_id == appliance_details["location"]
@@ -838,20 +842,20 @@ def _rank_thermostat(
838842

839843
return appl_class
840844

841-
def _scan_thermostats(self, debug_text="missing text"):
845+
def _scan_thermostats(self, debug_text="missing text") -> None:
842846
"""Helper-function for smile.py: get_all_devices() and single_master_thermostat().
843847
Update locations with thermostat ranking results.
844848
"""
845849
self._thermo_locs = self._match_locations()
846850

847-
thermo_matching = {
851+
thermo_matching: dict[str, int] = {
848852
"thermostat": 3,
849853
"zone_thermometer": 2,
850854
"zone_thermostat": 2,
851855
"thermostatic_radiator_valve": 1,
852856
}
853857

854-
high_prio = 0
858+
high_prio: int = 0
855859
for loc_id, location_details in self._thermo_locs.items():
856860
self._thermo_locs[loc_id] = location_details
857861

@@ -875,55 +879,55 @@ def _scan_thermostats(self, debug_text="missing text"):
875879
if thermo_matching[appl_class] > high_prio:
876880
high_prio = thermo_matching[appl_class]
877881

878-
def _temperature_uri_legacy(self):
882+
def _temperature_uri_legacy(self) -> str:
879883
"""Helper-function for _temperature_uri().
880884
Determine the location-set_temperature uri - from APPLIANCES.
881885
"""
882-
locator = ".//appliance[type='thermostat']"
883-
appliance_id = self._appliances.find(locator).attrib["id"]
886+
locator: str = ".//appliance[type='thermostat']"
887+
appliance_id: str = self._appliances.find(locator).attrib["id"]
884888

885889
return f"{APPLIANCES};id={appliance_id}/thermostat"
886890

887-
def _temperature_uri(self, loc_id):
891+
def _temperature_uri(self, loc_id) -> str:
888892
"""Helper-function for smile.py: set_temperature().
889893
Determine the location-set_temperature uri - from LOCATIONS."""
890894
if self._smile_legacy:
891895
return self._temperature_uri_legacy()
892896

893-
locator = f'location[@id="{loc_id}"]/actuator_functionalities/thermostat_functionality'
894-
thermostat_functionality_id = self._locations.find(locator).attrib["id"]
897+
locator: str = f'location[@id="{loc_id}"]/actuator_functionalities/thermostat_functionality'
898+
thermostat_functionality_id: str = self._locations.find(locator).attrib["id"]
895899

896900
return f"{LOCATIONS};id={loc_id}/thermostat;id={thermostat_functionality_id}"
897901

898-
def _group_switches(self):
902+
def _group_switches(self) -> dict[str]:
899903
"""Helper-function for smile.py: get_all_devices().
900904
Collect switching- or pump-group info.
901905
"""
902-
switch_groups = {}
906+
switch_groups: dict[str] = {}
903907
# P1 and Anna don't have switchgroups
904908
if self.smile_type == "power" or self.smile_name == "Anna":
905909
return switch_groups
906910

907-
search = self._domain_objects
911+
search: etree = self._domain_objects
908912

909-
appliances = search.findall("./appliance")
910-
groups = search.findall("./group")
913+
appliances: etree = search.findall("./appliance")
914+
groups: etree = search.findall("./group")
911915

912916
for group in groups:
913-
group_appl = {}
914-
members = []
915-
group_id = group.attrib["id"]
916-
group_name = group.find("name").text
917-
group_type = group.find("type").text
917+
group_appl: dict[str] = {}
918+
members: list[str] = []
919+
group_id: str = group.attrib["id"]
920+
group_name: str = group.find("name").text
921+
group_type: str = group.find("type").text
918922
if self.smile_type == "stretch":
919-
group_appliance = group.findall("appliances/appliance")
923+
group_appliance: etree | None = group.findall("appliances/appliance")
920924
for dummy in group_appliance:
921925
members.append(dummy.attrib["id"])
922926
else:
923927
for appliance in appliances:
924928
if appliance.find("./groups/group") is not None:
925-
appl_id = appliance.attrib["id"]
926-
apl_gr_id = appliance.find("./groups/group").attrib["id"]
929+
appl_id: str = appliance.attrib["id"]
930+
apl_gr_id: str = appliance.find("./groups/group").attrib["id"]
927931
if apl_gr_id == group_id:
928932
members.append(appl_id)
929933

@@ -943,27 +947,27 @@ def _group_switches(self):
943947

944948
return switch_groups
945949

946-
def _heating_valves(self):
950+
def _heating_valves(self) -> int | None:
947951
"""Helper-function for smile.py: _device_data_adam().
948952
Collect amount of open valves indicating active direct heating.
949953
For cases where the heat is provided from an external shared source (city heating).
950954
"""
951-
loc_found = 0
952-
open_valve_count = 0
955+
loc_found: int = 0
956+
open_valve_count: int = 0
953957
for appliance in self._appliances.findall(".//appliance"):
954-
locator = './/logs/point_log[type="valve_position"]/period/measurement'
958+
locator: str = './/logs/point_log[type="valve_position"]/period/measurement'
955959
if appliance.find(locator) is not None:
956960
loc_found += 1
957-
measure = appliance.find(locator).text
961+
measure: str = appliance.find(locator).text
958962
if float(measure) > 0.0:
959963
open_valve_count += 1
960964

961965
return None if loc_found == 0 else open_valve_count
962966

963-
def _power_data_peak_value(self, loc):
967+
def _power_data_peak_value(self, loc) -> Munch:
964968
"""Helper-function for _power_data_from_location()."""
965969
loc.found = True
966-
no_tariffs = False
970+
no_tariffs: bool = False
967971

968972
# Only once try to find P1 Legacy values
969973
if loc.logs.find(loc.locator) is None and self.smile_type == "power":
@@ -984,15 +988,15 @@ def _power_data_peak_value(self, loc):
984988

985989
if (peak := loc.peak_select.split("_")[1]) == "offpeak":
986990
peak = "off_peak"
987-
log_found = loc.log_type.split("_")[0]
991+
log_found: str = loc.log_type.split("_")[0]
988992
loc.key_string = f"{loc.measurement}_{peak}_{log_found}"
989993
# P1 with fw 2.x does not have tariff indicators for point_log values
990994
if no_tariffs:
991995
loc.key_string = f"{loc.measurement}_{log_found}"
992996
if "gas" in loc.measurement:
993997
loc.key_string = f"{loc.measurement}_{log_found}"
994998
loc.net_string = f"net_electricity_{log_found}"
995-
val = loc.logs.find(loc.locator).text
999+
val: str = loc.logs.find(loc.locator).text
9961000
loc.f_val = power_data_local_format(loc.attrs, loc.key_string, val)
9971001

9981002
return loc
@@ -1001,16 +1005,16 @@ def _power_data_from_location(self, loc_id) -> dict[str, Any] | None:
10011005
"""Helper-function for smile.py: _get_device_data().
10021006
Collect the power-data based on Location ID.
10031007
"""
1004-
direct_data = {}
1008+
direct_data: dict[str, any] = {}
10051009
loc: Munch = Munch()
10061010

10071011
if self.smile_type != "power":
10081012
return
10091013

1010-
search = self._locations
1011-
log_list = ["point_log", "cumulative_log", "interval_log"]
1012-
peak_list = ["nl_peak", "nl_offpeak"]
1013-
t_string = "tariff"
1014+
search: etree = self._locations
1015+
log_list: list[str] = ["point_log", "cumulative_log", "interval_log"]
1016+
peak_list: list[str] = ["nl_peak", "nl_offpeak"]
1017+
t_string: str = "tariff"
10141018
if self._smile_legacy:
10151019
t_string = "tariff_indicator"
10161020

@@ -1034,20 +1038,20 @@ def _power_data_from_location(self, loc_id) -> dict[str, Any] | None:
10341038

10351039
return direct_data
10361040

1037-
def _preset(self, loc_id):
1041+
def _preset(self, loc_id) -> str | None:
10381042
"""Helper-function for smile.py: device_data_climate().
10391043
Collect the active preset based on Location ID.
10401044
"""
10411045
if self._smile_legacy:
1042-
active_rule = self._domain_objects.find(
1046+
active_rule: etree | None = self._domain_objects.find(
10431047
"rule[active='true']/directives/when/then"
10441048
)
10451049
if active_rule is None or "icon" not in active_rule.keys():
10461050
return
10471051
return active_rule.attrib["icon"]
10481052

1049-
locator = f'.//location[@id="{loc_id}"]/preset'
1050-
preset = self._domain_objects.find(locator)
1053+
locator: str = f'.//location[@id="{loc_id}"]/preset'
1054+
preset: etree | None = self._domain_objects.find(locator)
10511055
if preset is not None:
10521056
return preset.text
10531057

0 commit comments

Comments
 (0)