"""Select settings of a ninlight lamp: power-on behaviour and sACN.""" from __future__ import annotations from homeassistant.components.select import SelectEntity from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from .coordinator import NinlightConfigEntry, NinlightCoordinator from .entity import NinlightEntity, NinlightSacnEntity PARALLEL_UPDATES = 0 # POB_NAMES in the firmware's nl_api.c POWER_ON_OPTIONS = ["last", "on", "off"] # The two sACN settings the firmware takes as strings, same spelling as its API. ON_TIMEOUT_OPTIONS = ["release", "hold"] LAYOUT_OPTIONS = ["dimmer", "rgb"] async def async_setup_entry( hass: HomeAssistant, entry: NinlightConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: async_add_entities( [ NinlightPowerOnSelect(entry.runtime_data), NinlightSacnOnTimeoutSelect(entry.runtime_data), NinlightSacnLayoutSelect(entry.runtime_data), ] ) class NinlightPowerOnSelect(NinlightEntity, SelectEntity): """What the lamp does when power returns.""" _attr_entity_category = EntityCategory.CONFIG _attr_translation_key = "power_on_behaviour" _attr_options = POWER_ON_OPTIONS def __init__(self, coordinator: NinlightCoordinator) -> None: super().__init__(coordinator, "power_on_behaviour") @property def current_option(self) -> str | None: value = self.coordinator.data.state.get("power_on_behaviour") return value if value in POWER_ON_OPTIONS else None async def async_select_option(self, option: str) -> None: await self.coordinator.async_set_state(power_on_behaviour=option) class NinlightSacnOnTimeoutSelect(NinlightSacnEntity, SelectEntity): """What the lamp does 2.5 s after the last sACN packet.""" _attr_entity_category = EntityCategory.CONFIG _attr_translation_key = "sacn_on_timeout" _attr_options = ON_TIMEOUT_OPTIONS def __init__(self, coordinator: NinlightCoordinator) -> None: super().__init__(coordinator, "sacn_on_timeout") @property def current_option(self) -> str | None: value = self.sacn.get("on_timeout") return value if value in ON_TIMEOUT_OPTIONS else None async def async_select_option(self, option: str) -> None: await self.coordinator.async_set_sacn(on_timeout=option) class NinlightSacnLayoutSelect(NinlightSacnEntity, SelectEntity): """Which slots the lamp reads: with a master dimmer, or plain R,G,B.""" _attr_entity_category = EntityCategory.CONFIG _attr_translation_key = "sacn_layout" _attr_options = LAYOUT_OPTIONS def __init__(self, coordinator: NinlightCoordinator) -> None: super().__init__(coordinator, "sacn_layout") @property def current_option(self) -> str | None: value = self.sacn.get("layout") return value if value in LAYOUT_OPTIONS else None async def async_select_option(self, option: str) -> None: await self.coordinator.async_set_sacn(layout=option)