Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement lights #57

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 58 additions & 5 deletions custom_components/deltadore_tydom/ha_entities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Home assistant entites."""
from typing import Any
from datetime import date
import math

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
Expand Down Expand Up @@ -31,10 +32,14 @@
)

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.percentage import (
percentage_to_ranged_value,
ranged_value_to_percentage,
)

from .tydom.tydom_devices import (
Tydom,
Expand Down Expand Up @@ -770,13 +775,20 @@ 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."""

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."""
Expand All @@ -795,12 +807,26 @@ def device_info(self) -> DeviceInfo:
"name": self.name,
}

@property
def is_closed(self) -> bool:
"""Return if the garage door is closed."""
return None

async def async_open_cover(self, **kwargs: Any) -> None:
"""Open the cover."""
await self._device.open()

class HaLight(LightEntity, HAEntity):
"""Representation of a Light."""

should_poll = False
supported_features = None
sensor_classes = {}
sensor_classes = {
"thermic_defect": BinarySensorDeviceClass.PROBLEM,
}
color_mode = set()
supported_color_modes = set()

BRIGHTNESS_SCALE = (0, 255)

def __init__(self, device: TydomLight, hass) -> None:
"""Initialize the sensor."""
Expand All @@ -810,6 +836,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 +851,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(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()

class HaAlarm(AlarmControlPanelEntity, HAEntity):
"""Representation of an Alarm."""

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

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", str(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."""