|
| 1 | +"""Sensor platform for Airobot thermostat.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Callable |
| 6 | +from dataclasses import dataclass |
| 7 | + |
| 8 | +from pyairobotrest.models import ThermostatStatus |
| 9 | + |
| 10 | +from homeassistant.components.sensor import ( |
| 11 | + SensorDeviceClass, |
| 12 | + SensorEntity, |
| 13 | + SensorEntityDescription, |
| 14 | + SensorStateClass, |
| 15 | +) |
| 16 | +from homeassistant.const import ( |
| 17 | + CONCENTRATION_PARTS_PER_MILLION, |
| 18 | + PERCENTAGE, |
| 19 | + EntityCategory, |
| 20 | + UnitOfTemperature, |
| 21 | + UnitOfTime, |
| 22 | +) |
| 23 | +from homeassistant.core import HomeAssistant |
| 24 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 25 | +from homeassistant.helpers.typing import StateType |
| 26 | + |
| 27 | +from . import AirobotConfigEntry |
| 28 | +from .entity import AirobotEntity |
| 29 | + |
| 30 | +PARALLEL_UPDATES = 0 |
| 31 | + |
| 32 | + |
| 33 | +@dataclass(frozen=True, kw_only=True) |
| 34 | +class AirobotSensorEntityDescription(SensorEntityDescription): |
| 35 | + """Describes Airobot sensor entity.""" |
| 36 | + |
| 37 | + value_fn: Callable[[ThermostatStatus], StateType] |
| 38 | + supported_fn: Callable[[ThermostatStatus], bool] = lambda _: True |
| 39 | + |
| 40 | + |
| 41 | +SENSOR_TYPES: tuple[AirobotSensorEntityDescription, ...] = ( |
| 42 | + AirobotSensorEntityDescription( |
| 43 | + key="air_temperature", |
| 44 | + translation_key="air_temperature", |
| 45 | + device_class=SensorDeviceClass.TEMPERATURE, |
| 46 | + native_unit_of_measurement=UnitOfTemperature.CELSIUS, |
| 47 | + state_class=SensorStateClass.MEASUREMENT, |
| 48 | + value_fn=lambda status: status.temp_air, |
| 49 | + ), |
| 50 | + AirobotSensorEntityDescription( |
| 51 | + key="humidity", |
| 52 | + device_class=SensorDeviceClass.HUMIDITY, |
| 53 | + native_unit_of_measurement=PERCENTAGE, |
| 54 | + state_class=SensorStateClass.MEASUREMENT, |
| 55 | + value_fn=lambda status: status.hum_air, |
| 56 | + ), |
| 57 | + AirobotSensorEntityDescription( |
| 58 | + key="floor_temperature", |
| 59 | + translation_key="floor_temperature", |
| 60 | + device_class=SensorDeviceClass.TEMPERATURE, |
| 61 | + native_unit_of_measurement=UnitOfTemperature.CELSIUS, |
| 62 | + state_class=SensorStateClass.MEASUREMENT, |
| 63 | + value_fn=lambda status: status.temp_floor, |
| 64 | + supported_fn=lambda status: status.has_floor_sensor, |
| 65 | + ), |
| 66 | + AirobotSensorEntityDescription( |
| 67 | + key="co2", |
| 68 | + device_class=SensorDeviceClass.CO2, |
| 69 | + native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, |
| 70 | + state_class=SensorStateClass.MEASUREMENT, |
| 71 | + value_fn=lambda status: status.co2, |
| 72 | + supported_fn=lambda status: status.has_co2_sensor, |
| 73 | + ), |
| 74 | + AirobotSensorEntityDescription( |
| 75 | + key="air_quality_index", |
| 76 | + device_class=SensorDeviceClass.AQI, |
| 77 | + state_class=SensorStateClass.MEASUREMENT, |
| 78 | + value_fn=lambda status: status.aqi, |
| 79 | + supported_fn=lambda status: status.has_co2_sensor, |
| 80 | + ), |
| 81 | + AirobotSensorEntityDescription( |
| 82 | + key="heating_uptime", |
| 83 | + translation_key="heating_uptime", |
| 84 | + device_class=SensorDeviceClass.DURATION, |
| 85 | + native_unit_of_measurement=UnitOfTime.SECONDS, |
| 86 | + suggested_unit_of_measurement=UnitOfTime.HOURS, |
| 87 | + state_class=SensorStateClass.TOTAL_INCREASING, |
| 88 | + entity_category=EntityCategory.DIAGNOSTIC, |
| 89 | + value_fn=lambda status: status.heating_uptime, |
| 90 | + entity_registry_enabled_default=False, |
| 91 | + ), |
| 92 | + AirobotSensorEntityDescription( |
| 93 | + key="errors", |
| 94 | + translation_key="errors", |
| 95 | + state_class=SensorStateClass.MEASUREMENT, |
| 96 | + entity_category=EntityCategory.DIAGNOSTIC, |
| 97 | + value_fn=lambda status: status.errors, |
| 98 | + ), |
| 99 | +) |
| 100 | + |
| 101 | + |
| 102 | +async def async_setup_entry( |
| 103 | + hass: HomeAssistant, |
| 104 | + entry: AirobotConfigEntry, |
| 105 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 106 | +) -> None: |
| 107 | + """Set up Airobot sensor platform.""" |
| 108 | + coordinator = entry.runtime_data |
| 109 | + async_add_entities( |
| 110 | + AirobotSensor(coordinator, description) |
| 111 | + for description in SENSOR_TYPES |
| 112 | + if description.supported_fn(coordinator.data.status) |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +class AirobotSensor(AirobotEntity, SensorEntity): |
| 117 | + """Representation of an Airobot sensor.""" |
| 118 | + |
| 119 | + entity_description: AirobotSensorEntityDescription |
| 120 | + |
| 121 | + def __init__( |
| 122 | + self, |
| 123 | + coordinator, |
| 124 | + description: AirobotSensorEntityDescription, |
| 125 | + ) -> None: |
| 126 | + """Initialize the sensor.""" |
| 127 | + super().__init__(coordinator) |
| 128 | + self.entity_description = description |
| 129 | + self._attr_unique_id = f"{coordinator.data.status.device_id}_{description.key}" |
| 130 | + |
| 131 | + @property |
| 132 | + def native_value(self) -> StateType: |
| 133 | + """Return the state of the sensor.""" |
| 134 | + return self.entity_description.value_fn(self.coordinator.data.status) |
0 commit comments