From 9dedf9004bec79bf1eefdb634c898afafe23f67b Mon Sep 17 00:00:00 2001 From: cyrilp Date: Fri, 1 Mar 2024 20:49:09 +0100 Subject: [PATCH 1/6] implement lights ref#55 --- .../deltadore_tydom/ha_entities.py | 43 ++++++++++++++++++- .../deltadore_tydom/tydom/tydom_devices.py | 26 +++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/custom_components/deltadore_tydom/ha_entities.py b/custom_components/deltadore_tydom/ha_entities.py index c49e31d..e02b203 100644 --- a/custom_components/deltadore_tydom/ha_entities.py +++ b/custom_components/deltadore_tydom/ha_entities.py @@ -1,6 +1,8 @@ """Home assistant entites.""" from typing import Any +from typing import Optional from datetime import date +import math from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, @@ -31,10 +33,11 @@ ) from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass, SensorEntity -from homeassistant.components.light import LightEntity +from homeassistant.components.light import LightEntity, ColorMode, ATTR_BRIGHTNESS from homeassistant.components.lock import LockEntity from homeassistant.components.update import UpdateEntity, UpdateEntityFeature, UpdateDeviceClass from homeassistant.components.alarm_control_panel import AlarmControlPanelEntity, CodeFormat +from homeassistant.util.color import value_to_brightness from .tydom.tydom_devices import ( Tydom, @@ -770,6 +773,11 @@ def device_info(self) -> DeviceInfo: "name": self.name, } + @property + def is_closed(self) -> bool: + """Return if the window is closed.""" + return self._device.openState == "LOCKED" + class HaGarage(CoverEntity, HAEntity): """Representation of a Garage door.""" @@ -795,12 +803,21 @@ def device_info(self) -> DeviceInfo: "name": self.name, } + @property + def is_closed(self) -> bool: + """Return if the window is closed.""" + return None + #return self._device.openState == "LOCKED" + class HaLight(LightEntity, HAEntity): """Representation of a Light.""" should_poll = False - supported_features = None sensor_classes = {} + color_mode = set() + supported_color_modes = set() + + BRIGHTNESS_SCALE = (0, 100) def __init__(self, device: TydomLight, hass) -> None: """Initialize the sensor.""" @@ -810,6 +827,12 @@ def __init__(self, device: TydomLight, hass) -> None: self._attr_unique_id = f"{self._device.device_id}_cover" self._attr_name = self._device.device_name self._registered_sensors = [] + if "level" in self._device._metadata: + self.color_mode.add(ColorMode.BRIGHTNESS) + self.supported_color_modes.add(ColorMode.BRIGHTNESS) + else: + self.color_mode.add(ColorMode.ONOFF) + self.supported_color_modes.add(ColorMode.ONOFF) @property def device_info(self) -> DeviceInfo: @@ -819,6 +842,22 @@ def device_info(self) -> DeviceInfo: "name": self.name, } + async def async_turn_on(self, **kwargs): + """Turn device on.""" + brightness = None + if ATTR_BRIGHTNESS in kwargs: + brightness = math.ceil(self.percentage_to_ranged_value(self.BRIGHTNESS_SCALE, kwargs[ATTR_BRIGHTNESS])) + await self._device.turn_on(brightness) + + async def async_turn_off(self, **kwargs): + """Turn device off.""" + await self._device.turn_off() + + @property + def brightness(self) -> Optional[int]: + """Return the current brightness.""" + return value_to_brightness(self.BRIGHTNESS_SCALE, self._device.brightness) + class HaAlarm(AlarmControlPanelEntity, HAEntity): """Representation of an Alarm.""" diff --git a/custom_components/deltadore_tydom/tydom/tydom_devices.py b/custom_components/deltadore_tydom/tydom/tydom_devices.py index a613388..f06c8e9 100644 --- a/custom_components/deltadore_tydom/tydom/tydom_devices.py +++ b/custom_components/deltadore_tydom/tydom/tydom_devices.py @@ -245,5 +245,31 @@ class TydomGarage(TydomDevice): class TydomLight(TydomDevice): """represents a light.""" + async def turn_on(self, brightness) -> None: + """Tell light to turn on.""" + if brightness is None: + command = "TOGGLE" + if "ON" in self._metadata["levelCmd"]["enum_values"]: + command = "ON" + + await self._tydom_client.put_devices_data( + self._id, self._endpoint, "levelCmd", command + ) + else: + await self._tydom_client.put_devices_data( + self._id, self._endpoint, "level", brightness + ) + + async def turn_off(self) -> None: + """Tell light to turn off.""" + + command = "TOGGLE" + if "OFF" in self._metadata["levelCmd"]["enum_values"]: + command = "OFF" + + await self._tydom_client.put_devices_data( + self._id, self._endpoint, "levelCmd", command + ) + class TydomAlarm(TydomDevice): """represents an alarm.""" From 70c6c74c0005d4cf5957ac2f50a50dca87cb622f Mon Sep 17 00:00:00 2001 From: cyrilp Date: Fri, 1 Mar 2024 20:52:49 +0100 Subject: [PATCH 2/6] fix lint issue --- custom_components/deltadore_tydom/ha_entities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/deltadore_tydom/ha_entities.py b/custom_components/deltadore_tydom/ha_entities.py index e02b203..ec0d8b3 100644 --- a/custom_components/deltadore_tydom/ha_entities.py +++ b/custom_components/deltadore_tydom/ha_entities.py @@ -854,7 +854,7 @@ async def async_turn_off(self, **kwargs): await self._device.turn_off() @property - def brightness(self) -> Optional[int]: + def brightness(self) -> int | None: """Return the current brightness.""" return value_to_brightness(self.BRIGHTNESS_SCALE, self._device.brightness) From 7ba5fdac99a25429dbb7ec17d0578f5e4fd440d6 Mon Sep 17 00:00:00 2001 From: cyrilp Date: Fri, 1 Mar 2024 21:14:46 +0100 Subject: [PATCH 3/6] remove unused import --- custom_components/deltadore_tydom/ha_entities.py | 1 - 1 file changed, 1 deletion(-) diff --git a/custom_components/deltadore_tydom/ha_entities.py b/custom_components/deltadore_tydom/ha_entities.py index ec0d8b3..71bbfb0 100644 --- a/custom_components/deltadore_tydom/ha_entities.py +++ b/custom_components/deltadore_tydom/ha_entities.py @@ -1,6 +1,5 @@ """Home assistant entites.""" from typing import Any -from typing import Optional from datetime import date import math From 21d71dd4b1fff89734dea0462b09da0b1010062d Mon Sep 17 00:00:00 2001 From: cyrilp Date: Fri, 1 Mar 2024 23:29:40 +0100 Subject: [PATCH 4/6] handle brightness --- .../deltadore_tydom/ha_entities.py | 24 ++++++++++++------- .../deltadore_tydom/tydom/tydom_devices.py | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/custom_components/deltadore_tydom/ha_entities.py b/custom_components/deltadore_tydom/ha_entities.py index 71bbfb0..04444b8 100644 --- a/custom_components/deltadore_tydom/ha_entities.py +++ b/custom_components/deltadore_tydom/ha_entities.py @@ -36,7 +36,10 @@ from homeassistant.components.lock import LockEntity from homeassistant.components.update import UpdateEntity, UpdateEntityFeature, UpdateDeviceClass from homeassistant.components.alarm_control_panel import AlarmControlPanelEntity, CodeFormat -from homeassistant.util.color import value_to_brightness +from homeassistant.util.percentage import ( + percentage_to_ranged_value, + ranged_value_to_percentage, +) from .tydom.tydom_devices import ( Tydom, @@ -816,7 +819,7 @@ class HaLight(LightEntity, HAEntity): color_mode = set() supported_color_modes = set() - BRIGHTNESS_SCALE = (0, 100) + BRIGHTNESS_SCALE = (0, 255) def __init__(self, device: TydomLight, hass) -> None: """Initialize the sensor.""" @@ -841,22 +844,27 @@ def device_info(self) -> DeviceInfo: "name": self.name, } + @property + def brightness(self) -> int | None: + """Return the current brightness.""" + return percentage_to_ranged_value(self.BRIGHTNESS_SCALE, self._device.level) + + @property + def is_on(self) -> bool: + """Return true if light is on.""" + return bool(self._device.level != 0) + async def async_turn_on(self, **kwargs): """Turn device on.""" brightness = None if ATTR_BRIGHTNESS in kwargs: - brightness = math.ceil(self.percentage_to_ranged_value(self.BRIGHTNESS_SCALE, kwargs[ATTR_BRIGHTNESS])) + brightness = math.ceil(ranged_value_to_percentage(self.BRIGHTNESS_SCALE, kwargs[ATTR_BRIGHTNESS])) await self._device.turn_on(brightness) async def async_turn_off(self, **kwargs): """Turn device off.""" await self._device.turn_off() - @property - def brightness(self) -> int | None: - """Return the current brightness.""" - return value_to_brightness(self.BRIGHTNESS_SCALE, self._device.brightness) - class HaAlarm(AlarmControlPanelEntity, HAEntity): """Representation of an Alarm.""" diff --git a/custom_components/deltadore_tydom/tydom/tydom_devices.py b/custom_components/deltadore_tydom/tydom/tydom_devices.py index f06c8e9..710ade9 100644 --- a/custom_components/deltadore_tydom/tydom/tydom_devices.py +++ b/custom_components/deltadore_tydom/tydom/tydom_devices.py @@ -257,7 +257,7 @@ async def turn_on(self, brightness) -> None: ) else: await self._tydom_client.put_devices_data( - self._id, self._endpoint, "level", brightness + self._id, self._endpoint, "level", str(brightness) ) async def turn_off(self) -> None: From c4ad7170ea48be6edaf7254cf91467ccbfeb34ab Mon Sep 17 00:00:00 2001 From: cyrilp Date: Sun, 3 Mar 2024 11:55:50 +0100 Subject: [PATCH 5/6] take into accound garage door --- custom_components/deltadore_tydom/ha_entities.py | 13 +++++++++---- .../deltadore_tydom/tydom/tydom_devices.py | 6 ++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/custom_components/deltadore_tydom/ha_entities.py b/custom_components/deltadore_tydom/ha_entities.py index 04444b8..6ff39ec 100644 --- a/custom_components/deltadore_tydom/ha_entities.py +++ b/custom_components/deltadore_tydom/ha_entities.py @@ -784,9 +784,11 @@ class HaGarage(CoverEntity, HAEntity): """Representation of a Garage door.""" should_poll = False - supported_features = None + supported_features = CoverEntityFeature.OPEN device_class = CoverDeviceClass.GARAGE - sensor_classes = {} + sensor_classes = { + "thermic_defect": BinarySensorDeviceClass.PROBLEM, + } def __init__(self, device: TydomGarage, hass) -> None: """Initialize the sensor.""" @@ -807,9 +809,12 @@ def device_info(self) -> DeviceInfo: @property def is_closed(self) -> bool: - """Return if the window is closed.""" + """Return if the garage door is closed.""" return None - #return self._device.openState == "LOCKED" + + async def async_open_cover(self, **kwargs: Any) -> None: + """Open the cover.""" + await self._device.open() class HaLight(LightEntity, HAEntity): """Representation of a Light.""" diff --git a/custom_components/deltadore_tydom/tydom/tydom_devices.py b/custom_components/deltadore_tydom/tydom/tydom_devices.py index 710ade9..ccb229e 100644 --- a/custom_components/deltadore_tydom/tydom/tydom_devices.py +++ b/custom_components/deltadore_tydom/tydom/tydom_devices.py @@ -242,6 +242,12 @@ class TydomGate(TydomDevice): class TydomGarage(TydomDevice): """represents a garage door.""" + async def open(self) -> None: + """Tell garage door to open.""" + await self._tydom_client.put_devices_data( + self._id, self._endpoint, "levelCmd", "TOGGLE" + ) + class TydomLight(TydomDevice): """represents a light.""" From 15e5f508a7eb599e87f32815bf628b1e68bd1058 Mon Sep 17 00:00:00 2001 From: cyrilp Date: Sun, 3 Mar 2024 12:34:58 +0100 Subject: [PATCH 6/6] add device class for light sensor --- custom_components/deltadore_tydom/ha_entities.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/custom_components/deltadore_tydom/ha_entities.py b/custom_components/deltadore_tydom/ha_entities.py index 6ff39ec..292773b 100644 --- a/custom_components/deltadore_tydom/ha_entities.py +++ b/custom_components/deltadore_tydom/ha_entities.py @@ -820,7 +820,9 @@ class HaLight(LightEntity, HAEntity): """Representation of a Light.""" should_poll = False - sensor_classes = {} + sensor_classes = { + "thermic_defect": BinarySensorDeviceClass.PROBLEM, + } color_mode = set() supported_color_modes = set()