Skip to content

Commit f5645ef

Browse files
committed
Add more typing hints
1 parent b241633 commit f5645ef

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

plugwise/helper.py

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,21 +1051,21 @@ def _preset(self, loc_id):
10511051
if preset is not None:
10521052
return preset.text
10531053

1054-
def _schemas_legacy(self, avail, sched_temp, sel):
1054+
def _schemas_legacy(self, avail, sched_temp, sel) -> tuple[str, ...]:
10551055
"""Helper-function for _schemas().
10561056
Collect available schemas/schedules for the legacy thermostat.
10571057
"""
1058-
name = None
1059-
schemas = {}
1058+
name: str | None = None
1059+
schemas: dict[str] = {}
10601060

10611061
for schema in self._domain_objects.findall(".//rule"):
10621062
if rule_name := schema.find("name").text:
10631063
if "preset" not in rule_name:
10641064
name = rule_name
10651065

1066-
log_type = "schedule_state"
1067-
locator = f"appliance[type='thermostat']/logs/point_log[type='{log_type}']/period/measurement"
1068-
active = False
1066+
log_type: str = "schedule_state"
1067+
locator: str = f"appliance[type='thermostat']/logs/point_log[type='{log_type}']/period/measurement"
1068+
active: bool = False
10691069
if (result := self._domain_objects.find(locator)) is not None:
10701070
active = result.text == "on"
10711071

@@ -1077,16 +1077,16 @@ def _schemas_legacy(self, avail, sched_temp, sel):
10771077

10781078
return avail, sel, sched_temp, None
10791079

1080-
def _schemas(self, location):
1080+
def _schemas(self, location: str) -> tuple[str, ...]:
10811081
"""Helper-function for smile.py: _device_data_climate().
10821082
Obtain the available schemas/schedules. Adam: a schedule can be connected to more than one location.
10831083
NEW: when a location_id is present then the schedule is active. Valid for both Adam and non-legacy Anna.
10841084
"""
1085-
available = ["None"]
1086-
last_used = None
1087-
rule_ids = {}
1088-
schedule_temperature = None
1089-
selected = "None"
1085+
available: list[str] = ["None"]
1086+
last_used: str | None = None
1087+
rule_ids: dict[str] = {}
1088+
schedule_temperature: str | None = None
1089+
selected: str = "None"
10901090

10911091
# Legacy Anna schedule, only one schedule allowed
10921092
if self._smile_legacy:
@@ -1097,18 +1097,18 @@ def _schemas(self, location):
10971097
if location not in self._last_active:
10981098
self._last_active[location] = None
10991099

1100-
tag = "zone_preset_based_on_time_and_presence_with_override"
1100+
tag: str = "zone_preset_based_on_time_and_presence_with_override"
11011101
if not (rule_ids := self._rule_ids_by_tag(tag, location)):
11021102
return available, selected, schedule_temperature, None
11031103

