2019-02-13 20:21:14 +00:00
|
|
|
"""Support for control of ElkM1 lighting (X10, UPB, etc)."""
|
2022-01-05 11:49:13 +00:00
|
|
|
from __future__ import annotations
|
2020-03-27 20:38:35 +00:00
|
|
|
|
2022-04-18 00:31:37 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from elkm1_lib.elements import Element
|
|
|
|
from elkm1_lib.elk import Elk
|
|
|
|
from elkm1_lib.lights import Light
|
|
|
|
|
2022-04-23 18:40:58 +00:00
|
|
|
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
|
2022-01-05 11:49:13 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-03-27 20:38:35 +00:00
|
|
|
from . import ElkEntity, create_elk_entities
|
|
|
|
from .const import DOMAIN
|
2018-10-08 15:30:27 +00:00
|
|
|
|
|
|
|
|
2022-01-05 11:49:13 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2018-10-08 15:30:27 +00:00
|
|
|
"""Set up the Elk light platform."""
|
2020-03-27 20:38:35 +00:00
|
|
|
elk_data = hass.data[DOMAIN][config_entry.entry_id]
|
2022-04-15 21:14:45 +00:00
|
|
|
entities: list[ElkEntity] = []
|
2020-03-27 20:38:35 +00:00
|
|
|
elk = elk_data["elk"]
|
|
|
|
create_elk_entities(elk_data, elk.lights, "plc", ElkLight, entities)
|
2022-07-11 07:11:37 +00:00
|
|
|
async_add_entities(entities)
|
2018-10-08 15:30:27 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class ElkLight(ElkEntity, LightEntity):
|
2019-02-13 20:21:14 +00:00
|
|
|
"""Representation of an Elk lighting device."""
|
2018-10-08 15:30:27 +00:00
|
|
|
|
2022-04-23 18:40:58 +00:00
|
|
|
_attr_color_mode = ColorMode.BRIGHTNESS
|
|
|
|
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
2022-04-18 00:31:37 +00:00
|
|
|
_element: Light
|
2022-04-03 11:56:46 +00:00
|
|
|
|
2022-04-18 00:31:37 +00:00
|
|
|
def __init__(self, element: Element, elk: Elk, elk_data: dict[str, Any]) -> None:
|
2019-02-13 20:21:14 +00:00
|
|
|
"""Initialize the Elk light."""
|
2018-10-10 17:05:19 +00:00
|
|
|
super().__init__(element, elk, elk_data)
|
2018-10-08 15:30:27 +00:00
|
|
|
self._brightness = self._element.status
|
|
|
|
|
|
|
|
@property
|
2022-04-18 00:31:37 +00:00
|
|
|
def brightness(self) -> int:
|
2018-10-08 15:30:27 +00:00
|
|
|
"""Get the brightness."""
|
|
|
|
return self._brightness
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Get the current brightness."""
|
|
|
|
return self._brightness != 0
|
|
|
|
|
2022-04-18 00:31:37 +00:00
|
|
|
def _element_changed(self, element: Element, changeset: Any) -> None:
|
2018-10-08 15:30:27 +00:00
|
|
|
status = self._element.status if self._element.status != 1 else 100
|
|
|
|
self._brightness = round(status * 2.55)
|
|
|
|
|
2022-04-18 00:31:37 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2018-10-08 15:30:27 +00:00
|
|
|
"""Turn on the light."""
|
|
|
|
self._element.level(round(kwargs.get(ATTR_BRIGHTNESS, 255) / 2.55))
|
|
|
|
|
2022-04-18 00:31:37 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2018-10-08 15:30:27 +00:00
|
|
|
"""Turn off the light."""
|
|
|
|
self._element.level(0)
|