22
33from __future__ import annotations
44
5+ from collections .abc import Callable
6+ from dataclasses import dataclass
57import logging
68
79from zcc import ControlPoint
1517from homeassistant .const import PERCENTAGE , EntityCategory , UnitOfTemperature
1618from homeassistant .core import HomeAssistant
1719from homeassistant .helpers .entity_platform import AddConfigEntryEntitiesCallback
20+ from homeassistant .helpers .typing import StateType
1821
1922from . import ZimiConfigEntry
2023from .entity import ZimiEntity
2124
22- SENSOR_KEY_DOOR_TEMP = "door_temperature"
23- SENSOR_KEY_GARAGE_BATTERY = "garage_battery"
24- SENSOR_KEY_GARAGE_HUMDITY = "garage_humidty"
25- SENSOR_KEY_GARAGE_TEMP = "garage_temperature"
2625
27- GARAGE_SENSOR_DESCRIPTIONS = (
28- SensorEntityDescription (
29- key = SENSOR_KEY_DOOR_TEMP ,
30- name = "Outside temperature" ,
26+ @dataclass (frozen = True , kw_only = True )
27+ class ZimiSensorEntityDescription (SensorEntityDescription ):
28+ """Class describing Zimi sensor entities."""
29+
30+ value_fn : Callable [[ControlPointDevice ], StateType ]
31+
32+
33+ GARAGE_SENSOR_DESCRIPTIONS : tuple [ZimiSensorEntityDescription , ...] = (
34+ ZimiSensorEntityDescription (
35+ key = "door_temperature" ,
36+ translation_key = "door_temperature" ,
3137 native_unit_of_measurement = UnitOfTemperature .CELSIUS ,
3238 device_class = SensorDeviceClass .TEMPERATURE ,
39+ value_fn = lambda device : device .door_temp ,
3340 ),
34- SensorEntityDescription (
35- key = SENSOR_KEY_GARAGE_BATTERY ,
36- name = "Battery Level" ,
41+ ZimiSensorEntityDescription (
42+ key = "garage_battery" ,
3743 native_unit_of_measurement = PERCENTAGE ,
3844 entity_category = EntityCategory .DIAGNOSTIC ,
3945 device_class = SensorDeviceClass .BATTERY ,
46+ value_fn = lambda device : device .battery_level ,
4047 ),
41- SensorEntityDescription (
42- key = SENSOR_KEY_GARAGE_TEMP ,
43- name = "Garage temperature" ,
48+ ZimiSensorEntityDescription (
49+ key = "garage_temperature" ,
4450 native_unit_of_measurement = UnitOfTemperature .CELSIUS ,
4551 device_class = SensorDeviceClass .TEMPERATURE ,
52+ value_fn = lambda device : device .garage_temp ,
4653 ),
47- SensorEntityDescription (
48- key = SENSOR_KEY_GARAGE_HUMDITY ,
49- name = "Garage humidity" ,
54+ ZimiSensorEntityDescription (
55+ key = "garage_humidty" ,
5056 native_unit_of_measurement = PERCENTAGE ,
5157 device_class = SensorDeviceClass .HUMIDITY ,
58+ value_fn = lambda device : device .garage_humidity ,
5259 ),
5360)
5461
@@ -62,50 +69,35 @@ async def async_setup_entry(
6269) -> None :
6370 """Set up the Zimi Sensor platform."""
6471
65- api : ControlPoint = config_entry .runtime_data
72+ api = config_entry .runtime_data
6673
67- for description in GARAGE_SENSOR_DESCRIPTIONS :
68- sensors : list [ZimiSensor ] = [
69- ZimiSensor (device , description , api ) for device in api .sensors
70- ]
71-
72- async_add_entities (sensors )
74+ async_add_entities (
75+ ZimiSensor (device , description , api )
76+ for device in api .sensors
77+ for description in GARAGE_SENSOR_DESCRIPTIONS
78+ )
7379
7480
7581class ZimiSensor (ZimiEntity , SensorEntity ):
7682 """Representation of a Zimi sensor."""
7783
84+ entity_description : ZimiSensorEntityDescription
85+
7886 def __init__ (
7987 self ,
8088 device : ControlPointDevice ,
81- description : SensorEntityDescription ,
89+ description : ZimiSensorEntityDescription ,
8290 api : ControlPoint ,
8391 ) -> None :
8492 """Initialize an ZimiSensor with specified type."""
8593
86- super ().__init__ (device , api )
94+ super ().__init__ (device , api , use_device_name = False )
8795
8896 self .entity_description = description
8997 self ._attr_unique_id = device .identifier + "." + self .entity_description .key
9098
91- _LOGGER .debug (
92- "Initialising ZimiSensor %s in %s" , self ._device .name , self ._device .room
93- )
94-
9599 @property
96- def native_value (self ) -> float | None :
100+ def native_value (self ) -> StateType :
97101 """Return the state of the sensor."""
98102
99- if self .entity_description .key == SENSOR_KEY_DOOR_TEMP :
100- return self ._device .door_temp
101-
102- if self .entity_description .key == SENSOR_KEY_GARAGE_BATTERY :
103- return self ._device .battery_level
104-
105- if self .entity_description .key == SENSOR_KEY_GARAGE_HUMDITY :
106- return self ._device .garage_humidity
107-
108- if self .entity_description .key == SENSOR_KEY_GARAGE_TEMP :
109- return self ._device .garage_temp
110-
111- return None
103+ return self .entity_description .value_fn (self ._device )
0 commit comments