1104-
schedules = {}
1104+
schedules: dict[str] = {}
11051105
for rule_id, loc_id in rule_ids.items():
1106-
name = self._domain_objects.find(f'rule[@id="{rule_id}"]/name').text
1107-
schedule = {}
1108-
locator = f'rule[@id="{rule_id}"]/directives'
1109-
directives = self._domain_objects.find(locator)
1106+
name: str = self._domain_objects.find(f'rule[@id="{rule_id}"]/name').text
1107+
schedule: dict[str] = {}
1108+
locator: str = f'rule[@id="{rule_id}"]/directives'
1109+
directives: etree | None = self._domain_objects.find(locator)
11101110
for directive in directives:
1111-
entry = directive.find("then").attrib
1111+
entry: str = directive.find("then").attrib
11121112
keys, dummy = zip(*entry.items())
11131113
if str(keys[0]) == "preset":
11141114
schedule[directive.attrib["time"]] = float(
@@ -1135,70 +1135,73 @@ def _schemas(self, location):
11351135

11361136
return available, selected, schedule_temperature, last_used
11371137

1138-
def _last_used_schedule(self, loc_id, rule_ids):
1138+
def _last_used_schedule(self, loc_id, rule_ids) -> str | None:
11391139
"""Helper-function for smile.py: _device_data_climate().
11401140
Determine the last-used schedule based on the location or the modified date.
11411141
"""
11421142
# First, find last_used == selected
1143-
last_used = self._last_active.get(loc_id)
1143+
last_used: str | None = self._last_active.get(loc_id)
11441144
if last_used is not None:
11451145
return last_used
11461146

11471147
# Alternatively, find last_used by finding the most recent modified_date
1148-
epoch = dt.datetime(1970, 1, 1, tzinfo=pytz.utc)
1149-
schemas = {}
1148+
epoch: dt.datetime = dt.datetime(1970, 1, 1, tzinfo=pytz.utc)
1149+
schemas: dict[str] | None = {}
11501150

11511151
if not rule_ids:
11521152
return # pragma: no cover
11531153

11541154
for rule_id, dummy in rule_ids.items():
1155-
schema_name = self._domain_objects.find(f'rule[@id="{rule_id}"]/name').text
1156-
schema_date = self._domain_objects.find(
1155+
schema_name: str = self._domain_objects.find(
1156+
f'rule[@id="{rule_id}"]/name'
1157+
).text
1158+
schema_date: str = self._domain_objects.find(
11571159
f'rule[@id="{rule_id}"]/modified_date'
11581160
).text
1159-
schema_time = parse(schema_date)
1161+
schema_time: dt.datetime = parse(schema_date)
11601162
schemas[schema_name] = (schema_time - epoch).total_seconds()
11611163

11621164
if schemas:
11631165
last_used = sorted(schemas.items(), key=lambda kv: kv[1])[-1][0]
11641166

11651167
return last_used
11661168

1167-
def _object_value(self, obj_id, measurement):
1169+
def _object_value(self, obj_id, measurement) -> float | int | None:
11681170
"""Helper-function for smile.py: _get_device_data() and _device_data_anna().
11691171
Obtain the value/state for the given object.
11701172
"""
1171-
search = self._domain_objects
1172-
locator = (
1173+
val: float | int | None = None
1174+
search: etree = self._domain_objects
1175+
locator: str = (
11731176
f'.//location[@id="{obj_id}"]/logs/point_log'
11741177
f'[type="{measurement}"]/period/measurement'
11751178
)
11761179
if search.find(locator) is not None:
11771180
val = format_measure(search.find(locator).text, None)
11781181
return val
11791182

1180-
return None
1183+
return val
11811184

1182-
def _get_lock_state(self, xml):
1185+
def _get_lock_state(self, xml) -> dict[str, Any]:
11831186
"""Helper-function for _get_appliance_data().
11841187
Adam & Stretches: obtain the relay-switch lock state.
11851188
"""
1186-
data = {}
1187-
actuator = "actuator_functionalities"
1188-
func_type = "relay_functionality"
1189+
data: dict[str, Any] = {}
1190+
actuator: str = "actuator_functionalities"
1191+
func_type: str = "relay_functionality"
11891192
if self.smile_type == "stretch" and self.smile_version[1].major == 2:
11901193
actuator = "actuators"
11911194
func_type = "relay"
1192-
appl_class = xml.find("type").text
1195+
appl_class: str = xml.find("type").text
11931196
if appl_class not in ["central_heating_pump", "valve_actuator"]:
1194-
locator = f".//{actuator}/{func_type}/lock"
1197+
locator: str = f".//{actuator}/{func_type}/lock"
11951198
if xml.find(locator) is not None:
1196-
measure = xml.find(locator).text
1199+
measure: str = xml.find(locator).text
11971200
data["lock"] = format_measure(measure, None)
11981201

11991202
return data
12001203

1201-
def _create_dicts_from_data(self, data, bs_dict, s_dict, sw_dict):
1204+
def _create_dicts_from_data(self, data, bs_dict, s_dict, sw_dict) -> None:
12021205
"""Helper-function for smile.py: _all_device_data().
12031206
Create dicts of binary_sensors, sensors, switches from the relevant data.
12041207
"""

0 commit comments

Comments
 (0)