core/homeassistant/components/elgato/light.py

156 lines
5.0 KiB
Python
Raw Normal View History

"""Support for Elgato lights."""
2021-02-13 22:50:25 +00:00
from __future__ import annotations
from typing import Any
from elgato import ElgatoError
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_HS_COLOR,
2022-04-24 19:15:43 +00:00
ColorMode,
2020-04-26 16:49:41 +00:00
LightEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
2022-06-15 10:12:07 +00:00
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import (
AddEntitiesCallback,
async_get_current_platform,
)
2021-05-01 03:51:53 +00:00
2022-06-15 10:12:07 +00:00
from .const import DOMAIN, SERVICE_IDENTIFY
from .coordinator import ElgatoDataUpdateCoordinator
from .entity import ElgatoEntity
PARALLEL_UPDATES = 1
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Elgato Light based on a config entry."""
coordinator: ElgatoDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities([ElgatoLight(coordinator)])
platform = async_get_current_platform()
platform.async_register_entity_service(
SERVICE_IDENTIFY,
{},
ElgatoLight.async_identify.__name__,
)
class ElgatoLight(ElgatoEntity, LightEntity):
"""Defines an Elgato Light."""
_attr_min_mireds = 143
_attr_max_mireds = 344
def __init__(self, coordinator: ElgatoDataUpdateCoordinator) -> None:
"""Initialize Elgato Light."""
super().__init__(coordinator)
2022-04-24 19:15:43 +00:00
self._attr_supported_color_modes = {ColorMode.COLOR_TEMP}
self._attr_unique_id = coordinator.data.info.serial_number
# Elgato Light supporting color, have a different temperature range
if self.coordinator.data.settings.power_on_hue is not None:
2022-04-24 19:15:43 +00:00
self._attr_supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
self._attr_min_mireds = 153
self._attr_max_mireds = 285
@property
2021-02-13 22:50:25 +00:00
def brightness(self) -> int | None:
"""Return the brightness of this light between 1..255."""
return round((self.coordinator.data.state.brightness * 255) / 100)
@property
2021-02-13 22:50:25 +00:00
def color_temp(self) -> int | None:
"""Return the CT color value in mireds."""
return self.coordinator.data.state.temperature
@property
def color_mode(self) -> str | None:
"""Return the color mode of the light."""
if self.coordinator.data.state.hue is not None:
2022-04-24 19:15:43 +00:00
return ColorMode.HS
2022-04-24 19:15:43 +00:00
return ColorMode.COLOR_TEMP
@property
def hs_color(self) -> tuple[float, float] | None:
"""Return the hue and saturation color value [float, float]."""
return (
self.coordinator.data.state.hue or 0,
self.coordinator.data.state.saturation or 0,
)
@property
def is_on(self) -> bool:
"""Return the state of the light."""
return self.coordinator.data.state.on
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the light."""
try:
await self.coordinator.client.light(on=False)
2022-06-15 10:12:07 +00:00
except ElgatoError as error:
raise HomeAssistantError(
"An error occurred while updating the Elgato Light"
) from error
finally:
await self.coordinator.async_refresh()
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
temperature = kwargs.get(ATTR_COLOR_TEMP)
hue = None
saturation = None
if ATTR_HS_COLOR in kwargs:
hue, saturation = kwargs[ATTR_HS_COLOR]
brightness = None
if ATTR_BRIGHTNESS in kwargs:
brightness = round((kwargs[ATTR_BRIGHTNESS] / 255) * 100)
# For Elgato lights supporting color mode, but in temperature mode;
# adjusting only brightness make them jump back to color mode.
# Resending temperature prevents that.
if (
brightness
and ATTR_HS_COLOR not in kwargs
and ATTR_COLOR_TEMP not in kwargs
and self.supported_color_modes
2022-04-24 19:15:43 +00:00
and ColorMode.HS in self.supported_color_modes
and self.color_mode == ColorMode.COLOR_TEMP
):
temperature = self.color_temp
try:
await self.coordinator.client.light(
on=True,
brightness=brightness,
hue=hue,
saturation=saturation,
temperature=temperature,
)
2022-06-15 10:12:07 +00:00
except ElgatoError as error:
raise HomeAssistantError(
"An error occurred while updating the Elgato Light"
) from error
finally:
await self.coordinator.async_refresh()
async def async_identify(self) -> None:
"""Identify the light, will make it blink."""
try:
await self.coordinator.client.identify()
2022-06-15 10:12:07 +00:00
except ElgatoError as error:
raise HomeAssistantError(
"An error occurred while identifying the Elgato Light"
) from error