Skip to content

Commit fb221b1

Browse files
committed
More typing added, small improvements
1 parent 5fe6eb2 commit fb221b1

File tree

1 file changed

+80
-72
lines changed

1 file changed

+80
-72
lines changed

plugwise/smile.py

Lines changed: 80 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@
3737
class SmileData(SmileHelper):
3838
"""The Plugwise Smile main class."""
3939

40-
def _append_special(self, data, d_id, bs_dict, s_dict):
40+
def _append_special(self, d_id, bs_dict, s_dict) -> None:
4141
"""Helper-function for smile.py: _all_device_data().
4242
When conditions are met, the plugwise_notification binary_sensor is appended.
4343
"""
4444
if d_id == self.gateway_id:
4545
if self._is_thermostat:
4646
bs_dict.update(PW_NOTIFICATION)
4747

48-
def _all_device_data(self):
48+
def _all_device_data(self) -> None:
4949
"""Helper-function for get_all_devices().
5050
Collect initial data for each device and add to self.gw_data and self.gw_devices.
5151
"""
5252
for dev_id, dev_dict in self._devices.items():
53-
dev_and_data = dev_dict
54-
temp_bs_dict = {}
55-
temp_s_dict = {}
56-
temp_sw_dict = {}
57-
data = self._get_device_data(dev_id)
53+
dev_and_data: dict[str, Any] = dev_dict
54+
temp_bs_dict: dict[str, bool] = {}
55+
temp_s_dict: dict[str, Any] = {}
56+
temp_sw_dict: dict[str, str] = {}
57+
data: dict[str, Any] = self._get_device_data(dev_id)
5858

5959
self._create_dicts_from_data(data, temp_bs_dict, temp_s_dict, temp_sw_dict)
60-
self._append_special(data, dev_id, temp_bs_dict, temp_s_dict)
60+
self._append_special(dev_id, temp_bs_dict, temp_s_dict)
6161

6262
dev_and_data.update(data)
6363
if temp_bs_dict:
@@ -78,14 +78,14 @@ def _all_device_data(self):
7878
)
7979
self.gw_data["smile_name"] = self.smile_name
8080

81-
def get_all_devices(self):
81+
def get_all_devices(self) -> None:
8282
"""Determine the devices present from the obtained XML-data."""
83-
self._devices = {}
83+
self._devices: dict[str, Any] = {}
8484
self._scan_thermostats()
8585
self.single_master_thermostat()
8686

8787
for appliance, details in self._appl_data.items():
88-
loc_id = details["location"]
88+
loc_id: str = details["location"]
8989
# Don't assign the _home_location to thermostat-devices without a location, they are not active
9090
if loc_id is None and details["class"] not in THERMOSTAT_CLASSES:
9191
details["location"] = self._home_location
@@ -106,14 +106,14 @@ def get_all_devices(self):
106106
# Collect data for each device via helper function
107107
self._all_device_data()
108108

109-
def _device_data_switching_group(self, details, device_data):
109+
def _device_data_switching_group(self, details, device_data) -> dict[str, Any]:
110110
"""Helper-function for _get_device_data().
111111
Determine switching group device data.
112112
"""
113113
if details["class"] in SWITCH_GROUP_TYPES:
114-
counter = 0
114+
counter: int = 0
115115
for member in details["members"]:
116-
appl_data = self._get_appliance_data(member)
116+
appl_data: dict[str, Any] = self._get_appliance_data(member)
117117
if appl_data["relay"]:
118118
counter += 1
119119

@@ -123,7 +123,7 @@ def _device_data_switching_group(self, details, device_data):
123123

124124
return device_data
125125

126-
def _device_data_adam(self, details, device_data):
126+
def _device_data_adam(self, details, device_data) -> dict[str, Any]:
127127
"""Helper-function for _get_device_data().
128128
Determine Adam device data.
129129
"""
@@ -137,16 +137,16 @@ def _device_data_adam(self, details, device_data):
137137

138138
return device_data
139139

140-
def _device_data_climate(self, details, device_data):
140+
def _device_data_climate(self, details, device_data) -> dict[str, Any]:
141141
"""Helper-function for _get_device_data().
142142
Determine climate-control device data.
143143
"""
144-
loc_id = details["location"]
144+
loc_id: str = details["location"]
145145

