|
| 1 | +"""Select platform for NetDaemon.""" |
| 2 | +from typing import TYPE_CHECKING |
| 3 | + |
| 4 | +from homeassistant.components.select import SelectEntity |
| 5 | + |
| 6 | +from .const import ( |
| 7 | + ATTR_CLIENT, |
| 8 | + ATTR_COORDINATOR, |
| 9 | + ATTR_ENTITY_ID, |
| 10 | + ATTR_STATE, |
| 11 | + ATTR_OPTIONS, |
| 12 | + DOMAIN, |
| 13 | + LOGGER, |
| 14 | + PLATFORM_SELECT, |
| 15 | +) |
| 16 | +from .entity import NetDaemonEntity |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from homeassistant.config_entries import ConfigEntry |
| 20 | + from homeassistant.core import HomeAssistant |
| 21 | + from homeassistant.helpers.update_coordinator import DataUpdateCoordinator |
| 22 | + |
| 23 | + from .client import NetDaemonClient |
| 24 | + |
| 25 | +async def async_setup_entry( |
| 26 | + hass: "HomeAssistant", _config_entry: "ConfigEntry", async_add_devices |
| 27 | +) -> None: |
| 28 | + """Setup select platform.""" |
| 29 | + client: "NetDaemonClient" = hass.data[DOMAIN][ATTR_CLIENT] |
| 30 | + coordinator: "DataUpdateCoordinator" = hass.data[DOMAIN][ATTR_COORDINATOR] |
| 31 | + |
| 32 | + selects = [] |
| 33 | + for entity in client.entities: |
| 34 | + if entity.split(".")[0] == PLATFORM_SELECT: |
| 35 | + LOGGER.debug("Adding %s", entity) |
| 36 | + selects.append( |
| 37 | + NetDaemonSelect(coordinator, entity.split(".")[1]) |
| 38 | + ) |
| 39 | + |
| 40 | + if selects: |
| 41 | + async_add_devices(selects) |
| 42 | + |
| 43 | + |
| 44 | +class NetDaemonSelect(NetDaemonEntity, SelectEntity): |
| 45 | + """NetDaemon select class.""" |
| 46 | + |
| 47 | + @property |
| 48 | + def current_option(self): |
| 49 | + """Return the state of the select.""" |
| 50 | + if not self.entity_id: |
| 51 | + return None |
| 52 | + state = str(self._coordinator.data[self.entity_id][ATTR_STATE]) |
| 53 | + return state |
| 54 | + |
| 55 | + @property |
| 56 | + def options(self): |
| 57 | + """Return the list of available options.""" |
| 58 | + if not self.entity_id: |
| 59 | + return None |
| 60 | + return self._coordinator.data[self.entity_id][ATTR_OPTIONS] |
| 61 | + |
| 62 | + async def async_select_option(self, option: str) -> None: |
| 63 | + """Change the selected option.""" |
| 64 | + await self.hass.data[DOMAIN][ATTR_CLIENT].entity_update( |
| 65 | + {ATTR_ENTITY_ID: self.entity_id, ATTR_STATE: option} |
| 66 | + ) |
| 67 | + self.async_write_ha_state() |
0 commit comments