2019-12-08 08:26:31 +00:00
|
|
|
"""Support for LED lights."""
|
2021-02-13 22:50:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-12-08 08:26:31 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2021-05-04 21:36:48 +00:00
|
|
|
from typing import Any
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
from elgato import Elgato, ElgatoError, Info, State
|
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-12-08 08:26:31 +00:00
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-05-03 16:42:45 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_IDENTIFIERS,
|
|
|
|
ATTR_MANUFACTURER,
|
|
|
|
ATTR_MODEL,
|
|
|
|
ATTR_NAME,
|
|
|
|
ATTR_SW_VERSION,
|
|
|
|
)
|
2021-04-21 10:18:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 21:36:48 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import (
|
|
|
|
AddEntitiesCallback,
|
|
|
|
async_get_current_platform,
|
|
|
|
)
|
2021-05-01 03:51:53 +00:00
|
|
|
|
2021-05-03 15:32:10 +00:00
|
|
|
from .const import DATA_ELGATO_CLIENT, DOMAIN, SERVICE_IDENTIFY
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=10)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-04-21 10:18:42 +00:00
|
|
|
hass: HomeAssistant,
|
2019-12-08 08:26:31 +00:00
|
|
|
entry: ConfigEntry,
|
2021-05-04 21:36:48 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2019-12-08 08:26:31 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up Elgato Key Light based on a config entry."""
|
|
|
|
elgato: Elgato = hass.data[DOMAIN][entry.entry_id][DATA_ELGATO_CLIENT]
|
|
|
|
info = await elgato.info()
|
2021-02-13 22:50:25 +00:00
|
|
|
async_add_entities([ElgatoLight(elgato, info)], True)
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2021-05-03 15:32:10 +00:00
|
|
|
platform = async_get_current_platform()
|
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_IDENTIFY,
|
|
|
|
{},
|
|
|
|
ElgatoLight.async_identify.__name__,
|
|
|
|
)
|
|
|
|
|
2019-12-08 08:26:31 +00:00
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class ElgatoLight(LightEntity):
|
2019-12-08 08:26:31 +00:00
|
|
|
"""Defines a Elgato Key Light."""
|
|
|
|
|
|
|
|
def __init__(
|
2020-08-27 11:56:20 +00:00
|
|
|
self,
|
|
|
|
elgato: Elgato,
|
|
|
|
info: Info,
|
2021-04-30 20:21:30 +00:00
|
|
|
) -> None:
|
2019-12-08 08:26:31 +00:00
|
|
|
"""Initialize Elgato Key Light."""
|
|
|
|
self._info: Info = info
|
2021-03-09 18:52:53 +00:00
|
|
|
self._state: State | None = None
|
2019-12-08 08:26:31 +00:00
|
|
|
self.elgato = elgato
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the entity."""
|
|
|
|
# Return the product name, if display name is not set
|
2021-03-09 18:52:53 +00:00
|
|
|
return self._info.display_name or self._info.product_name
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
2021-03-09 18:52:53 +00:00
|
|
|
return self._state is not None
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID for this sensor."""
|
|
|
|
return self._info.serial_number
|
|
|
|
|
|
|
|
@property
|
2021-02-13 22:50:25 +00:00
|
|
|
def brightness(self) -> int | None:
|
2019-12-08 08:26:31 +00:00
|
|
|
"""Return the brightness of this light between 1..255."""
|
2021-03-09 18:52:53 +00:00
|
|
|
assert self._state is not None
|
|
|
|
return round((self._state.brightness * 255) / 100)
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
@property
|
2021-02-13 22:50:25 +00:00
|
|
|
def color_temp(self) -> int | None:
|
2019-12-08 08:26:31 +00:00
|
|
|
"""Return the CT color value in mireds."""
|
2021-03-09 18:52:53 +00:00
|
|
|
assert self._state is not None
|
|
|
|
return self._state.temperature
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
@property
|
2021-02-13 22:50:25 +00:00
|
|
|
def min_mireds(self) -> int:
|
2019-12-08 08:26:31 +00:00
|
|
|
"""Return the coldest color_temp that this light supports."""
|
|
|
|
return 143
|
|
|
|
|
|
|
|
@property
|
2021-02-13 22:50:25 +00:00
|
|
|
def max_mireds(self) -> int:
|
2019-12-08 08:26:31 +00:00
|
|
|
"""Return the warmest color_temp that this light supports."""
|
|
|
|
return 344
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return the state of the light."""
|
2021-03-09 18:52:53 +00:00
|
|
|
assert self._state is not None
|
|
|
|
return self._state.on
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn off the light."""
|
2021-04-30 20:21:30 +00:00
|
|
|
try:
|
|
|
|
await self.elgato.light(on=False)
|
|
|
|
except ElgatoError:
|
|
|
|
_LOGGER.error("An error occurred while updating the Elgato Key Light")
|
|
|
|
self._state = None
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn on the light."""
|
2021-04-30 20:21:30 +00:00
|
|
|
temperature = kwargs.get(ATTR_COLOR_TEMP)
|
|
|
|
brightness = None
|
2019-12-08 08:26:31 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
2021-04-30 20:21:30 +00:00
|
|
|
brightness = round((kwargs[ATTR_BRIGHTNESS] / 255) * 100)
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
try:
|
2021-04-30 20:21:30 +00:00
|
|
|
await self.elgato.light(
|
|
|
|
on=True, brightness=brightness, temperature=temperature
|
|
|
|
)
|
2019-12-08 08:26:31 +00:00
|
|
|
except ElgatoError:
|
|
|
|
_LOGGER.error("An error occurred while updating the Elgato Key Light")
|
2021-03-09 18:52:53 +00:00
|
|
|
self._state = None
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
async def async_update(self) -> None:
|
|
|
|
"""Update Elgato entity."""
|
2021-03-09 18:52:53 +00:00
|
|
|
restoring = self._state is None
|
2019-12-08 08:26:31 +00:00
|
|
|
try:
|
2021-04-30 20:21:30 +00:00
|
|
|
self._state = await self.elgato.state()
|
2021-03-09 18:52:53 +00:00
|
|
|
if restoring:
|
|
|
|
_LOGGER.info("Connection restored")
|
|
|
|
except ElgatoError as err:
|
|
|
|
meth = _LOGGER.error if self._state else _LOGGER.debug
|
|
|
|
meth("An error occurred while updating the Elgato Key Light: %s", err)
|
|
|
|
self._state = None
|
2019-12-08 08:26:31 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-01 03:51:53 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2019-12-08 08:26:31 +00:00
|
|
|
"""Return device information about this Elgato Key Light."""
|
|
|
|
return {
|
2021-05-03 16:42:45 +00:00
|
|
|
ATTR_IDENTIFIERS: {(DOMAIN, self._info.serial_number)},
|
|
|
|
ATTR_NAME: self._info.product_name,
|
|
|
|
ATTR_MANUFACTURER: "Elgato",
|
|
|
|
ATTR_MODEL: self._info.product_name,
|
|
|
|
ATTR_SW_VERSION: f"{self._info.firmware_version} ({self._info.firmware_build_number})",
|
2019-12-08 08:26:31 +00:00
|
|
|
}
|
2021-05-03 15:32:10 +00:00
|
|
|
|
|
|
|
async def async_identify(self) -> None:
|
|
|
|
"""Identify the light, will make it blink."""
|
|
|
|
try:
|
|
|
|
await self.elgato.identify()
|
|
|
|
except ElgatoError:
|
|
|
|
_LOGGER.exception("An error occurred while identifying the Elgato Light")
|
|
|
|
self._state = None
|