hass-core/homeassistant/components/abode/sensor.py
Erik Montnemery 4e07ab1b32
Move temperature conversions to sensor base class (1/8) (#48261)
* Move temperature conversions to entity base class (1/8)

* Update integrations a-c

* Leave old temperature conversion until all integrations are migrated

* tweak

* Use contextlib.suppress

* Remove the MeasurableUnitEntity mixin

* Address comments, add tests

* Fix f-string

* Drop deprecation warning from base entity class

* Update with _attr-shorthand

* Fix rebase mistakes

* Fix additional rebase mistakes

* Only report temperature conversion once

* Fix additional rebase mistakes

* Format homeassistant/components/bbox/sensor.py

* Fix check for overidden _attr_state

* Remove test workarounds from implementation

* Remove useless None-check

* Tweaks

* Migrate new sensors a-c

* Update climacell

* Push deprecation of temperature conversion forward

* Override __repr__ in SensorEntity

* Include native_value in SensorEntity attributes

* Pylint

* Black

* Black

* Fix rebase mistakes

* black

* Fix rebase mistakes

* Revert changes in august/sensor.py

* Revert handling of unit converted restored state

* Apply code review suggestion

* Fix arlo test
2021-08-11 10:45:05 +02:00

78 lines
2.6 KiB
Python

"""Support for Abode Security System sensors."""
from __future__ import annotations
import abodepy.helpers.constants as CONST
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import (
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_TEMPERATURE,
)
from . import AbodeDevice
from .const import DOMAIN
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key=CONST.TEMP_STATUS_KEY,
name="Temperature",
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):
"""Set up Abode sensor devices."""
data = hass.data[DOMAIN]
entities = []
for device in data.abode.get_devices(generic_type=CONST.TYPE_SENSOR):
conditions = device.get_value(CONST.STATUSES_KEY)
entities.extend(
[
AbodeSensor(data, device, description)
for description in SENSOR_TYPES
if description.key in conditions
]
)
async_add_entities(entities)
class AbodeSensor(AbodeDevice, SensorEntity):
"""A sensor implementation for Abode devices."""
def __init__(self, data, device, description: SensorEntityDescription):
"""Initialize a sensor for an Abode device."""
super().__init__(data, device)
self.entity_description = description
self._attr_name = f"{device.name} {description.name}"
self._attr_unique_id = f"{device.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 = device.lux_unit
@property
def native_value(self):
"""Return the state of the sensor."""
if self.entity_description.key == CONST.TEMP_STATUS_KEY:
return self._device.temp
if self.entity_description.key == CONST.HUMI_STATUS_KEY:
return self._device.humidity
if self.entity_description.key == CONST.LUX_STATUS_KEY:
return self._device.lux