mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +00:00
Use EntityDescription - abode (#54321)
This commit is contained in:
parent
d79fc2c506
commit
4133cc05eb
@ -1,7 +1,9 @@
|
|||||||
"""Support for Abode Security System sensors."""
|
"""Support for Abode Security System sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import abodepy.helpers.constants as CONST
|
import abodepy.helpers.constants as CONST
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
DEVICE_CLASS_HUMIDITY,
|
DEVICE_CLASS_HUMIDITY,
|
||||||
DEVICE_CLASS_ILLUMINANCE,
|
DEVICE_CLASS_ILLUMINANCE,
|
||||||
@ -11,12 +13,23 @@ from homeassistant.const import (
|
|||||||
from . import AbodeDevice
|
from . import AbodeDevice
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
# Sensor types: Name, icon
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
SENSOR_TYPES = {
|
SensorEntityDescription(
|
||||||
CONST.TEMP_STATUS_KEY: ["Temperature", DEVICE_CLASS_TEMPERATURE],
|
key=CONST.TEMP_STATUS_KEY,
|
||||||
CONST.HUMI_STATUS_KEY: ["Humidity", DEVICE_CLASS_HUMIDITY],
|
name="Temperature",
|
||||||
CONST.LUX_STATUS_KEY: ["Lux", DEVICE_CLASS_ILLUMINANCE],
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
}
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=CONST.HUMI_STATUS_KEY,
|
||||||
|
name="Humidity",
|
||||||
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=CONST.LUX_STATUS_KEY,
|
||||||
|
name="Lux",
|
||||||
|
device_class=DEVICE_CLASS_ILLUMINANCE,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
@ -26,10 +39,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
for device in data.abode.get_devices(generic_type=CONST.TYPE_SENSOR):
|
for device in data.abode.get_devices(generic_type=CONST.TYPE_SENSOR):
|
||||||
for sensor_type in SENSOR_TYPES:
|
conditions = device.get_value(CONST.STATUSES_KEY)
|
||||||
if sensor_type not in device.get_value(CONST.STATUSES_KEY):
|
entities.extend(
|
||||||
continue
|
[
|
||||||
entities.append(AbodeSensor(data, device, sensor_type))
|
AbodeSensor(data, device, description)
|
||||||
|
for description in SENSOR_TYPES
|
||||||
|
if description.key in conditions
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
@ -37,26 +54,25 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
class AbodeSensor(AbodeDevice, SensorEntity):
|
class AbodeSensor(AbodeDevice, SensorEntity):
|
||||||
"""A sensor implementation for Abode devices."""
|
"""A sensor implementation for Abode devices."""
|
||||||
|
|
||||||
def __init__(self, data, device, sensor_type):
|
def __init__(self, data, device, description: SensorEntityDescription):
|
||||||
"""Initialize a sensor for an Abode device."""
|
"""Initialize a sensor for an Abode device."""
|
||||||
super().__init__(data, device)
|
super().__init__(data, device)
|
||||||
self._sensor_type = sensor_type
|
self.entity_description = description
|
||||||
self._attr_name = f"{device.name} {SENSOR_TYPES[sensor_type][0]}"
|
self._attr_name = f"{device.name} {description.name}"
|
||||||
self._attr_device_class = SENSOR_TYPES[self._sensor_type][1]
|
self._attr_unique_id = f"{device.device_uuid}-{description.key}"
|
||||||
self._attr_unique_id = f"{device.device_uuid}-{sensor_type}"
|
if description.key == CONST.TEMP_STATUS_KEY:
|
||||||
if self._sensor_type == CONST.TEMP_STATUS_KEY:
|
|
||||||
self._attr_unit_of_measurement = device.temp_unit
|
self._attr_unit_of_measurement = device.temp_unit
|
||||||
elif self._sensor_type == CONST.HUMI_STATUS_KEY:
|
elif description.key == CONST.HUMI_STATUS_KEY:
|
||||||
self._attr_unit_of_measurement = device.humidity_unit
|
self._attr_unit_of_measurement = device.humidity_unit
|
||||||
elif self._sensor_type == CONST.LUX_STATUS_KEY:
|
elif description.key == CONST.LUX_STATUS_KEY:
|
||||||
self._attr_unit_of_measurement = device.lux_unit
|
self._attr_unit_of_measurement = device.lux_unit
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
if self._sensor_type == CONST.TEMP_STATUS_KEY:
|
if self.entity_description.key == CONST.TEMP_STATUS_KEY:
|
||||||
return self._device.temp
|
return self._device.temp
|
||||||
if self._sensor_type == CONST.HUMI_STATUS_KEY:
|
if self.entity_description.key == CONST.HUMI_STATUS_KEY:
|
||||||
return self._device.humidity
|
return self._device.humidity
|
||||||
if self._sensor_type == CONST.LUX_STATUS_KEY:
|
if self.entity_description.key == CONST.LUX_STATUS_KEY:
|
||||||
return self._device.lux
|
return self._device.lux
|
||||||
|
Loading…
x
Reference in New Issue
Block a user