mirror of
https://github.com/home-assistant/core.git
synced 2025-08-13 17:45:19 +02:00
value_fn and inline refactoring
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from zcc import ControlPoint
|
from zcc import ControlPoint
|
||||||
@@ -15,40 +17,48 @@ from homeassistant.components.sensor import (
|
|||||||
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature
|
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
from homeassistant.helpers.typing import StateType
|
||||||
|
|
||||||
from . import ZimiConfigEntry
|
from . import ZimiConfigEntry
|
||||||
from .entity import ZimiEntity
|
from .entity import ZimiEntity
|
||||||
|
|
||||||
SENSOR_KEY_DOOR_TEMP = "door_temperature"
|
|
||||||
SENSOR_KEY_GARAGE_BATTERY = "garage_battery"
|
|
||||||
SENSOR_KEY_GARAGE_HUMDITY = "garage_humidty"
|
|
||||||
SENSOR_KEY_GARAGE_TEMP = "garage_temperature"
|
|
||||||
|
|
||||||
GARAGE_SENSOR_DESCRIPTIONS = (
|
@dataclass(frozen=True, kw_only=True)
|
||||||
SensorEntityDescription(
|
class ZimiSensorEntityDescription(SensorEntityDescription):
|
||||||
key=SENSOR_KEY_DOOR_TEMP,
|
"""Class describing Zimi sensor entities."""
|
||||||
|
|
||||||
|
value_fn: Callable[[ControlPointDevice], StateType]
|
||||||
|
|
||||||
|
|
||||||
|
GARAGE_SENSOR_DESCRIPTIONS: tuple[ZimiSensorEntityDescription, ...] = (
|
||||||
|
ZimiSensorEntityDescription(
|
||||||
|
key="door_temperature",
|
||||||
name="Outside temperature",
|
name="Outside temperature",
|
||||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||||
device_class=SensorDeviceClass.TEMPERATURE,
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
|
value_fn=lambda device: device.door_temp,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
ZimiSensorEntityDescription(
|
||||||
key=SENSOR_KEY_GARAGE_BATTERY,
|
key="garage_battery",
|
||||||
name="Battery Level",
|
name="Battery Level",
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
device_class=SensorDeviceClass.BATTERY,
|
device_class=SensorDeviceClass.BATTERY,
|
||||||
|
value_fn=lambda device: device.battery_level,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
ZimiSensorEntityDescription(
|
||||||
key=SENSOR_KEY_GARAGE_TEMP,
|
key="garage_temperature",
|
||||||
name="Garage temperature",
|
name="Garage temperature",
|
||||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||||
device_class=SensorDeviceClass.TEMPERATURE,
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
|
value_fn=lambda device: device.garage_temp,
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
ZimiSensorEntityDescription(
|
||||||
key=SENSOR_KEY_GARAGE_HUMDITY,
|
key="garage_humidty",
|
||||||
name="Garage humidity",
|
name="Garage humidity",
|
||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
device_class=SensorDeviceClass.HUMIDITY,
|
device_class=SensorDeviceClass.HUMIDITY,
|
||||||
|
value_fn=lambda device: device.garage_humidity,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -64,16 +74,22 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
api = config_entry.runtime_data
|
api = config_entry.runtime_data
|
||||||
|
|
||||||
async_add_entities(ZimiSensor(device, description, api) for device in api.sensors for description in GARAGE_SENSOR_DESCRIPTIONS)
|
async_add_entities(
|
||||||
|
ZimiSensor(device, description, api)
|
||||||
|
for device in api.sensors
|
||||||
|
for description in GARAGE_SENSOR_DESCRIPTIONS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ZimiSensor(ZimiEntity, SensorEntity):
|
class ZimiSensor(ZimiEntity, SensorEntity):
|
||||||
"""Representation of a Zimi sensor."""
|
"""Representation of a Zimi sensor."""
|
||||||
|
|
||||||
|
entity_description: ZimiSensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
device: ControlPointDevice,
|
device: ControlPointDevice,
|
||||||
description: SensorEntityDescription,
|
description: ZimiSensorEntityDescription,
|
||||||
api: ControlPoint,
|
api: ControlPoint,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize an ZimiSensor with specified type."""
|
"""Initialize an ZimiSensor with specified type."""
|
||||||
@@ -84,19 +100,7 @@ class ZimiSensor(ZimiEntity, SensorEntity):
|
|||||||
self._attr_unique_id = device.identifier + "." + self.entity_description.key
|
self._attr_unique_id = device.identifier + "." + self.entity_description.key
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> float | None:
|
def native_value(self) -> str | int | float | None:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
|
|
||||||
if self.entity_description.key == SENSOR_KEY_DOOR_TEMP:
|
return self.entity_description.value_fn(self._device)
|
||||||
return self._device.door_temp
|
|
||||||
|
|
||||||
if self.entity_description.key == SENSOR_KEY_GARAGE_BATTERY:
|
|
||||||
return self._device.battery_level
|
|
||||||
|
|
||||||
if self.entity_description.key == SENSOR_KEY_GARAGE_HUMDITY:
|
|
||||||
return self._device.garage_humidity
|
|
||||||
|
|
||||||
if self.entity_description.key == SENSOR_KEY_GARAGE_TEMP:
|
|
||||||
return self._device.garage_temp
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
Reference in New Issue
Block a user