146146
# Presets
147147
device_data["preset_modes"] = None
148148
device_data["active_preset"] = None
149-
presets = self._presets(loc_id)
149+
presets: dict[str, Any] = self._presets(loc_id)
150150
if presets:
151151
device_data["presets"] = presets
152152
device_data["preset_modes"] = list(presets)
@@ -165,7 +165,7 @@ def _device_data_climate(self, details, device_data):
165165

166166
# Operation mode: auto, heat, cool
167167
device_data["mode"] = "auto"
168-
schedule_status = False
168+
schedule_status: bool = False
169169
if sel_schema != "None":
170170
schedule_status = True
171171
if not schedule_status:
@@ -180,27 +180,29 @@ def _device_data_climate(self, details, device_data):
180180

181181
return device_data
182182

183-
def _get_device_data(self, dev_id):
183+
def _get_device_data(self, dev_id) -> dict[str, Any]:
184184
"""Helper-function for _all_device_data() and async_update().
185185
Provide device-data, based on Location ID (= dev_id), from APPLIANCES.
186186
"""
187-
devices = self._devices
188-
details = devices.get(dev_id)
189-
device_data = self._get_appliance_data(dev_id)
187+
devices: dict[str, Any] = self._devices
188+
details: dict[str, Any] = devices.get(dev_id)
189+
device_data: dict[str, Any] = self._get_appliance_data(dev_id)
190190

