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."""