Skip to content

Commit

Permalink
implement lights ref#55
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilp committed Mar 1, 2024
1 parent cbe9908 commit 9dedf90
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
43 changes: 41 additions & 2 deletions custom_components/deltadore_tydom/ha_entities.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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."""

Expand All @@ -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."""
Expand All @@ -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:
Expand All @@ -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."""

Expand Down
26 changes: 26 additions & 0 deletions custom_components/deltadore_tydom/tydom/tydom_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

0 comments on commit 9dedf90

Please sign in to comment.