mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Use EntityDescription - konnected (#55923)
This commit is contained in:
parent
32b8be5a6e
commit
0b23ce658e
@ -1,5 +1,7 @@
|
|||||||
"""Support for DHT and DS18B20 sensors attached to a Konnected device."""
|
"""Support for DHT and DS18B20 sensors attached to a Konnected device."""
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_DEVICES,
|
CONF_DEVICES,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
@ -16,9 +18,19 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|||||||
|
|
||||||
from .const import DOMAIN as KONNECTED_DOMAIN, SIGNAL_DS18B20_NEW
|
from .const import DOMAIN as KONNECTED_DOMAIN, SIGNAL_DS18B20_NEW
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
||||||
DEVICE_CLASS_TEMPERATURE: ["Temperature", TEMP_CELSIUS],
|
"temperature": SensorEntityDescription(
|
||||||
DEVICE_CLASS_HUMIDITY: ["Humidity", PERCENTAGE],
|
key=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
name="Temperature",
|
||||||
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
),
|
||||||
|
"humidity": SensorEntityDescription(
|
||||||
|
key=DEVICE_CLASS_HUMIDITY,
|
||||||
|
name="Humidity",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -26,7 +38,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
"""Set up sensors attached to a Konnected device from a config entry."""
|
"""Set up sensors attached to a Konnected device from a config entry."""
|
||||||
data = hass.data[KONNECTED_DOMAIN]
|
data = hass.data[KONNECTED_DOMAIN]
|
||||||
device_id = config_entry.data["id"]
|
device_id = config_entry.data["id"]
|
||||||
sensors = []
|
|
||||||
|
|
||||||
# Initialize all DHT sensors.
|
# Initialize all DHT sensors.
|
||||||
dht_sensors = [
|
dht_sensors = [
|
||||||
@ -34,11 +45,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
for sensor in data[CONF_DEVICES][device_id][CONF_SENSORS]
|
for sensor in data[CONF_DEVICES][device_id][CONF_SENSORS]
|
||||||
if sensor[CONF_TYPE] == "dht"
|
if sensor[CONF_TYPE] == "dht"
|
||||||
]
|
]
|
||||||
for sensor in dht_sensors:
|
entities = [
|
||||||
sensors.append(KonnectedSensor(device_id, sensor, DEVICE_CLASS_TEMPERATURE))
|
KonnectedSensor(device_id, data=sensor_config, description=description)
|
||||||
sensors.append(KonnectedSensor(device_id, sensor, DEVICE_CLASS_HUMIDITY))
|
for sensor_config in dht_sensors
|
||||||
|
for description in SENSOR_TYPES.values()
|
||||||
|
]
|
||||||
|
|
||||||
async_add_entities(sensors)
|
async_add_entities(entities)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_add_ds18b20(attrs):
|
def async_add_ds18b20(attrs):
|
||||||
@ -57,7 +70,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
KonnectedSensor(
|
KonnectedSensor(
|
||||||
device_id,
|
device_id,
|
||||||
sensor_config,
|
sensor_config,
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
SENSOR_TYPES["temperature"],
|
||||||
addr=attrs.get("addr"),
|
addr=attrs.get("addr"),
|
||||||
initial_state=attrs.get("temp"),
|
initial_state=attrs.get("temp"),
|
||||||
)
|
)
|
||||||
@ -73,15 +86,20 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
class KonnectedSensor(SensorEntity):
|
class KonnectedSensor(SensorEntity):
|
||||||
"""Represents a Konnected DHT Sensor."""
|
"""Represents a Konnected DHT Sensor."""
|
||||||
|
|
||||||
def __init__(self, device_id, data, sensor_type, addr=None, initial_state=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
device_id,
|
||||||
|
data,
|
||||||
|
description: SensorEntityDescription,
|
||||||
|
addr=None,
|
||||||
|
initial_state=None,
|
||||||
|
):
|
||||||
"""Initialize the entity for a single sensor_type."""
|
"""Initialize the entity for a single sensor_type."""
|
||||||
|
self.entity_description = description
|
||||||
self._addr = addr
|
self._addr = addr
|
||||||
self._data = data
|
self._data = data
|
||||||
self._device_id = device_id
|
|
||||||
self._type = sensor_type
|
|
||||||
self._zone_num = self._data.get(CONF_ZONE)
|
self._zone_num = self._data.get(CONF_ZONE)
|
||||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
self._attr_unique_id = addr or f"{device_id}-{self._zone_num}-{description.key}"
|
||||||
self._unique_id = addr or f"{device_id}-{self._zone_num}-{sensor_type}"
|
|
||||||
|
|
||||||
# set initial state if known at initialization
|
# set initial state if known at initialization
|
||||||
self._state = initial_state
|
self._state = initial_state
|
||||||
@ -89,38 +107,20 @@ class KonnectedSensor(SensorEntity):
|
|||||||
self._state = round(float(self._state), 1)
|
self._state = round(float(self._state), 1)
|
||||||
|
|
||||||
# set entity name if given
|
# set entity name if given
|
||||||
self._name = self._data.get(CONF_NAME)
|
if name := self._data.get(CONF_NAME):
|
||||||
if self._name:
|
name += f" {description.name}"
|
||||||
self._name += f" {SENSOR_TYPES[sensor_type][0]}"
|
self._attr_name = name
|
||||||
|
|
||||||
@property
|
self._attr_device_info = {"identifiers": {(KONNECTED_DOMAIN, device_id)}}
|
||||||
def unique_id(self) -> str:
|
|
||||||
"""Return the unique id."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self):
|
|
||||||
"""Return the device info."""
|
|
||||||
return {"identifiers": {(KONNECTED_DOMAIN, self._device_id)}}
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Store entity_id and register state change callback."""
|
"""Store entity_id and register state change callback."""
|
||||||
entity_id_key = self._addr or self._type
|
entity_id_key = self._addr or self.entity_description.key
|
||||||
self._data[entity_id_key] = self.entity_id
|
self._data[entity_id_key] = self.entity_id
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
self.hass, f"konnected.{self.entity_id}.update", self.async_set_state
|
self.hass, f"konnected.{self.entity_id}.update", self.async_set_state
|
||||||
@ -129,7 +129,7 @@ class KonnectedSensor(SensorEntity):
|
|||||||
@callback
|
@callback
|
||||||
def async_set_state(self, state):
|
def async_set_state(self, state):
|
||||||
"""Update the sensor's state."""
|
"""Update the sensor's state."""
|
||||||
if self._type == DEVICE_CLASS_HUMIDITY:
|
if self.entity_description.key == DEVICE_CLASS_HUMIDITY:
|
||||||
self._state = int(float(state))
|
self._state = int(float(state))
|
||||||
else:
|
else:
|
||||||
self._state = round(float(state), 1)
|
self._state = round(float(state), 1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user