mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Use EntityDescription - dht (#55183)
This commit is contained in:
parent
ab6be2890a
commit
ed95bda781
@ -1,5 +1,6 @@
|
||||
"""Support for Adafruit DHT temperature and humidity sensor."""
|
||||
from contextlib import suppress
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
@ -7,7 +8,11 @@ import adafruit_dht
|
||||
import board
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_MONITORED_CONDITIONS,
|
||||
CONF_NAME,
|
||||
@ -33,10 +38,22 @@ MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
||||
|
||||
SENSOR_TEMPERATURE = "temperature"
|
||||
SENSOR_HUMIDITY = "humidity"
|
||||
SENSOR_TYPES = {
|
||||
SENSOR_TEMPERATURE: ["Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
|
||||
SENSOR_HUMIDITY: ["Humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
}
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
key=SENSOR_TEMPERATURE,
|
||||
name="Temperature",
|
||||
native_unit_of_measurement=TEMP_CELSIUS,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key=SENSOR_HUMIDITY,
|
||||
name="Humidity",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=DEVICE_CLASS_HUMIDITY,
|
||||
),
|
||||
)
|
||||
|
||||
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
||||
|
||||
|
||||
def validate_pin_input(value):
|
||||
@ -53,7 +70,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
vol.Required(CONF_SENSOR): cv.string,
|
||||
vol.Required(CONF_PIN): vol.All(cv.string, validate_pin_input),
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
|
||||
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
||||
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
||||
),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_TEMPERATURE_OFFSET, default=0): vol.All(
|
||||
@ -84,21 +101,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
return False
|
||||
|
||||
data = DHTClient(sensor, pin, name)
|
||||
dev = []
|
||||
|
||||
with suppress(KeyError):
|
||||
for variable in config[CONF_MONITORED_CONDITIONS]:
|
||||
dev.append(
|
||||
DHTSensor(
|
||||
data,
|
||||
variable,
|
||||
name,
|
||||
temperature_offset,
|
||||
humidity_offset,
|
||||
)
|
||||
)
|
||||
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
||||
entities = [
|
||||
DHTSensor(data, name, temperature_offset, humidity_offset, description)
|
||||
for description in SENSOR_TYPES
|
||||
if description.key in monitored_conditions
|
||||
]
|
||||
|
||||
add_entities(dev, True)
|
||||
add_entities(entities, True)
|
||||
|
||||
|
||||
class DHTSensor(SensorEntity):
|
||||
@ -107,36 +118,18 @@ class DHTSensor(SensorEntity):
|
||||
def __init__(
|
||||
self,
|
||||
dht_client,
|
||||
sensor_type,
|
||||
name,
|
||||
temperature_offset,
|
||||
humidity_offset,
|
||||
description: SensorEntityDescription,
|
||||
):
|
||||
"""Initialize the sensor."""
|
||||
self.client_name = name
|
||||
self._name = SENSOR_TYPES[sensor_type][0]
|
||||
self.entity_description = description
|
||||
self.dht_client = dht_client
|
||||
self.type = sensor_type
|
||||
self.temperature_offset = temperature_offset
|
||||
self.humidity_offset = humidity_offset
|
||||
self._state = None
|
||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
||||
self._attr_device_class = SENSOR_TYPES[sensor_type][2]
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return f"{self.client_name} {self._name}"
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self):
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return self._unit_of_measurement
|
||||
self._attr_name = f"{name} {description.name}"
|
||||
|
||||
def update(self):
|
||||
"""Get the latest data from the DHT and updates the states."""
|
||||
@ -145,7 +138,8 @@ class DHTSensor(SensorEntity):
|
||||
humidity_offset = self.humidity_offset
|
||||
data = self.dht_client.data
|
||||
|
||||
if self.type == SENSOR_TEMPERATURE and SENSOR_TEMPERATURE in data:
|
||||
sensor_type = self.entity_description.key
|
||||
if sensor_type == SENSOR_TEMPERATURE and sensor_type in data:
|
||||
temperature = data[SENSOR_TEMPERATURE]
|
||||
_LOGGER.debug(
|
||||
"Temperature %.1f \u00b0C + offset %.1f",
|
||||
@ -153,12 +147,12 @@ class DHTSensor(SensorEntity):
|
||||
temperature_offset,
|
||||
)
|
||||
if -20 <= temperature < 80:
|
||||
self._state = round(temperature + temperature_offset, 1)
|
||||
elif self.type == SENSOR_HUMIDITY and SENSOR_HUMIDITY in data:
|
||||
self._attr_native_value = round(temperature + temperature_offset, 1)
|
||||
elif sensor_type == SENSOR_HUMIDITY and sensor_type in data:
|
||||
humidity = data[SENSOR_HUMIDITY]
|
||||
_LOGGER.debug("Humidity %.1f%% + offset %.1f", humidity, humidity_offset)
|
||||
if 0 <= humidity <= 100:
|
||||
self._state = round(humidity + humidity_offset, 1)
|
||||
self._attr_native_value = round(humidity + humidity_offset, 1)
|
||||
|
||||
|
||||
class DHTClient:
|
||||
|
Loading…
x
Reference in New Issue
Block a user