"""Selects: положения заслонок (af_*_direction, число позиций из num_dir).""" from __future__ import annotations from homeassistant.components.select import SelectEntity from pyfglair import Prop from pyfglair.templates import prop_info from .coordinator import FglairCoordinator, FglairRuntime from .entity import FglairEntity class FglairDirectionSelect(FglairEntity, SelectEntity): def __init__( self, coordinator: FglairCoordinator, prop: Prop, num_dir_prop: Prop, key: str, ) -> None: super().__init__(coordinator, prop.name.lower(), key) self._prop = prop self._num_dir_prop = num_dir_prop @property def options(self) -> list[str]: num = self.raw_value(self._num_dir_prop) if num is None or num <= 0 or num > 15: return ["0"] return [str(value) for value in range(num)] @property def current_option(self) -> str | None: value = self.raw_value(self._prop) if value is None: return None text = str(value) return text if text in self.options else self.options[0] async def async_select_option(self, option: str) -> None: await self.coordinator.async_write([(self._prop, int(option))]) async def async_setup_entry(hass, entry, async_add_entities) -> None: runtime: FglairRuntime = entry.runtime_data coordinator = runtime.coordinator template = runtime.client.template entities = [] if ( prop_info(template, Prop.AF_VERTICAL_DIRECTION) is not None and prop_info(template, Prop.AF_VERTICAL_NUM_DIR) is not None ): entities.append( FglairDirectionSelect( coordinator, Prop.AF_VERTICAL_DIRECTION, Prop.AF_VERTICAL_NUM_DIR, "af_vertical_direction", ) ) if ( prop_info(template, Prop.AF_HORIZONTAL_DIRECTION) is not None and prop_info(template, Prop.AF_HORIZONTAL_NUM_DIR) is not None ): entities.append( FglairDirectionSelect( coordinator, Prop.AF_HORIZONTAL_DIRECTION, Prop.AF_HORIZONTAL_NUM_DIR, "af_horizontal_direction", ) ) async_add_entities(entities)