Skip to content

Commit 0412862

Browse files
authored
Merge pull request #375 from plugwise/reorder
Integrate creating platform-dicts and improve typing
2 parents 59ffe4c + 1fceb16 commit 0412862

File tree

10 files changed

+223
-223
lines changed

10 files changed

+223
-223
lines changed

.github/workflows/verify.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ jobs:
167167
needs: commitcheck
168168
strategy:
169169
matrix:
170-
python-version: ["3.11", "3.10", "3.9"]
170+
python-version: ["3.11", "3.10"]
171171
steps:
172172
- name: Check out committed code
173173
uses: actions/checkout@v3
@@ -206,7 +206,7 @@ jobs:
206206
needs: prepare-test-cache
207207
strategy:
208208
matrix:
209-
python-version: ["3.11", "3.10", "3.9"]
209+
python-version: ["3.11", "3.10"]
210210

211211
steps:
212212
- name: Check out committed code

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## v0.32.1 Improve typing, bugfix
4+
5+
- Integrate the process of creating binary_sensors, sensors, and switches dicts. Should make typing simpler.
6+
- Fix an apparent notification-bug for p1v4.
7+
- Improve typing: fix all type-ignores.
8+
- Clean up no longer used code.
9+
- Remove support for python3.9.
10+
311
## v0.32.0: New Feature: add support for changing the temperature offset on a supported thermostat device
412

513
- Add support for changing the temperature-offset on Jip, Lisa, Tom, Floor and on Anna (in some configurations)