191191
# Generic
192192
if details["class"] == "gateway" or dev_id == self.gateway_id:
193193
# Adam & Anna: the Smile outdoor_temperature is present in DOMAIN_OBJECTS and LOCATIONS - under Home
194194
# The outdoor_temperature present in APPLIANCES is a local sensor connected to the active device
195195
if self.smile_type == "thermostat":
196-
outdoor_temperature = self._object_value(
196+
outdoor_temperature: str = self._object_value(
197197
self._home_location, "outdoor_temperature"
198198
)
199199
if outdoor_temperature is not None:
200200
device_data["outdoor_temperature"] = outdoor_temperature
201201

202202
# Get P1 data from LOCATIONS
203-
power_data = self._power_data_from_location(details["location"])
203+
power_data: dict[str, Any] = self._power_data_from_location(
204+
details["location"]
205+
)
204206
if power_data is not None:
205207
device_data.update(power_data)
206208

@@ -222,11 +224,11 @@ def _get_device_data(self, dev_id):
222224

223225
return device_data
224226

225-
def single_master_thermostat(self):
227+
def single_master_thermostat(self) -> None:
226228
"""Determine if there is a single master thermostat in the setup."""
227229
if self.smile_type == "thermostat":
228230
self._is_thermostat = True
229-
count = 0
231+
count: int = 0
230232
for dummy, data in self._thermo_locs.items():
231233
if "master_prio" in data:
232234
if data.get("master_prio") > 0:
@@ -465,26 +467,26 @@ async def async_update(self) -> Tuple[dict, dict]:
465467

466468
return [self.gw_data, self.gw_devices]
467469

468-
async def _set_schedule_state_legacy(self, name, status):
470+
async def _set_schedule_state_legacy(self, name: str, status: str) -> bool:
469471
"""Helper-function for set_schedule_state()."""
470-
schema_rule_id = None
472+
schema_rule_id: str = None
471473
for rule in self._domain_objects.findall("rule"):
472474
if rule.find("name").text == name:
473475
schema_rule_id = rule.attrib["id"]
474476

475477
if schema_rule_id is None:
476478
return False
477479

478-
template_id = None
479-
state = "false"
480-
if str(status) == "on":
480+
template_id: str = None
481+
state: str = "false"
482+
if status == "on":
481483
state = "true"
482-
locator = f'.//*[@id="{schema_rule_id}"]/template'
484+
locator: etree = f'.//*[@id="{schema_rule_id}"]/template'
483485
for rule in self._domain_objects.findall(locator):
484486
template_id = rule.attrib["id"]
485487

486-
uri = f"{RULES};id={schema_rule_id}"
487-
data = (
488+
uri: str = f"{RULES};id={schema_rule_id}"
489+
data: str = (
488490
"<rules><rule"
489491
f' id="{schema_rule_id}"><name><![CDATA[{name}]]></name><template'
490492
f' id="{template_id}" /><active>{state}</active></rule></rules>'
@@ -493,32 +495,32 @@ async def _set_schedule_state_legacy(self, name, status):
493495
await self._request(uri, method="put", data=data)
494496
return True
495497

496-
async def set_schedule_state(self, loc_id, name, state):
498+
async def set_schedule_state(self, loc_id: str, name: str, state: str) -> bool:
497499
"""Set the Schedule, with the given name, on the relevant Thermostat.
498500
Determined from - DOMAIN_OBJECTS.
499501
"""
500502
if self._smile_legacy:
501503
return await self._set_schedule_state_legacy(name, state)
502504

503-
schema_rule = self._rule_ids_by_name(str(name), loc_id)
505+
schema_rule = self._rule_ids_by_name(name, loc_id)
504506
if not schema_rule or schema_rule is None:
505507
return False
506508

507-
schema_rule_id = next(iter(schema_rule))
508-
info = ""
509+
schema_rule_id: str = next(iter(schema_rule))
510+
info: str = ""
509511
if state == "on":
510512
info = f'<context><zone><location id="{loc_id}" /></zone></context>'
511513

512-
template = (
514+
template: str = (
513515
'<template tag="zone_preset_based_on_time_and_presence_with_override" />'
514516
)
515517
if self.smile_name != "Adam":
516-
locator = f'.//*[@id="{schema_rule_id}"]/template'
518+
locator: str = f'.//*[@id="{schema_rule_id}"]/template'
517519
template_id = self._domain_objects.find(locator).attrib["id"]
518-
template = f'<template id="{template_id}" />'
520+
template: str = f'<template id="{template_id}" />'
519521

520-
uri = f"{RULES};id={schema_rule_id}"
521-
data = (
522+
uri: str = f"{RULES};id={schema_rule_id}"
523+
data: str = (
522524
f'<rules><rule id="{schema_rule_id}"><name><![CDATA[{name}]]></name>'
523525
f"{template}<contexts>{info}</contexts></rule></rules>"
524526
)
@@ -527,20 +529,20 @@ async def set_schedule_state(self, loc_id, name, state):
527529

528530
return True
529531

530-
async def set_preset(self, loc_id, preset):
532+
async def set_preset(self, loc_id: str, preset: str) -> bool:
531533
"""Set the given Preset on the relevant Thermostat - from LOCATIONS."""
532534
if self._smile_legacy:
533535
return await self._set_preset_legacy(preset)
534536

535-
current_location = self._locations.find(f'location[@id="{loc_id}"]')
536-
location_name = current_location.find("name").text
537-
location_type = current_location.find("type").text
537+
current_location: etree = self._locations.find(f'location[@id="{loc_id}"]')
538+
location_name: str = current_location.find("name").text
539+
location_type: str = current_location.find("type").text
538540

539541
if preset not in self._presets(loc_id):
540542
return False
541543

542-
uri = f"{LOCATIONS};id={loc_id}"
543-
data = (
544+
uri: str = f"{LOCATIONS};id={loc_id}"
545+
data: str = (
544546
"<locations><location"
545547
f' id="{loc_id}"><name>{location_name}</name><type>{location_type}'
546548
f"</type><preset>{preset}</preset></location></locations>"
@@ -549,36 +551,40 @@ async def set_preset(self, loc_id, preset):
549551
await self._request(uri, method="put", data=data)
550552
return True
551553

552-
async def set_temperature(self, loc_id, temperature):
554+
async def set_temperature(self, loc_id: str, temperature: str) -> bool:
553555
"""Set the given Temperature on the relevant Thermostat."""
554-
temperature = str(temperature)
555-
uri = self._temperature_uri(loc_id)
556-
data = (
556+
uri: str = self._temperature_uri(loc_id)
557+
data: str = (
557558
"<thermostat_functionality><setpoint>"
558559
f"{temperature}</setpoint></thermostat_functionality>"
559560
)
560561

561562
await self._request(uri, method="put", data=data)
562563
return True
563564

564-
async def _set_groupswitch_member_state(self, members, state, switch):
565+
async def _set_groupswitch_member_state(
566+
self, members: dict[str], state: str, switch: str
567+
) -> bool:
565568
"""Helper-function for set_switch_state() .
566569
Set the given State of the relevant Switch within a group of members.
567570
"""
568571
for member in members:
569-
locator = f'appliance[@id="{member}"]/{switch.actuator}/{switch.func_type}'
570-
switch_id = self._appliances.find(locator).attrib["id"]
571-
uri = f"{APPLIANCES};id={member}/{switch.device};id={switch_id}"
572+
locator: str = (
573+
f'appliance[@id="{member}"]/{switch.actuator}/{switch.func_type}'
574+
)
575+
switch_id: str = self._appliances.find(locator).attrib["id"]
576+
uri: str = f"{APPLIANCES};id={member}/{switch.device};id={switch_id}"
572577
if self._stretch_v2:
573578
uri = f"{APPLIANCES};id={member}/{switch.device}"
574-
state = str(state)
575-
data = f"<{switch.func_type}><{switch.func}>{state}</{switch.func}></{switch.func_type}>"
579+
data: str = f"<{switch.func_type}><{switch.func}>{state}</{switch.func}></{switch.func_type}>"
576580

577581
await self._request(uri, method="put", data=data)
578582

579583
return True
580584

581-
async def set_switch_state(self, appl_id, members, model, state):
585+
async def set_switch_state(
586+
self, appl_id: str, members: dict[str], model: str, state: str
587+
) -> bool:
582588
"""Set the given State of the relevant Switch."""
583589
switch = Munch()
584590
switch.actuator = "actuator_functionalities"
@@ -600,40 +606,42 @@ async def set_switch_state(self, appl_id, members, model, state):
600606
if members is not None:
601607
return await self._set_groupswitch_member_state(members, state, switch)
602608

603-
locator = f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}'
604-
switch_id = self._appliances.find(locator).attrib["id"]
605-
uri = f"{APPLIANCES};id={appl_id}/{switch.device};id={switch_id}"
609+
locator: str = (
610+
f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}'
611+
)
612+
switch_id: str = self._appliances.find(locator).attrib["id"]
613+
uri: str = f"{APPLIANCES};id={appl_id}/{switch.device};id={switch_id}"
606614
if self._stretch_v2:
607615
uri = f"{APPLIANCES};id={appl_id}/{switch.device}"
608-
data = f"<{switch.func_type}><{switch.func}>{state}</{switch.func}></{switch.func_type}>"
616+
data: str = f"<{switch.func_type}><{switch.func}>{state}</{switch.func}></{switch.func_type}>"
609617

610618
if model == "relay":
611619
locator = (
612620
f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}/lock'
613621
)
614-
lock_state = self._appliances.find(locator).text
622+
lock_state: str = self._appliances.find(locator).text
615623
# Don't bother switching a relay when the corresponding lock-state is true
616624
if lock_state == "true":
617625
return False
618626

619627
await self._request(uri, method="put", data=data)
620628
return True
621629

622-
async def _set_preset_legacy(self, preset):
630+
async def _set_preset_legacy(self, preset: str) -> bool:
623631
"""Set the given Preset on the relevant Thermostat - from DOMAIN_OBJECTS."""
624-
locator = f'rule/directives/when/then[@icon="{preset}"].../.../...'
632+
locator: str = f'rule/directives/when/then[@icon="{preset}"].../.../...'
625633
if (rule := self._domain_objects.find(locator)) is None:
626634
return False
627635

628-
uri = f"{RULES}"
629-
data = f'<rules><rule id="{rule.attrib["id"]}"><active>true</active></rule></rules>'
636+
uri: str = f"{RULES}"
637+
data: str = f'<rules><rule id="{rule.attrib["id"]}"><active>true</active></rule></rules>'
630638

631639
await self._request(uri, method="put", data=data)
632640
return True
633641

634-
async def delete_notification(self):
642+
async def delete_notification(self) -> bool:
635643
"""Delete the active Plugwise Notification."""
636-
uri = f"{NOTIFICATIONS}"
644+
uri: str = f"{NOTIFICATIONS}"
637645

638646
await self._request(uri, method="delete")
639647
return True

0 commit comments

Comments
 (0)