Files
core/homeassistant/components/led_infrared/light.py
T

208 lines
5.2 KiB
Python

"""Light platform for LED Infrared integration."""
from typing import Any, override
from homeassistant.components.infrared import InfraredEmitterConsumerEntity
from homeassistant.components.light import (
ATTR_EFFECT,
ColorMode,
LightEntity,
LightEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import CONF_DEVICE_TYPE, CONF_INFRARED_ENTITY_ID, LEDIrDeviceType
from .entity import LEDIrBaseEntity
PARALLEL_UPDATES = 1
SUPPORTED_EFFECTS = {
LEDIrDeviceType.GENERIC_24_KEY: ["flash", "strobe", "fade", "smooth"],
LEDIrDeviceType.GENERIC_13_KEY: [
"mode_1",
"mode_2",
"mode_3",
"mode_4",
"mode_5",
"mode_6",
"mode_7",
"mode_8",
],
LEDIrDeviceType.GENERIC_40_KEY: [
"auto",
"fade3",
"fade7",
"flash",
"jump3",
"jump7",
],
LEDIrDeviceType.GENERIC_44_KEY: [
"auto",
"fade3",
"fade7",
"flash",
"jump3",
"jump7",
"diy1",
"diy2",
"diy3",
"diy4",
"diy5",
"diy6",
],
}
SUPPORTED_COLORS = {
LEDIrDeviceType.GENERIC_24_KEY: [
"red",
"green",
"blue",
"white",
"tomato",
"light_green",
"sky_blue",
"orange_red",
"cyan",
"rebecca_purple",
"orange",
"turquoise",
"purple",
"yellow",
"dark_cyan",
"plum",
],
LEDIrDeviceType.GENERIC_40_KEY: [
"red",
"green",
"blue",
"white",
"tomato",
"light_green",
"deep_blue",
"floral_white",
"orange",
"turquoise",
"purple",
"lavender_blush",
"yellowish",
"cyan",
"magenta",
"ghost_white",
"yellow",
"aqua",
"pink",
"light_cyan",
],
LEDIrDeviceType.GENERIC_44_KEY: [
"red",
"green",
"blue",
"white",
"tomato",
"light_green",
"deep_blue",
"floral_white",
"orange",
"turquoise",
"purple",
"lavender_blush",
"yellowish",
"cyan",
"magenta",
"ghost_white",
"yellow",
"aqua",
"pink",
"light_cyan",
],
}
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up platform from config entry."""
if not (infrared_entity_id := entry.data.get(CONF_INFRARED_ENTITY_ID)):
return
async_add_entities(
[LEDIrLightEntity(entry, entry.data[CONF_DEVICE_TYPE], infrared_entity_id)]
)
class LEDIrLightEntity(LEDIrBaseEntity, InfraredEmitterConsumerEntity, LightEntity):
"""Represents a LED Infrared light entity."""
_attr_assumed_state = True
_attr_color_mode = ColorMode.ONOFF
_attr_effect_list: list[str]
_attr_has_entity_name = True
_attr_name = None
_attr_supported_color_modes = {ColorMode.ONOFF}
_attr_supported_features = LightEntityFeature.EFFECT
_attr_translation_key = "light"
def __init__(
self,
entry: ConfigEntry,
device_type: LEDIrDeviceType,
infrared_entity_id: str,
) -> None:
"""Initialize the entity."""
super().__init__(entry, device_type)
self._infrared_emitter_entity_id = infrared_entity_id
self._attr_unique_id = entry.entry_id
self._attr_effect_list = SUPPORTED_EFFECTS.get(
device_type, []
) + SUPPORTED_COLORS.get(device_type, [])
@override
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn device on."""
await self._send_command(self._codes.ON.to_command())
self._attr_is_on = True
effect: str | None = kwargs.get(ATTR_EFFECT)
if effect and effect in self._attr_effect_list:
await self._send_command(self._codes[effect.upper()].to_command())
self._attr_effect = effect
self.async_write_ha_state()
@override
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
await self._send_command(self._codes.OFF.to_command())
self._attr_is_on = False
self.async_write_ha_state()
@callback
def _async_handle_event(self, event_type: str) -> None:
"""Handle event."""
if event_type in ("on", "off"):
self._attr_is_on = event_type == "on"
elif event_type in self._attr_effect_list:
self._attr_is_on = True
self._attr_effect = event_type
self.async_write_ha_state()
@override
async def async_added_to_hass(self) -> None:
"""Register event callback."""
self.async_on_remove(
async_dispatcher_connect(
self.hass, self._entry.entry_id, self._async_handle_event
)
)
await super().async_added_to_hass()