|
| 1 | +"""Select platform for mill.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from homeassistant.components.select import SelectEntity, SelectEntityDescription |
| 5 | + |
| 6 | +from .const import DOMAIN, LOGGER |
| 7 | +from .coordinator import MillDataUpdateCoordinator |
| 8 | +from .entity import MillEntity |
| 9 | + |
| 10 | +ENTITY_DESCRIPTIONS = ( |
| 11 | + SelectEntityDescription( |
| 12 | + key="lidLockSetting", |
| 13 | + name="Lid Lock Setting", |
| 14 | + icon="mdi:lock-outline", |
| 15 | + options=["AlwaysLocked", "AlwaysUnlocked", "LockedWhenHot"], |
| 16 | + ), |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +async def async_setup_entry(hass, entry, async_add_devices): |
| 21 | + """Set up the select platform.""" |
| 22 | + coordinator = hass.data[DOMAIN][entry.entry_id] |
| 23 | + entities = [] |
| 24 | + |
| 25 | + for device in coordinator.data: |
| 26 | + for entity_description in ENTITY_DESCRIPTIONS: |
| 27 | + # Only add lid lock setting if the device supports it |
| 28 | + if coordinator.data[device].get(entity_description.key) is not None: |
| 29 | + entities.append( |
| 30 | + MillSelect( |
| 31 | + coordinator=coordinator, |
| 32 | + entity_description=entity_description, |
| 33 | + device=device |
| 34 | + ) |
| 35 | + ) |
| 36 | + |
| 37 | + async_add_devices(entities) |
| 38 | + |
| 39 | + |
| 40 | +class MillSelect(MillEntity, SelectEntity): |
| 41 | + """Mill Select class.""" |
| 42 | + |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + coordinator: MillDataUpdateCoordinator, |
| 46 | + entity_description: SelectEntityDescription, |
| 47 | + device, |
| 48 | + ) -> None: |
| 49 | + """Initialize the select class.""" |
| 50 | + super().__init__(coordinator, entity_description, device) |
| 51 | + self.entity_description = entity_description |
| 52 | + self.device = device |
| 53 | + self._attr_options = entity_description.options |
| 54 | + |
| 55 | + @property |
| 56 | + def current_option(self) -> str | None: |
| 57 | + """Return the selected entity option.""" |
| 58 | + desc = self.entity_description |
| 59 | + lid_lock_data = self.coordinator.data[self.device].get(desc.key) |
| 60 | + |
| 61 | + if isinstance(lid_lock_data, dict): |
| 62 | + desired = lid_lock_data.get('desired') |
| 63 | + reported = lid_lock_data.get('reported') |
| 64 | + # Use desired state if it differs from reported and is not None |
| 65 | + if desired != reported and desired is not None: |
| 66 | + current_setting = desired |
| 67 | + else: |
| 68 | + current_setting = reported |
| 69 | + else: |
| 70 | + current_setting = lid_lock_data |
| 71 | + |
| 72 | + # Make sure the current setting is in our options list |
| 73 | + if current_setting in self._attr_options: |
| 74 | + return current_setting |
| 75 | + else: |
| 76 | + LOGGER.warning(f"Unknown lid lock setting: {current_setting}") |
| 77 | + return None |
| 78 | + |
| 79 | + async def async_select_option(self, option: str) -> None: |
| 80 | + """Change the selected option.""" |
| 81 | + if option not in self._attr_options: |
| 82 | + LOGGER.error(f"Invalid option: {option}. Valid options: {self._attr_options}") |
| 83 | + return |
| 84 | + |
| 85 | + await self.coordinator.client.async_set_lock(self.device, option) |
| 86 | + await self.coordinator.async_request_refresh() |
| 87 | + |
| 88 | + async def delayed_refresh(): |
| 89 | + import asyncio |
| 90 | + await asyncio.sleep(5) # Wait 5 seconds |
| 91 | + await self.coordinator.async_request_refresh() |
| 92 | + await asyncio.sleep(10) # Wait another 10 seconds |
| 93 | + await self.coordinator.async_request_refresh() |
| 94 | + |
| 95 | + # Run delayed refreshes in background |
| 96 | + self.coordinator.hass.async_create_task(delayed_refresh()) |
0 commit comments