|
| 1 | +"""Component to interface with binary sensors.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import logging |
| 5 | + |
| 6 | +from homeassistant.components.button import ButtonEntity |
| 7 | +from homeassistant.config_entries import ConfigEntry |
| 8 | +from homeassistant.core import callback |
| 9 | +from homeassistant.core import HomeAssistant |
| 10 | +from homeassistant.helpers.entity import EntityCategory |
| 11 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 12 | +from homeassistant.helpers.update_coordinator import CoordinatorEntity |
| 13 | + |
| 14 | +from .const import COORDINATOR |
| 15 | +from .const import DAIKIN_DEVICES |
| 16 | +from .const import DOMAIN as DAIKIN_DOMAIN |
| 17 | + |
| 18 | +_LOGGER = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +async def async_setup_entry( |
| 22 | + hass: HomeAssistant, |
| 23 | + config_entry: ConfigEntry, |
| 24 | + async_add_entities: AddEntitiesCallback, |
| 25 | +) -> None: |
| 26 | + entities = [] |
| 27 | + |
| 28 | + coordinator = hass.data[DAIKIN_DOMAIN][COORDINATOR] |
| 29 | + |
| 30 | + for dev_id, device in hass.data[DAIKIN_DOMAIN][DAIKIN_DEVICES].items(): |
| 31 | + entities.append(DaikinRefreshButton(device, config_entry, coordinator)) |
| 32 | + |
| 33 | + if entities: |
| 34 | + async_add_entities(entities) |
| 35 | + |
| 36 | + |
| 37 | +class DaikinRefreshButton(CoordinatorEntity, ButtonEntity): |
| 38 | + """Button to request an immediate device data update.""" |
| 39 | + |
| 40 | + def __init__(self, device, config_entry, coordinator): |
| 41 | + super().__init__(coordinator) |
| 42 | + self._device = device |
| 43 | + self._attr_unique_id = f"{self._device.id}_refresh" |
| 44 | + self._attr_entity_category = EntityCategory.CONFIG |
| 45 | + self._attr_icon = "mdi:refresh" |
| 46 | + self._attr_name = "Refresh" |
| 47 | + self._attr_device_info = self._device.device_info() |
| 48 | + self._attr_has_entity_name = True |
| 49 | + self._config_entry = config_entry |
| 50 | + |
| 51 | + _LOGGER.info("Device '%s' has refresh button", self._device.name) |
| 52 | + |
| 53 | + @property |
| 54 | + def available(self) -> bool: |
| 55 | + return self._device.available |
| 56 | + |
| 57 | + @callback |
| 58 | + def _handle_coordinator_update(self) -> None: |
| 59 | + self.async_write_ha_state() |
| 60 | + |
| 61 | + async def async_press(self) -> None: |
| 62 | + await self.coordinator._async_update_data() |
| 63 | + self.coordinator.async_update_listeners() |
0 commit comments