Skip to content

Commit

Permalink
Merge pull request #375 from plugwise/reorder
Browse files Browse the repository at this point in the history
Integrate creating platform-dicts and improve typing
  • Loading branch information
bouwew authored Aug 29, 2023
2 parents 59ffe4c + 1fceb16 commit 0412862
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 223 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ jobs:
needs: commitcheck
strategy:
matrix:
python-version: ["3.11", "3.10", "3.9"]
python-version: ["3.11", "3.10"]
steps:
- name: Check out committed code
uses: actions/checkout@v3
Expand Down Expand Up @@ -206,7 +206,7 @@ jobs:
needs: prepare-test-cache
strategy:
matrix:
python-version: ["3.11", "3.10", "3.9"]
python-version: ["3.11", "3.10"]

steps:
- name: Check out committed code
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v0.32.1 Improve typing, bugfix

- Integrate the process of creating binary_sensors, sensors, and switches dicts. Should make typing simpler.
- Fix an apparent notification-bug for p1v4.
- Improve typing: fix all type-ignores.
- Clean up no longer used code.
- Remove support for python3.9.

## v0.32.0: New Feature: add support for changing the temperature offset on a supported thermostat device

- Add support for changing the temperature-offset on Jip, Lisa, Tom, Floor and on Anna (in some configurations)
Expand Down
2 changes: 1 addition & 1 deletion fixtures/p1v4/all_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"devices": {
"a455b61e52394b2db5081ce025a430f3": {
"binary_sensors": {
"plugwise_notification": false
"plugwise_notification": true
},
"dev_class": "gateway",
"firmware": "4.1.1",
Expand Down
83 changes: 46 additions & 37 deletions plugwise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""
from __future__ import annotations

from typing import cast

import aiohttp
from defusedxml import ElementTree as etree

Expand Down Expand Up @@ -35,17 +37,26 @@
ApplianceData,
DeviceData,
PlugwiseData,
SmileBinarySensors,
SmileSensors,
SmileSwitches,
)
from .exceptions import (
InvalidSetupError,
PlugwiseError,
ResponseError,
UnsupportedDeviceError,
)
from .helper import SmileComm, SmileHelper, update_helper
from .helper import SmileComm, SmileHelper


def remove_empty_platform_dicts(data: DeviceData) -> DeviceData:
"""Helper-function for removing any empty platform dicts."""
if not data["binary_sensors"]:
data.pop("binary_sensors")
if not data["sensors"]:
data.pop("sensors")
if not data["switches"]:
data.pop("switches")

return data


class SmileData(SmileHelper):
Expand Down Expand Up @@ -82,18 +93,24 @@ def _all_device_data(self) -> None:
Collect initial data for each device and add to self.gw_data and self.gw_devices.
"""
for device_id, device in self._appl_data.items():
bs_dict: SmileBinarySensors = {}
s_dict: SmileSensors = {}
sw_dict: SmileSwitches = {}
self.gw_devices.update({device_id: cast(DeviceData, device)})

data = self._get_device_data(device_id)
self.gw_devices[device_id] = self._update_device_with_dicts(
device_id, data, device, bs_dict, s_dict, sw_dict
)
# Add plugwise notification binary_sensor to the relevant gateway
if device_id == self.gateway_id and (
self._is_thermostat
or (not self._smile_legacy and self.smile_type == "power")
):
data["binary_sensors"]["plugwise_notification"] = False

self.gw_devices[device_id].update(data)

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

remove_empty_platform_dicts(self.gw_devices[device_id])

self.gw_data.update(
{"smile_name": self.smile_name, "gateway_id": self.gateway_id}
)
Expand Down Expand Up @@ -153,10 +170,10 @@ def _device_data_switching_group(
counter = 0
for member in details["members"]:
member_data = self._get_appliance_data(member)
if member_data.get("relay"):
if member_data["switches"].get("relay"):
counter += 1

device_data["relay"] = counter != 0
device_data["switches"]["relay"] = counter != 0

return device_data

Expand All @@ -174,7 +191,7 @@ def _device_data_adam(
and self._on_off_device
and self._heating_valves() is not None
):
device_data["heating_state"] = self._heating_valves() != 0
device_data["binary_sensors"]["heating_state"] = self._heating_valves() != 0

return device_data

Expand Down Expand Up @@ -275,7 +292,7 @@ def _get_device_data(self, dev_id: str) -> DeviceData:
self._home_location, "outdoor_temperature"
)
if outdoor_temperature is not None:
device_data["outdoor_temperature"] = outdoor_temperature
device_data["sensors"]["outdoor_temperature"] = outdoor_temperature

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

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

for dev_id, dev_dict in self.gw_devices.items():
data = self._get_device_data(dev_id)
for key, value in data.items():
if key in dev_dict:
dev_dict[key] = value # type: ignore [literal-required]

for item in ("binary_sensors", "sensors", "switches"):
notifs: dict[str, dict[str, str]] = {}
if item == "binary_sensors":
notifs = self._notifications
if item in dev_dict:
for key in data:
update_helper(
data,
self.gw_devices,
dev_dict,
dev_id,
item,
key,
notifs,
)
for device_id, device in self.gw_devices.items():
data = self._get_device_data(device_id)
if (
"binary_sensors" in device
and "plugwise_notification" in device["binary_sensors"]
):
data["binary_sensors"]["plugwise_notification"] = bool(
self._notifications
)

device.update(data)

# Update for cooling
if dev_dict["dev_class"] in ZONE_THERMOSTATS:
self.update_for_cooling(dev_dict)
if device["dev_class"] in ZONE_THERMOSTATS:
self.update_for_cooling(device)

remove_empty_platform_dicts(device)

return PlugwiseData(self.gw_data, self.gw_devices)

Expand Down
34 changes: 29 additions & 5 deletions plugwise/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@
"zone_thermometer",
"zone_thermostat",
)
ACTIVE_ACTUATORS: Final[tuple[str, ...]] = (
ActuatorType = Literal[
"domestic_hot_water_setpoint",
"max_dhw_temperature",
"maximum_boiler_temperature",
"temperature_offset",
"thermostat",
)
]
ACTIVE_ACTUATORS: Final[tuple[str, ...]] = get_args(ActuatorType)
ActuatorDataType = Literal[
"lower_bound",
"resolution",
"setpoint",
"setpoint_high",
"setpoint_low",
"upper_bound",
]
DAYS: Final[dict[str, int]] = {
"mo": 0,
"tu": 1,
Expand Down Expand Up @@ -206,7 +215,6 @@
"intended_central_heating_state": DATA(
"heating_state", NONE
), # This key shows in general the heating-behavior better than c-h_state. except when connected to a heatpump
"maximum_boiler_temperature": UOM(TEMP_CELSIUS),
"modulation_level": UOM(PERCENTAGE),
"return_water_temperature": DATA("return_temperature", TEMP_CELSIUS),
# Used with the Elga heatpump - marcelveldt
Expand All @@ -226,7 +234,11 @@
"outdoor_temperature": DATA("outdoor_air_temperature", TEMP_CELSIUS),
}

TOGGLES: Final[dict[str, str]] = {
ToggleNameType = Literal[
"cooling_ena_switch",
"dhw_cm_switch",
]
TOGGLES: Final[dict[str, ToggleNameType]] = {
"cooling_enabled": "cooling_ena_switch",
"domestic_hot_water_comfort_mode": "dhw_cm_switch",
}
Expand All @@ -248,6 +260,19 @@

# All available Binary Sensor, Sensor, and Switch Types

ApplianceType = Literal[
"dev_class",
"firmware",
"hardware",
"location",
"mac_address",
"members",
"model",
"name",
"vendor",
"zigbee_mac_address",
]

BinarySensorType = Literal[
"cooling_enabled",
"compressor_state",
Expand All @@ -271,7 +296,6 @@
"select_regulation_mode",
"select_schedule",
]

SelectOptionsType = Literal[
"dhw_modes",
"regulation_modes",
Expand Down
Loading

0 comments on commit 0412862

Please sign in to comment.