Skip to content

Commit 0ef08d3

Browse files
authored
Merge pull request #380 from plugwise/improve-typing-2
Optimize and rearrange typing-related constants, implement and cleanup.
2 parents 00b9297 + d42ca44 commit 0ef08d3

File tree

4 files changed

+105
-106
lines changed

4 files changed

+105
-106
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Ongoing
44

55
- Extend p1v4_442_triple userdata to include a Plugwise notification, extending the related fixture which is used in PW-beta and Core Plugwise.
6+
- Optimize and rearrange typing-related constants, implement and cleanup.
67

78
## v0.32.1 Improve typing, bugfix
89

plugwise/__init__.py

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

7-
from typing import cast
8-
97
import aiohttp
108
from defusedxml import ElementTree as etree
119

@@ -34,7 +32,6 @@
3432
SYSTEM,
3533
ZONE_THERMOSTATS,
3634
ActuatorData,
37-
ApplianceData,
3835
DeviceData,
3936
PlugwiseData,
4037
)
@@ -93,7 +90,7 @@ def _all_device_data(self) -> None:
9390
Collect initial data for each device and add to self.gw_data and self.gw_devices.
9491
"""
9592
for device_id, device in self._appl_data.items():
96-
self.gw_devices.update({device_id: cast(DeviceData, device)})
93+
self.gw_devices.update({device_id: device})
9794

9895
data = self._get_device_data(device_id)
9996
# Add plugwise notification binary_sensor to the relevant gateway
@@ -160,7 +157,7 @@ def get_all_devices(self) -> None:
160157
self._all_device_data()
161158

162159
def _device_data_switching_group(
163-
self, details: ApplianceData, device_data: DeviceData
160+
self, details: DeviceData, device_data: DeviceData
164161
) -> DeviceData:
165162
"""Helper-function for _get_device_data().
166163
@@ -178,7 +175,7 @@ def _device_data_switching_group(
178175
return device_data
179176

180177
def _device_data_adam(
181-
self, details: ApplianceData, device_data: DeviceData
178+
self, details: DeviceData, device_data: DeviceData
182179
) -> DeviceData:
183180
"""Helper-function for _get_device_data().
184181
@@ -196,7 +193,7 @@ def _device_data_adam(
196193
return device_data
197194

198195
def _device_data_climate(
199-
self, details: ApplianceData, device_data: DeviceData
196+
self, details: DeviceData, device_data: DeviceData
200197
) -> DeviceData:
201198
"""Helper-function for _get_device_data().
202199
@@ -249,7 +246,7 @@ def _device_data_climate(
249246
return device_data
250247

251248
def _check_availability(
252-
self, details: ApplianceData, device_data: DeviceData
249+
self, details: DeviceData, device_data: DeviceData
253250
) -> DeviceData:
254251
"""Helper-function for _get_device_data().
255252

plugwise/constants.py

Lines changed: 95 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,6 @@
3131
VOLUME_CUBIC_METERS: Final = "m³"
3232
VOLUME_CUBIC_METERS_PER_HOUR: Final = "m³/h"
3333

34-
ACTUATOR_CLASSES: Final[tuple[str, ...]] = (
35-
"heater_central",
36-
"thermostat",
37-
"thermostatic_radiator_valve",
38-
"zone_thermometer",
39-
"zone_thermostat",
40-
)
41-
ActuatorType = Literal[
42-
"domestic_hot_water_setpoint",
43-
"max_dhw_temperature",
44-
"maximum_boiler_temperature",
45-
"temperature_offset",
46-
"thermostat",
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-
]
5734
DAYS: Final[dict[str, int]] = {
5835
"mo": 0,
5936
"tu": 1,
@@ -108,36 +85,10 @@
10885
"070051": "Switch",
10986
"080029": "Switch",
11087
}
111-
LIMITS: Final[tuple[str, ...]] = (
112-
"offset",
113-
"setpoint",
114-
"resolution",
115-
"lower_bound",
116-
"upper_bound",
117-
)
88+
11889
MAX_SETPOINT: Final[float] = 30.0
11990
MIN_SETPOINT: Final[float] = 4.0
12091
NONE: Final = "None"
121-
SPECIAL_FORMAT: Final[tuple[str, ...]] = (ENERGY_KILO_WATT_HOUR, VOLUME_CUBIC_METERS)
122-
SWITCH_GROUP_TYPES: Final[tuple[str, ...]] = ("switching", "report")
123-
ZONE_THERMOSTATS: Final[tuple[str, ...]] = (
124-
"thermostat",
125-
"thermostatic_radiator_valve",
126-
"zone_thermometer",
127-
"zone_thermostat",
128-
)
129-
THERMOSTAT_CLASSES: Final[tuple[str, ...]] = (
130-
"thermostat",
131-
"thermo_sensor",
132-
"zone_thermometer",
133-
"zone_thermostat",
134-
"thermostatic_radiator_valve",
135-
)
136-
SPECIAL_PLUG_TYPES: Final[tuple[str, ...]] = (
137-
"central_heating_pump",
138-
"valve_actuator",
139-
"heater_electric",
140-
)
14192

14293
# XML data paths
14394
APPLIANCES: Final = "/core/appliances"
@@ -151,6 +102,7 @@
151102

152103
UOM = namedtuple("UOM", "unit_of_measurement")
153104
DATA = namedtuple("DATA", "name unit_of_measurement")
105+
154106
# P1 related measurements:
155107
P1_MEASUREMENTS: Final[dict[str, UOM]] = {
156108
"electricity_consumed": UOM(POWER_WATT),
@@ -234,15 +186,6 @@
234186
"outdoor_temperature": DATA("outdoor_air_temperature", TEMP_CELSIUS),
235187
}
236188

237-
ToggleNameType = Literal[
238-
"cooling_ena_switch",
239-
"dhw_cm_switch",
240-
]
241-
TOGGLES: Final[dict[str, ToggleNameType]] = {
242-
"cooling_enabled": "cooling_ena_switch",
243-
"domestic_hot_water_comfort_mode": "dhw_cm_switch",
244-
}
245-
246189
# Known types of Smiles and Stretches
247190
SMILE = namedtuple("SMILE", "smile_type smile_name")
248191
SMILES: Final[dict[str, SMILE]] = {
@@ -258,7 +201,32 @@
258201
"stretch_v3": SMILE("stretch", "Stretch"),
259202
}
260203

261-
# All available Binary Sensor, Sensor, and Switch Types
204+
# Class, Literal and related tuple-definitions
205+
206+
ACTUATOR_CLASSES: Final[tuple[str, ...]] = (
207+
"heater_central",
208+
"thermostat",
209+
"thermostatic_radiator_valve",
210+
"zone_thermometer",
211+
"zone_thermostat",
212+
)
213+
ActuatorType = Literal[
214+
"domestic_hot_water_setpoint",
215+
"max_dhw_temperature",
216+
"maximum_boiler_temperature",
217+
"temperature_offset",
218+
"thermostat",
219+
]
220+
ACTIVE_ACTUATORS: Final[tuple[str, ...]] = get_args(ActuatorType)
221+
222+
ActuatorDataType = Literal[
223+
"lower_bound",
224+
"resolution",
225+
"setpoint",
226+
"setpoint_high",
227+
"setpoint_low",
228+
"upper_bound",
229+
]
262230

263231
ApplianceType = Literal[
264232
"dev_class",
@@ -291,6 +259,14 @@
291259
"temperature_offset",
292260
]
293261

262+
LIMITS: Final[tuple[str, ...]] = (
263+
"offset",
264+
"setpoint",
265+
"resolution",
266+
"lower_bound",
267+
"upper_bound",
268+
)
269+
294270
SelectType = Literal[
295271
"select_dhw_mode",
296272
"select_regulation_mode",
@@ -357,6 +333,14 @@
357333
]
358334
SENSORS: Final[tuple[str, ...]] = get_args(SensorType)
359335

336+
SPECIAL_PLUG_TYPES: Final[tuple[str, ...]] = (
337+
"central_heating_pump",
338+
"valve_actuator",
339+
"heater_electric",
340+
)
341+
342+
SPECIAL_FORMAT: Final[tuple[str, ...]] = (ENERGY_KILO_WATT_HOUR, VOLUME_CUBIC_METERS)
343+
360344
SwitchType = Literal[
361345
"cooling_ena_switch",
362346
"dhw_cm_switch",
@@ -365,20 +349,31 @@
365349
]
366350
SWITCHES: Final[tuple[str, ...]] = get_args(SwitchType)
367351

352+
SWITCH_GROUP_TYPES: Final[tuple[str, ...]] = ("switching", "report")
368353

369-
class ApplianceData(TypedDict, total=False):
370-
"""The Appliance Data class."""
354+
THERMOSTAT_CLASSES: Final[tuple[str, ...]] = (
355+
"thermostat",
356+
"thermo_sensor",
357+
"zone_thermometer",
358+
"zone_thermostat",
359+
"thermostatic_radiator_valve",
360+
)
371361

372-
dev_class: str
373-
firmware: str | None
374-
hardware: str
375-
location: str
376-
mac_address: str | None
377-
members: list[str]
378-
model: str
379-
name: str
380-
vendor: str
381-
zigbee_mac_address: str | None
362+
ToggleNameType = Literal[
363+
"cooling_ena_switch",
364+
"dhw_cm_switch",
365+
]
366+
TOGGLES: Final[dict[str, ToggleNameType]] = {
367+
"cooling_enabled": "cooling_ena_switch",
368+
"domestic_hot_water_comfort_mode": "dhw_cm_switch",
369+
}
370+
371+
ZONE_THERMOSTATS: Final[tuple[str, ...]] = (
372+
"thermostat",
373+
"thermostatic_radiator_valve",
374+
"zone_thermometer",
375+
"zone_thermostat",
376+
)
382377

383378

384379
class GatewayData(TypedDict, total=False):
@@ -449,7 +444,6 @@ class SmileSensors(TypedDict, total=False):
449444
electricity_produced_peak_interval: int
450445
electricity_produced_peak_point: int
451446
electricity_produced_point: float
452-
elga_status_code: int
453447
gas_consumed_cumulative: float
454448
gas_consumed_interval: float
455449
humidity: float
@@ -502,16 +496,30 @@ class ActuatorData(TypedDict, total=False):
502496
upper_bound: float
503497

504498

505-
class DeviceData(
506-
ApplianceData,
507-
SmileBinarySensors,
508-
SmileSensors,
509-
SmileSwitches,
510-
TypedDict,
511-
total=False,
512-
):
499+
class DeviceData(TypedDict, total=False):
513500
"""The Device Data class, covering the collected and ordered output-data per device."""
514501

502+
# Appliance base data
503+
dev_class: str
504+
firmware: str | None
505+
hardware: str
506+
location: str
507+
mac_address: str | None
508+
members: list[str]
509+
model: str
510+
name: str
511+
vendor: str
512+
zigbee_mac_address: str | None
513+
514+
# For temporary use
515+
cooling_enabled: bool
516+
domestic_hot_water_setpoint: float
517+
elga_status_code: int
518+
c_heating_state: bool
519+
520+
# Device availability
521+
available: bool | None
522+
515523
# Loria
516524
select_dhw_mode: str
517525
dhw_modes: list[str]
@@ -521,31 +529,25 @@ class DeviceData(
521529
regulation_modes: list[str]
522530

523531
# Master Thermostats
524-
preset_modes: list[str] | None
532+
# Presets:
525533
active_preset: str | None
526-
534+
preset_modes: list[str] | None
535+
# Schedules:
527536
available_schedules: list[str]
528-
select_schedule: str
529537
last_used: str | None
538+
select_schedule: str
530539

531540
mode: str
532-
533541
# Extra for Adam Master Thermostats
534542
control_state: str | bool
535543

536-
# For temporary use
537-
c_heating_state: bool
538-
modified: str
539-
540-
# Device availability
541-
available: bool | None
542-
544+
# Dict-types
543545
binary_sensors: SmileBinarySensors
544546
max_dhw_temperature: ActuatorData
545547
maximum_boiler_temperature: ActuatorData
546-
temperature_offset: ActuatorData
547548
sensors: SmileSensors
548549
switches: SmileSwitches
550+
temperature_offset: ActuatorData
549551
thermostat: ActuatorData
550552

551553

plugwise/helper.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
ActuatorData,
5555
ActuatorDataType,
5656
ActuatorType,
57-
ApplianceData,
5857
ApplianceType,
5958
BinarySensorType,
6059
DeviceData,
@@ -309,7 +308,7 @@ class SmileHelper:
309308
def __init__(self) -> None:
310309
"""Set the constructor for this class."""
311310
self._appliances: etree
312-
self._appl_data: dict[str, ApplianceData] = {}
311+
self._appl_data: dict[str, DeviceData] = {}
313312
self._cooling_activation_outdoor_temp: float
314313
self._cooling_deactivation_threshold: float
315314
self._cooling_present = False
@@ -1101,7 +1100,7 @@ def _rank_thermostat(
11011100
thermo_matching: dict[str, int],
11021101
loc_id: str,
11031102
appliance_id: str,
1104-
appliance_details: ApplianceData,
1103+
appliance_details: DeviceData,
11051104
) -> None:
11061105
"""Helper-function for _scan_thermostats().
11071106
@@ -1176,12 +1175,12 @@ def _thermostat_uri(self, loc_id: str) -> str:
11761175

11771176
return f"{LOCATIONS};id={loc_id}/thermostat;id={thermostat_functionality_id}"
11781177

1179-
def _group_switches(self) -> dict[str, ApplianceData]:
1178+
def _group_switches(self) -> dict[str, DeviceData]:
11801179
"""Helper-function for smile.py: get_all_devices().
11811180
11821181
Collect switching- or pump-group info.
11831182
"""
1184-
switch_groups: dict[str, ApplianceData] = {}
1183+
switch_groups: dict[str, DeviceData] = {}
11851184
# P1 and Anna don't have switchgroups
11861185
if self.smile_type == "power" or self.smile_name == "Smile Anna":
11871186
return switch_groups

0 commit comments

Comments
 (0)