Use functions to get value and unit in Abode (#99084)

This commit is contained in:
Joost Lekkerkerker 2023-08-29 17:37:26 +02:00 committed by GitHub
parent 10c53dd284
commit b9fd2ee3b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,8 @@
"""Support for Abode Security System sensors.""" """Support for Abode Security System sensors."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import cast from typing import cast
from jaraco.abode.devices.sensor import Sensor as AbodeSense from jaraco.abode.devices.sensor import Sensor as AbodeSense
@ -19,18 +21,38 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import AbodeDevice, AbodeSystem from . import AbodeDevice, AbodeSystem
from .const import DOMAIN from .const import DOMAIN
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( @dataclass
class AbodeSensorDescriptionMixin:
"""Mixin for Abode sensor."""
value_fn: Callable[[AbodeSense], float]
native_unit_of_measurement_fn: Callable[[AbodeSense], str]
@dataclass
class AbodeSensorDescription(SensorEntityDescription, AbodeSensorDescriptionMixin):
"""Class describing Abode sensor entities."""
SENSOR_TYPES: tuple[AbodeSensorDescription, ...] = (
AbodeSensorDescription(
key=CONST.TEMP_STATUS_KEY, key=CONST.TEMP_STATUS_KEY,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement_fn=lambda device: device.temp_unit,
value_fn=lambda device: cast(float, device.temp),
), ),
SensorEntityDescription( AbodeSensorDescription(
key=CONST.HUMI_STATUS_KEY, key=CONST.HUMI_STATUS_KEY,
device_class=SensorDeviceClass.HUMIDITY, device_class=SensorDeviceClass.HUMIDITY,
native_unit_of_measurement_fn=lambda device: device.humidity_unit,
value_fn=lambda device: cast(float, device.humidity),
), ),
SensorEntityDescription( AbodeSensorDescription(
key=CONST.LUX_STATUS_KEY, key=CONST.LUX_STATUS_KEY,
device_class=SensorDeviceClass.ILLUMINANCE, device_class=SensorDeviceClass.ILLUMINANCE,
native_unit_of_measurement_fn=lambda _: LIGHT_LUX,
value_fn=lambda device: cast(float, device.lux),
), ),
) )
@ -52,32 +74,26 @@ async def async_setup_entry(
class AbodeSensor(AbodeDevice, SensorEntity): class AbodeSensor(AbodeDevice, SensorEntity):
"""A sensor implementation for Abode devices.""" """A sensor implementation for Abode devices."""
entity_description: AbodeSensorDescription
_device: AbodeSense _device: AbodeSense
def __init__( def __init__(
self, self,
data: AbodeSystem, data: AbodeSystem,
device: AbodeSense, device: AbodeSense,
description: SensorEntityDescription, description: AbodeSensorDescription,
) -> None: ) -> None:
"""Initialize a sensor for an Abode device.""" """Initialize a sensor for an Abode device."""
super().__init__(data, device) super().__init__(data, device)
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{device.uuid}-{description.key}" self._attr_unique_id = f"{device.uuid}-{description.key}"
if description.key == CONST.TEMP_STATUS_KEY:
self._attr_native_unit_of_measurement = device.temp_unit
elif description.key == CONST.HUMI_STATUS_KEY:
self._attr_native_unit_of_measurement = device.humidity_unit
elif description.key == CONST.LUX_STATUS_KEY:
self._attr_native_unit_of_measurement = LIGHT_LUX
@property @property
def native_value(self) -> float | None: def native_value(self) -> float:
"""Return the state of the sensor.""" """Return the state of the sensor."""
if self.entity_description.key == CONST.TEMP_STATUS_KEY: return self.entity_description.value_fn(self._device)
return cast(float, self._device.temp)
if self.entity_description.key == CONST.HUMI_STATUS_KEY: @property
return cast(float, self._device.humidity) def native_unit_of_measurement(self) -> str:
if self.entity_description.key == CONST.LUX_STATUS_KEY: """Return the native unit of measurement."""
return cast(float, self._device.lux) return self.entity_description.native_unit_of_measurement_fn(self._device)
return None