mirror of
https://github.com/home-assistant/core.git
synced 2025-06-07 14:47:12 +00:00
Use EntityDescription - htu21d (#55186)
This commit is contained in:
parent
bf6d549910
commit
839b9563ad
@ -1,4 +1,6 @@
|
|||||||
"""Support for HTU21D temperature and humidity sensor."""
|
"""Support for HTU21D temperature and humidity sensor."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
@ -7,7 +9,11 @@ from i2csense.htu21d import HTU21D # pylint: disable=import-error
|
|||||||
import smbus
|
import smbus
|
||||||
import voluptuous as vol
|
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 (
|
from homeassistant.const import (
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
DEVICE_CLASS_HUMIDITY,
|
DEVICE_CLASS_HUMIDITY,
|
||||||
@ -30,6 +36,19 @@ DEFAULT_NAME = "HTU21D Sensor"
|
|||||||
SENSOR_TEMPERATURE = "temperature"
|
SENSOR_TEMPERATURE = "temperature"
|
||||||
SENSOR_HUMIDITY = "humidity"
|
SENSOR_HUMIDITY = "humidity"
|
||||||
|
|
||||||
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_TEMPERATURE,
|
||||||
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_HUMIDITY,
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
@ -37,11 +56,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
DEVICE_CLASS_MAP = {
|
|
||||||
SENSOR_TEMPERATURE: DEVICE_CLASS_TEMPERATURE,
|
|
||||||
SENSOR_HUMIDITY: DEVICE_CLASS_HUMIDITY,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Set up the HTU21D sensor."""
|
"""Set up the HTU21D sensor."""
|
||||||
@ -56,12 +70,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
|
|
||||||
sensor_handler = await hass.async_add_executor_job(HTU21DHandler, sensor)
|
sensor_handler = await hass.async_add_executor_job(HTU21DHandler, sensor)
|
||||||
|
|
||||||
dev = [
|
entities = [
|
||||||
HTU21DSensor(sensor_handler, name, SENSOR_TEMPERATURE, TEMP_CELSIUS),
|
HTU21DSensor(sensor_handler, name, description) for description in SENSOR_TYPES
|
||||||
HTU21DSensor(sensor_handler, name, SENSOR_HUMIDITY, PERCENTAGE),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
async_add_entities(dev)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class HTU21DHandler:
|
class HTU21DHandler:
|
||||||
@ -81,38 +94,21 @@ class HTU21DHandler:
|
|||||||
class HTU21DSensor(SensorEntity):
|
class HTU21DSensor(SensorEntity):
|
||||||
"""Implementation of the HTU21D sensor."""
|
"""Implementation of the HTU21D sensor."""
|
||||||
|
|
||||||
def __init__(self, htu21d_client, name, variable, unit):
|
def __init__(self, htu21d_client, name, description: SensorEntityDescription):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._name = f"{name}_{variable}"
|
self.entity_description = description
|
||||||
self._variable = variable
|
|
||||||
self._unit_of_measurement = unit
|
|
||||||
self._client = htu21d_client
|
self._client = htu21d_client
|
||||||
self._state = None
|
|
||||||
self._attr_device_class = DEVICE_CLASS_MAP[variable]
|
|
||||||
|
|
||||||
@property
|
self._attr_name = f"{name}_{description.key}"
|
||||||
def name(self) -> str:
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self) -> int:
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self) -> str:
|
|
||||||
"""Return the unit of measurement of the sensor."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Get the latest data from the HTU21D sensor and update the state."""
|
"""Get the latest data from the HTU21D sensor and update the state."""
|
||||||
await self.hass.async_add_executor_job(self._client.update)
|
await self.hass.async_add_executor_job(self._client.update)
|
||||||
if self._client.sensor.sample_ok:
|
if self._client.sensor.sample_ok:
|
||||||
if self._variable == SENSOR_TEMPERATURE:
|
if self.entity_description.key == SENSOR_TEMPERATURE:
|
||||||
value = round(self._client.sensor.temperature, 1)
|
value = round(self._client.sensor.temperature, 1)
|
||||||
else:
|
else:
|
||||||
value = round(self._client.sensor.humidity, 1)
|
value = round(self._client.sensor.humidity, 1)
|
||||||
self._state = value
|
self._attr_native_value = value
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning("Bad sample")
|
_LOGGER.warning("Bad sample")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user