|
| 1 | +from homeassistant.components.device_tracker.const import CONF_SCAN_INTERVAL |
| 2 | +from homeassistant.components.select import SelectEntity |
| 3 | +from homeassistant.config_entries import ConfigEntry |
| 4 | +from homeassistant.core import HomeAssistant |
| 5 | +from homeassistant.helpers.entity import DeviceInfo |
| 6 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 7 | + |
| 8 | +from .const import COORDINATOR_DECOS_KEY |
| 9 | +from .const import DOMAIN |
| 10 | +from .device import create_device_info |
| 11 | + |
| 12 | +POLLING_INTERVAL_OPTIONS = ["10", "30", "60", "120"] |
| 13 | + |
| 14 | + |
| 15 | +async def async_setup_entry( |
| 16 | + hass: HomeAssistant, |
| 17 | + config_entry: ConfigEntry, |
| 18 | + async_add_entities: AddEntitiesCallback, |
| 19 | +): |
| 20 | + """Set up select entities.""" |
| 21 | + coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR_DECOS_KEY] |
| 22 | + async_add_entities([DecoPollingIntervalSelect(hass, config_entry, coordinator)]) |
| 23 | + |
| 24 | + |
| 25 | +class DecoPollingIntervalSelect(SelectEntity): |
| 26 | + """Select entity to control Deco polling interval.""" |
| 27 | + |
| 28 | + _attr_has_entity_name = True |
| 29 | + _attr_name = "Polling interval" |
| 30 | + _attr_icon = "mdi:timer-cog" |
| 31 | + _attr_options = POLLING_INTERVAL_OPTIONS |
| 32 | + |
| 33 | + def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, coordinator): |
| 34 | + self.hass = hass |
| 35 | + self.config_entry = config_entry |
| 36 | + self.coordinator = coordinator |
| 37 | + self._attr_unique_id = f"{config_entry.entry_id}_polling_interval" |
| 38 | + |
| 39 | + @property |
| 40 | + def current_option(self) -> str: |
| 41 | + """Return current polling interval as string.""" |
| 42 | + value = self.config_entry.data.get(CONF_SCAN_INTERVAL, 30) |
| 43 | + return str(value) |
| 44 | + |
| 45 | + @property |
| 46 | + def device_info(self) -> DeviceInfo | None: |
| 47 | + """Attach select to the master Deco device.""" |
| 48 | + master_deco = self.coordinator.data.master_deco |
| 49 | + if master_deco is None: |
| 50 | + return None |
| 51 | + return create_device_info(master_deco, master_deco) |
| 52 | + |
| 53 | + async def async_select_option(self, option: str) -> None: |
| 54 | + """Change polling interval.""" |
| 55 | + new_value = int(option) |
| 56 | + |
| 57 | + data = dict(self.config_entry.data) |
| 58 | + data[CONF_SCAN_INTERVAL] = new_value |
| 59 | + |
| 60 | + self.hass.config_entries.async_update_entry( |
| 61 | + self.config_entry, |
| 62 | + data=data, |
| 63 | + ) |
| 64 | + |
| 65 | + await self.hass.config_entries.async_reload(self.config_entry.entry_id) |
0 commit comments