fixtures/p1v4/all_data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"devices": {
33
"a455b61e52394b2db5081ce025a430f3": {
44
"binary_sensors": {
5-
"plugwise_notification": false
5+
"plugwise_notification": true
66
},
77
"dev_class": "gateway",
88
"firmware": "4.1.1",

plugwise/__init__.py

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"""
55
from __future__ import annotations
66

7+
from typing import cast
8+
79
import aiohttp
810
from defusedxml import ElementTree as etree
911

@@ -35,17 +37,26 @@
3537
ApplianceData,
3638
DeviceData,
3739
PlugwiseData,
38-
SmileBinarySensors,
39-
SmileSensors,
40-
SmileSwitches,
4140
)
4241
from .exceptions import (
4342
InvalidSetupError,
4443
PlugwiseError,
4544
ResponseError,
4645
UnsupportedDeviceError,
4746
)
48-
from .helper import SmileComm, SmileHelper, update_helper
47+
from .helper import SmileComm, SmileHelper
48+
49+
50+
def remove_empty_platform_dicts(data: DeviceData) -> DeviceData:
51+
"""Helper-function for removing any empty platform dicts."""
52+
if not data["binary_sensors"]:
53+
data.pop("binary_sensors")
54+
if not data["sensors"]:
55+
data.pop("sensors")
56+
if not data["switches"]:
57+
data.pop("switches")
58+
59+
return data
4960

5061

5162
class SmileData(SmileHelper):
@@ -82,18 +93,24 @@ def _all_device_data(self) -> None:
8293
Collect initial data for each device and add to self.gw_data and self.gw_devices.
8394
"""
8495
for device_id, device in self._appl_data.items():
85-
bs_dict: SmileBinarySensors = {}
86-
s_dict: SmileSensors = {}
87-
sw_dict: SmileSwitches = {}
96+
self.gw_devices.update({device_id: cast(DeviceData, device)})
97+
8898
data = self._get_device_data(device_id)
89-
self.gw_devices[device_id] = self._update_device_with_dicts(
90-
device_id, data, device, bs_dict, s_dict, sw_dict
91-
)
99+
# Add plugwise notification binary_sensor to the relevant gateway
100+
if device_id == self.gateway_id and (
101+
self._is_thermostat
102+
or (not self._smile_legacy and self.smile_type == "power")
103+
):
104+
data["binary_sensors"]["plugwise_notification"] = False
105+
106+
self.gw_devices[device_id].update(data)
92107

93108
# Update for cooling
94109
if self.gw_devices[device_id]["dev_class"] in ZONE_THERMOSTATS:
95110
self.update_for_cooling(self.gw_devices[device_id])
96111

112+
remove_empty_platform_dicts(self.gw_devices[device_id])
113+
97114
self.gw_data.update(
98115
{"smile_name": self.smile_name, "gateway_id": self.gateway_id}
99116
)
@@ -153,10 +170,10 @@ def _device_data_switching_group(
153170
counter = 0
154171
for member in details["members"]:
155172
member_data = self._get_appliance_data(member)
156-
if member_data.get("relay"):
173+
if member_data["switches"].get("relay"):
157174
counter += 1
158175

159-
device_data["relay"] = counter != 0
176+
device_data["switches"]["relay"] = counter != 0
160177

161178
return device_data
162179

@@ -174,7 +191,7 @@ def _device_data_adam(
174191
and self._on_off_device
175192
and self._heating_valves() is not None
176193
):
177-
device_data["heating_state"] = self._heating_valves() != 0
194+
device_data["binary_sensors"]["heating_state"] = self._heating_valves() != 0
178195

179196
return device_data
180197

@@ -275,7 +292,7 @@ def _get_device_data(self, dev_id: str) -> DeviceData:
275292
self._home_location, "outdoor_temperature"
276293
)
277294
if outdoor_temperature is not None:
278-
device_data["outdoor_temperature"] = outdoor_temperature
295+
device_data["sensors"]["outdoor_temperature"] = outdoor_temperature
279296

280297
# Show the allowed regulation modes
281298
if self._reg_allowed_modes:
@@ -519,31 +536,23 @@ async def async_update(self) -> PlugwiseData:
519536

520537
self.gw_data["notifications"] = self._notifications
521538

522-
for dev_id, dev_dict in self.gw_devices.items():
523-
data = self._get_device_data(dev_id)
524-
for key, value in data.items():
525-
if key in dev_dict:
526-
dev_dict[key] = value # type: ignore [literal-required]
527-
528-
for item in ("binary_sensors", "sensors", "switches"):
529-
notifs: dict[str, dict[str, str]] = {}
530-
if item == "binary_sensors":
531-
notifs = self._notifications
532-
if item in dev_dict:
533-
for key in data:
534-
update_helper(
535-
data,
536-
self.gw_devices,
537-
dev_dict,
538-
dev_id,
539-
item,
540-
key,
541-
notifs,
542-
)
539+
for device_id, device in self.gw_devices.items():
540+
data = self._get_device_data(device_id)
541+
if (
542+
"binary_sensors" in device
543+
and "plugwise_notification" in device["binary_sensors"]
544+
):
545+
data["binary_sensors"]["plugwise_notification"] = bool(
546+
self._notifications
547+
)
548+
549+
device.update(data)
543550

544551
# Update for cooling
545-
if dev_dict["dev_class"] in ZONE_THERMOSTATS:
546-
self.update_for_cooling(dev_dict)
552+
if device["dev_class"] in ZONE_THERMOSTATS:
553+
self.update_for_cooling(device)
554+
555+
remove_empty_platform_dicts(device)
547556

548557
return PlugwiseData(self.gw_data, self.gw_devices)
549558

plugwise/constants.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,22 @@
3838
"zone_thermometer",
3939
"zone_thermostat",
4040
)
41-
ACTIVE_ACTUATORS: Final[tuple[str, ...]] = (
41+
ActuatorType = Literal[
4242
"domestic_hot_water_setpoint",
4343
"max_dhw_temperature",
4444
"maximum_boiler_temperature",
4545
"temperature_offset",
4646
"thermostat",
47-
)
47+
]
48+
ACTIVE_ACTUATORS: Final[tuple[str, ...]] = get_args(ActuatorType)
49+
ActuatorDataType = Literal[
50+
"lower_bound",
51+
"resolution",
52+
"setpoint",
53+
"setpoint_high",
54+
"setpoint_low",
55+
"upper_bound",
56+
]
4857
DAYS: Final[dict[str, int]] = {
4958
"mo": 0,
5059
"tu": 1,
@@ -206,7 +215,6 @@
206215
"intended_central_heating_state": DATA(
207216
"heating_state", NONE
208217
), # This key shows in general the heating-behavior better than c-h_state. except when connected to a heatpump
209-
"maximum_boiler_temperature": UOM(TEMP_CELSIUS),
210218
"modulation_level": UOM(PERCENTAGE),
211219
"return_water_temperature": DATA("return_temperature", TEMP_CELSIUS),
212220
# Used with the Elga heatpump - marcelveldt
@@ -226,7 +234,11 @@
226234
"outdoor_temperature": DATA("outdoor_air_temperature", TEMP_CELSIUS),
227235
}
228236

229-
TOGGLES: Final[dict[str, str]] = {
237+
ToggleNameType = Literal[
238+
"cooling_ena_switch",
239+
"dhw_cm_switch",
240+
]
241+
TOGGLES: Final[dict[str, ToggleNameType]] = {
230242
"cooling_enabled": "cooling_ena_switch",
231243
"domestic_hot_water_comfort_mode": "dhw_cm_switch",
232244
}
@@ -248,6 +260,19 @@
248260

249261
# All available Binary Sensor, Sensor, and Switch Types
250262

263+
ApplianceType = Literal[
264+
"dev_class",
265+
"firmware",
266+
"hardware",
267+
"location",
268+
"mac_address",
269+
"members",
270+
"model",
271+
"name",
272+
"vendor",
273+
"zigbee_mac_address",
274+
]
275+
251276
BinarySensorType = Literal[
252277
"cooling_enabled",
253278
"compressor_state",
@@ -271,7 +296,6 @@
271296
"select_regulation_mode",
272297
"select_schedule",
273298
]
274-
275299
SelectOptionsType = Literal[
276300
"dhw_modes",
277301
"regulation_modes",

0 commit comments

Comments
 (0)