|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import date, datetime |
| 4 | + |
| 5 | +from homeassistant.components.date import DateEntity, ENTITY_ID_FORMAT |
| 6 | +from homeassistant.config_entries import ConfigEntry |
| 7 | +from homeassistant.core import HomeAssistant, callback |
| 8 | +from homeassistant.exceptions import ConfigEntryNotReady |
| 9 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 10 | + |
| 11 | +from .base import LuxtronikEntity |
| 12 | +from .common import get_sensor_data, key_exists |
| 13 | +from .const import ( |
| 14 | + CONF_COORDINATOR, |
| 15 | + CONF_HA_SENSOR_PREFIX, |
| 16 | + DOMAIN, |
| 17 | + LOGGER, |
| 18 | + DeviceKey, |
| 19 | +) |
| 20 | +from .coordinator import LuxtronikCoordinator, LuxtronikCoordinatorData |
| 21 | +from .date_entities_predefined import CALENDAR_ENTITIES |
| 22 | +from .model import LuxtronikDateEntityDescription |
| 23 | + |
| 24 | + |
| 25 | +async def async_setup_entry( |
| 26 | + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback |
| 27 | +) -> None: |
| 28 | + data = hass.data.get(DOMAIN, {}).get(entry.entry_id) |
| 29 | + if not data or CONF_COORDINATOR not in data: |
| 30 | + raise ConfigEntryNotReady |
| 31 | + |
| 32 | + coordinator: LuxtronikCoordinator = data[CONF_COORDINATOR] |
| 33 | + |
| 34 | + if not coordinator.last_update_success: |
| 35 | + raise ConfigEntryNotReady |
| 36 | + |
| 37 | + unavailable_keys = [ |
| 38 | + i.luxtronik_key |
| 39 | + for i in CALENDAR_ENTITIES |
| 40 | + if not key_exists(coordinator.data, i.luxtronik_key) |
| 41 | + ] |
| 42 | + if unavailable_keys: |
| 43 | + LOGGER.warning("Not present in Luxtronik data, skipping: %s", unavailable_keys) |
| 44 | + |
| 45 | + async_add_entities( |
| 46 | + [ |
| 47 | + LuxtronikDateEntity( |
| 48 | + hass, entry, coordinator, description, description.device_key |
| 49 | + ) |
| 50 | + for description in CALENDAR_ENTITIES |
| 51 | + if ( |
| 52 | + coordinator.entity_active(description) |
| 53 | + and key_exists(coordinator.data, description.luxtronik_key) |
| 54 | + ) |
| 55 | + ], |
| 56 | + True, |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +class LuxtronikDateEntity(LuxtronikEntity, DateEntity): |
| 61 | + """Luxtronik Date Entity that supports user-editable dates.""" |
| 62 | + |
| 63 | + entity_description: LuxtronikDateEntityDescription |
| 64 | + |
| 65 | + def __init__( |
| 66 | + self, |
| 67 | + hass: HomeAssistant, |
| 68 | + entry: ConfigEntry, |
| 69 | + coordinator: LuxtronikCoordinator, |
| 70 | + description: LuxtronikDateEntityDescription, |
| 71 | + device_info_ident: DeviceKey, |
| 72 | + ) -> None: |
| 73 | + super().__init__( |
| 74 | + coordinator=coordinator, |
| 75 | + description=description, |
| 76 | + device_info_ident=device_info_ident, |
| 77 | + ) |
| 78 | + |
| 79 | + prefix = entry.data[CONF_HA_SENSOR_PREFIX] |
| 80 | + self.entity_id = ENTITY_ID_FORMAT.format(f"{prefix}_{description.key}") |
| 81 | + self._attr_unique_id = self.entity_id |
| 82 | + |
| 83 | + @callback |
| 84 | + def _handle_coordinator_update( |
| 85 | + self, data: LuxtronikCoordinatorData | None = None |
| 86 | + ) -> None: |
| 87 | + data = self.coordinator.data if data is None else data |
| 88 | + if data is None: |
| 89 | + return |
| 90 | + |
| 91 | + value = get_sensor_data(data, self.entity_description.luxtronik_key.value) |
| 92 | + |
| 93 | + if isinstance(value, (int, float)): |
| 94 | + try: |
| 95 | + dt_value = datetime.fromtimestamp(value) |
| 96 | + self._attr_native_value = dt_value.date() |
| 97 | + except (ValueError, OSError): |
| 98 | + self._attr_native_value = None |
| 99 | + elif isinstance(value, date): |
| 100 | + self._attr_native_value = value |
| 101 | + else: |
| 102 | + self._attr_native_value = None |
| 103 | + |
| 104 | + self.async_write_ha_state() |
| 105 | + super()._handle_coordinator_update() |
| 106 | + |
| 107 | + async def async_set_value(self, value: date) -> None: |
| 108 | + """Handle user-set date from the UI.""" |
| 109 | + self._attr_native_value = value |
| 110 | + timestamp = int(datetime.combine(value, datetime.min.time()).timestamp()) |
| 111 | + await self.coordinator.async_write( |
| 112 | + self.entity_description.luxtronik_key.value.split(".")[1], timestamp |
| 113 | + ) |
| 114 | + self.async_write_ha_state() |
0 commit comments