|
| 1 | +"""Sensor platform for Home Assistant Backup integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Callable |
| 6 | +from dataclasses import dataclass |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | +from homeassistant.components.sensor import ( |
| 10 | + SensorDeviceClass, |
| 11 | + SensorEntity, |
| 12 | + SensorEntityDescription, |
| 13 | +) |
| 14 | +from homeassistant.core import HomeAssistant |
| 15 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 16 | + |
| 17 | +from .coordinator import BackupConfigEntry, BackupCoordinatorData |
| 18 | +from .entity import BackupManagerEntity |
| 19 | +from .manager import BackupManagerState |
| 20 | + |
| 21 | + |
| 22 | +@dataclass(kw_only=True, frozen=True) |
| 23 | +class BackupSensorEntityDescription(SensorEntityDescription): |
| 24 | + """Description for Home Assistant Backup sensor entities.""" |
| 25 | + |
| 26 | + value_fn: Callable[[BackupCoordinatorData], str | datetime | None] |
| 27 | + |
| 28 | + |
| 29 | +BACKUP_MANAGER_DESCRIPTIONS = ( |
| 30 | + BackupSensorEntityDescription( |
| 31 | + key="backup_manager_state", |
| 32 | + translation_key="backup_manager_state", |
| 33 | + device_class=SensorDeviceClass.ENUM, |
| 34 | + options=[state.value for state in BackupManagerState], |
| 35 | + value_fn=lambda data: data.backup_manager_state, |
| 36 | + ), |
| 37 | + BackupSensorEntityDescription( |
| 38 | + key="next_scheduled_automatic_backup", |
| 39 | + translation_key="next_scheduled_automatic_backup", |
| 40 | + device_class=SensorDeviceClass.TIMESTAMP, |
| 41 | + value_fn=lambda data: data.next_scheduled_automatic_backup, |
| 42 | + ), |
| 43 | + BackupSensorEntityDescription( |
| 44 | + key="last_successful_automatic_backup", |
| 45 | + translation_key="last_successful_automatic_backup", |
| 46 | + device_class=SensorDeviceClass.TIMESTAMP, |
| 47 | + value_fn=lambda data: data.last_successful_automatic_backup, |
| 48 | + ), |
| 49 | +) |
| 50 | + |
| 51 | + |
| 52 | +async def async_setup_entry( |
| 53 | + hass: HomeAssistant, |
| 54 | + config_entry: BackupConfigEntry, |
| 55 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 56 | +) -> None: |
| 57 | + """Sensor set up for backup config entry.""" |
| 58 | + |
| 59 | + coordinator = config_entry.runtime_data |
| 60 | + |
| 61 | + async_add_entities( |
| 62 | + BackupManagerSensor(coordinator, description) |
| 63 | + for description in BACKUP_MANAGER_DESCRIPTIONS |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +class BackupManagerSensor(BackupManagerEntity, SensorEntity): |
| 68 | + """Sensor to track backup manager state.""" |
| 69 | + |
| 70 | + entity_description: BackupSensorEntityDescription |
| 71 | + |
| 72 | + @property |
| 73 | + def native_value(self) -> str | datetime | None: |
| 74 | + """Return native value of entity.""" |
| 75 | + return self.entity_description.value_fn(self.coordinator.data) |
0 commit comments