2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Abode Security System sensors."""
|
2021-08-09 21:40:57 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-08-29 15:37:26 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
2022-01-10 14:54:09 +00:00
|
|
|
from typing import cast
|
|
|
|
|
2023-01-24 11:44:38 +00:00
|
|
|
from jaraco.abode.devices.sensor import Sensor as AbodeSense
|
|
|
|
from jaraco.abode.helpers import constants as CONST
|
2019-10-12 20:02:12 +00:00
|
|
|
|
2021-12-08 18:58:01 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-12-24 13:06:14 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-08-30 13:33:38 +00:00
|
|
|
from homeassistant.const import LIGHT_LUX, PERCENTAGE, UnitOfTemperature
|
2021-12-24 13:06:14 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2017-10-07 08:25:53 +00:00
|
|
|
|
2022-01-10 14:54:09 +00:00
|
|
|
from . import AbodeDevice, AbodeSystem
|
2019-10-13 18:01:04 +00:00
|
|
|
from .const import DOMAIN
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2023-08-30 13:33:38 +00:00
|
|
|
ABODE_TEMPERATURE_UNIT_HA_UNIT = {
|
|
|
|
CONST.UNIT_FAHRENHEIT: UnitOfTemperature.FAHRENHEIT,
|
|
|
|
CONST.UNIT_CELSIUS: UnitOfTemperature.CELSIUS,
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:37:26 +00:00
|
|
|
|
2023-12-19 02:28:13 +00:00
|
|
|
@dataclass(frozen=True)
|
2023-08-29 15:37:26 +00:00
|
|
|
class AbodeSensorDescriptionMixin:
|
|
|
|
"""Mixin for Abode sensor."""
|
|
|
|
|
|
|
|
value_fn: Callable[[AbodeSense], float]
|
|
|
|
native_unit_of_measurement_fn: Callable[[AbodeSense], str]
|
|
|
|
|
|
|
|
|
2023-12-19 02:28:13 +00:00
|
|
|
@dataclass(frozen=True)
|
2023-08-29 15:37:26 +00:00
|
|
|
class AbodeSensorDescription(SensorEntityDescription, AbodeSensorDescriptionMixin):
|
|
|
|
"""Class describing Abode sensor entities."""
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_TYPES: tuple[AbodeSensorDescription, ...] = (
|
|
|
|
AbodeSensorDescription(
|
2021-08-09 21:40:57 +00:00
|
|
|
key=CONST.TEMP_STATUS_KEY,
|
2021-12-08 18:58:01 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2023-08-30 13:33:38 +00:00
|
|
|
native_unit_of_measurement_fn=lambda device: ABODE_TEMPERATURE_UNIT_HA_UNIT[
|
|
|
|
device.temp_unit
|
|
|
|
],
|
2023-08-29 15:37:26 +00:00
|
|
|
value_fn=lambda device: cast(float, device.temp),
|
2021-08-09 21:40:57 +00:00
|
|
|
),
|
2023-08-29 15:37:26 +00:00
|
|
|
AbodeSensorDescription(
|
2021-08-09 21:40:57 +00:00
|
|
|
key=CONST.HUMI_STATUS_KEY,
|
2021-12-08 18:58:01 +00:00
|
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
2023-08-30 13:33:38 +00:00
|
|
|
native_unit_of_measurement_fn=lambda _: PERCENTAGE,
|
2023-08-29 15:37:26 +00:00
|
|
|
value_fn=lambda device: cast(float, device.humidity),
|
2021-08-09 21:40:57 +00:00
|
|
|
),
|
2023-08-29 15:37:26 +00:00
|
|
|
AbodeSensorDescription(
|
2021-08-09 21:40:57 +00:00
|
|
|
key=CONST.LUX_STATUS_KEY,
|
2021-12-08 18:58:01 +00:00
|
|
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
2023-08-29 15:37:26 +00:00
|
|
|
native_unit_of_measurement_fn=lambda _: LIGHT_LUX,
|
|
|
|
value_fn=lambda device: cast(float, device.lux),
|
2021-08-09 21:40:57 +00:00
|
|
|
),
|
|
|
|
)
|
2017-10-07 08:25:53 +00:00
|
|
|
|
|
|
|
|
2021-12-24 13:06:14 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-10 14:54:09 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2021-12-24 13:06:14 +00:00
|
|
|
) -> None:
|
2019-11-09 06:35:45 +00:00
|
|
|
"""Set up Abode sensor devices."""
|
2022-01-10 14:54:09 +00:00
|
|
|
data: AbodeSystem = hass.data[DOMAIN]
|
2019-11-09 06:35:45 +00:00
|
|
|
|
2022-08-10 22:09:05 +00:00
|
|
|
async_add_entities(
|
|
|
|
AbodeSensor(data, device, description)
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
for device in data.abode.get_devices(generic_type=CONST.TYPE_SENSOR)
|
|
|
|
if description.key in device.get_value(CONST.STATUSES_KEY)
|
|
|
|
)
|
2017-10-07 08:25:53 +00:00
|
|
|
|
|
|
|
|
2021-03-22 19:05:13 +00:00
|
|
|
class AbodeSensor(AbodeDevice, SensorEntity):
|
2017-10-07 08:25:53 +00:00
|
|
|
"""A sensor implementation for Abode devices."""
|
|
|
|
|
2023-08-29 15:37:26 +00:00
|
|
|
entity_description: AbodeSensorDescription
|
2022-01-10 14:54:09 +00:00
|
|
|
_device: AbodeSense
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
data: AbodeSystem,
|
|
|
|
device: AbodeSense,
|
2023-08-29 15:37:26 +00:00
|
|
|
description: AbodeSensorDescription,
|
2022-01-10 14:54:09 +00:00
|
|
|
) -> None:
|
2017-10-07 08:25:53 +00:00
|
|
|
"""Initialize a sensor for an Abode device."""
|
|
|
|
super().__init__(data, device)
|
2021-08-09 21:40:57 +00:00
|
|
|
self.entity_description = description
|
2023-08-03 07:10:31 +00:00
|
|
|
self._attr_unique_id = f"{device.uuid}-{description.key}"
|
2019-11-04 20:49:11 +00:00
|
|
|
|
2017-10-07 08:25:53 +00:00
|
|
|
@property
|
2023-08-29 15:37:26 +00:00
|
|
|
def native_value(self) -> float:
|
2017-10-07 08:25:53 +00:00
|
|
|
"""Return the state of the sensor."""
|
2023-08-29 15:37:26 +00:00
|
|
|
return self.entity_description.value_fn(self._device)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_unit_of_measurement(self) -> str:
|
|
|
|
"""Return the native unit of measurement."""
|
|
|
|
return self.entity_description.native_unit_of_measurement_fn(self._device)
|