mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Use EntityDescription - enocean (#55087)
This commit is contained in:
parent
9fe434ac36
commit
4d93f23feb
@ -1,7 +1,13 @@
|
|||||||
"""Support for EnOcean sensors."""
|
"""Support for EnOcean sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
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_DEVICE_CLASS,
|
CONF_DEVICE_CLASS,
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
@ -32,32 +38,36 @@ SENSOR_TYPE_POWER = "powersensor"
|
|||||||
SENSOR_TYPE_TEMPERATURE = "temperature"
|
SENSOR_TYPE_TEMPERATURE = "temperature"
|
||||||
SENSOR_TYPE_WINDOWHANDLE = "windowhandle"
|
SENSOR_TYPE_WINDOWHANDLE = "windowhandle"
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_DESC_TEMPERATURE = SensorEntityDescription(
|
||||||
SENSOR_TYPE_HUMIDITY: {
|
key=SENSOR_TYPE_TEMPERATURE,
|
||||||
"name": "Humidity",
|
name="Temperature",
|
||||||
"unit": PERCENTAGE,
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
"icon": "mdi:water-percent",
|
icon="mdi:thermometer",
|
||||||
"class": DEVICE_CLASS_HUMIDITY,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
},
|
)
|
||||||
SENSOR_TYPE_POWER: {
|
|
||||||
"name": "Power",
|
SENSOR_DESC_HUMIDITY = SensorEntityDescription(
|
||||||
"unit": POWER_WATT,
|
key=SENSOR_TYPE_HUMIDITY,
|
||||||
"icon": "mdi:power-plug",
|
name="Humidity",
|
||||||
"class": DEVICE_CLASS_POWER,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
},
|
icon="mdi:water-percent",
|
||||||
SENSOR_TYPE_TEMPERATURE: {
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
"name": "Temperature",
|
)
|
||||||
"unit": TEMP_CELSIUS,
|
|
||||||
"icon": "mdi:thermometer",
|
SENSOR_DESC_POWER = SensorEntityDescription(
|
||||||
"class": DEVICE_CLASS_TEMPERATURE,
|
key=SENSOR_TYPE_POWER,
|
||||||
},
|
name="Power",
|
||||||
SENSOR_TYPE_WINDOWHANDLE: {
|
native_unit_of_measurement=POWER_WATT,
|
||||||
"name": "WindowHandle",
|
icon="mdi:power-plug",
|
||||||
"unit": None,
|
device_class=DEVICE_CLASS_POWER,
|
||||||
"icon": "mdi:window",
|
)
|
||||||
"class": None,
|
|
||||||
},
|
SENSOR_DESC_WINDOWHANDLE = SensorEntityDescription(
|
||||||
}
|
key=SENSOR_TYPE_WINDOWHANDLE,
|
||||||
|
name="WindowHandle",
|
||||||
|
icon="mdi:window",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
@ -74,81 +84,60 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up an EnOcean sensor device."""
|
"""Set up an EnOcean sensor device."""
|
||||||
dev_id = config.get(CONF_ID)
|
dev_id = config[CONF_ID]
|
||||||
dev_name = config.get(CONF_NAME)
|
dev_name = config[CONF_NAME]
|
||||||
sensor_type = config.get(CONF_DEVICE_CLASS)
|
sensor_type = config[CONF_DEVICE_CLASS]
|
||||||
|
|
||||||
|
entities: list[EnOceanSensor] = []
|
||||||
if sensor_type == SENSOR_TYPE_TEMPERATURE:
|
if sensor_type == SENSOR_TYPE_TEMPERATURE:
|
||||||
temp_min = config.get(CONF_MIN_TEMP)
|
temp_min = config[CONF_MIN_TEMP]
|
||||||
temp_max = config.get(CONF_MAX_TEMP)
|
temp_max = config[CONF_MAX_TEMP]
|
||||||
range_from = config.get(CONF_RANGE_FROM)
|
range_from = config[CONF_RANGE_FROM]
|
||||||
range_to = config.get(CONF_RANGE_TO)
|
range_to = config[CONF_RANGE_TO]
|
||||||
add_entities(
|
entities = [
|
||||||
[
|
EnOceanTemperatureSensor(
|
||||||
EnOceanTemperatureSensor(
|
dev_id,
|
||||||
dev_id, dev_name, temp_min, temp_max, range_from, range_to
|
dev_name,
|
||||||
)
|
SENSOR_DESC_TEMPERATURE,
|
||||||
]
|
scale_min=temp_min,
|
||||||
)
|
scale_max=temp_max,
|
||||||
|
range_from=range_from,
|
||||||
|
range_to=range_to,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
elif sensor_type == SENSOR_TYPE_HUMIDITY:
|
elif sensor_type == SENSOR_TYPE_HUMIDITY:
|
||||||
add_entities([EnOceanHumiditySensor(dev_id, dev_name)])
|
entities = [EnOceanHumiditySensor(dev_id, dev_name, SENSOR_DESC_HUMIDITY)]
|
||||||
|
|
||||||
elif sensor_type == SENSOR_TYPE_POWER:
|
elif sensor_type == SENSOR_TYPE_POWER:
|
||||||
add_entities([EnOceanPowerSensor(dev_id, dev_name)])
|
entities = [EnOceanPowerSensor(dev_id, dev_name, SENSOR_DESC_POWER)]
|
||||||
|
|
||||||
elif sensor_type == SENSOR_TYPE_WINDOWHANDLE:
|
elif sensor_type == SENSOR_TYPE_WINDOWHANDLE:
|
||||||
add_entities([EnOceanWindowHandle(dev_id, dev_name)])
|
entities = [EnOceanWindowHandle(dev_id, dev_name, SENSOR_DESC_WINDOWHANDLE)]
|
||||||
|
|
||||||
|
if entities:
|
||||||
|
add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class EnOceanSensor(EnOceanEntity, RestoreEntity, SensorEntity):
|
class EnOceanSensor(EnOceanEntity, RestoreEntity, SensorEntity):
|
||||||
"""Representation of an EnOcean sensor device such as a power meter."""
|
"""Representation of an EnOcean sensor device such as a power meter."""
|
||||||
|
|
||||||
def __init__(self, dev_id, dev_name, sensor_type):
|
def __init__(self, dev_id, dev_name, description: SensorEntityDescription):
|
||||||
"""Initialize the EnOcean sensor device."""
|
"""Initialize the EnOcean sensor device."""
|
||||||
super().__init__(dev_id, dev_name)
|
super().__init__(dev_id, dev_name)
|
||||||
self._sensor_type = sensor_type
|
self.entity_description = description
|
||||||
self._device_class = SENSOR_TYPES[self._sensor_type]["class"]
|
self._attr_name = f"{description.name} {dev_name}"
|
||||||
self._dev_name = f"{SENSOR_TYPES[self._sensor_type]['name']} {dev_name}"
|
|
||||||
self._unit_of_measurement = SENSOR_TYPES[self._sensor_type]["unit"]
|
|
||||||
self._icon = SENSOR_TYPES[self._sensor_type]["icon"]
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the device."""
|
|
||||||
return self._dev_name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Icon to use in the frontend."""
|
|
||||||
return self._icon
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device class of the sensor."""
|
|
||||||
return self._device_class
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the device."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Call when entity about to be added to hass."""
|
"""Call when entity about to be added to hass."""
|
||||||
# If not None, we got an initial value.
|
# If not None, we got an initial value.
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
if self._state is not None:
|
if self._attr_native_value is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
state = await self.async_get_last_state()
|
||||||
if state is not None:
|
if state is not None:
|
||||||
self._state = state.state
|
self._attr_native_value = state.state
|
||||||
|
|
||||||
def value_changed(self, packet):
|
def value_changed(self, packet):
|
||||||
"""Update the internal state of the sensor."""
|
"""Update the internal state of the sensor."""
|
||||||
@ -161,10 +150,6 @@ class EnOceanPowerSensor(EnOceanSensor):
|
|||||||
- A5-12-01 (Automated Meter Reading, Electricity)
|
- A5-12-01 (Automated Meter Reading, Electricity)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, dev_id, dev_name):
|
|
||||||
"""Initialize the EnOcean power sensor device."""
|
|
||||||
super().__init__(dev_id, dev_name, SENSOR_TYPE_POWER)
|
|
||||||
|
|
||||||
def value_changed(self, packet):
|
def value_changed(self, packet):
|
||||||
"""Update the internal state of the sensor."""
|
"""Update the internal state of the sensor."""
|
||||||
if packet.rorg != 0xA5:
|
if packet.rorg != 0xA5:
|
||||||
@ -174,7 +159,7 @@ class EnOceanPowerSensor(EnOceanSensor):
|
|||||||
# this packet reports the current value
|
# this packet reports the current value
|
||||||
raw_val = packet.parsed["MR"]["raw_value"]
|
raw_val = packet.parsed["MR"]["raw_value"]
|
||||||
divisor = packet.parsed["DIV"]["raw_value"]
|
divisor = packet.parsed["DIV"]["raw_value"]
|
||||||
self._state = raw_val / (10 ** divisor)
|
self._attr_native_value = raw_val / (10 ** divisor)
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
|
||||||
@ -196,9 +181,19 @@ class EnOceanTemperatureSensor(EnOceanSensor):
|
|||||||
- A5-10-10 to A5-10-14
|
- A5-10-10 to A5-10-14
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, dev_id, dev_name, scale_min, scale_max, range_from, range_to):
|
def __init__(
|
||||||
|
self,
|
||||||
|
dev_id,
|
||||||
|
dev_name,
|
||||||
|
description: SensorEntityDescription,
|
||||||
|
*,
|
||||||
|
scale_min,
|
||||||
|
scale_max,
|
||||||
|
range_from,
|
||||||
|
range_to,
|
||||||
|
):
|
||||||
"""Initialize the EnOcean temperature sensor device."""
|
"""Initialize the EnOcean temperature sensor device."""
|
||||||
super().__init__(dev_id, dev_name, SENSOR_TYPE_TEMPERATURE)
|
super().__init__(dev_id, dev_name, description)
|
||||||
self._scale_min = scale_min
|
self._scale_min = scale_min
|
||||||
self._scale_max = scale_max
|
self._scale_max = scale_max
|
||||||
self.range_from = range_from
|
self.range_from = range_from
|
||||||
@ -213,7 +208,7 @@ class EnOceanTemperatureSensor(EnOceanSensor):
|
|||||||
raw_val = packet.data[3]
|
raw_val = packet.data[3]
|
||||||
temperature = temp_scale / temp_range * (raw_val - self.range_from)
|
temperature = temp_scale / temp_range * (raw_val - self.range_from)
|
||||||
temperature += self._scale_min
|
temperature += self._scale_min
|
||||||
self._state = round(temperature, 1)
|
self._attr_native_value = round(temperature, 1)
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
|
||||||
@ -226,16 +221,12 @@ class EnOceanHumiditySensor(EnOceanSensor):
|
|||||||
- A5-10-10 to A5-10-14 (Room Operating Panels)
|
- A5-10-10 to A5-10-14 (Room Operating Panels)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, dev_id, dev_name):
|
|
||||||
"""Initialize the EnOcean humidity sensor device."""
|
|
||||||
super().__init__(dev_id, dev_name, SENSOR_TYPE_HUMIDITY)
|
|
||||||
|
|
||||||
def value_changed(self, packet):
|
def value_changed(self, packet):
|
||||||
"""Update the internal state of the sensor."""
|
"""Update the internal state of the sensor."""
|
||||||
if packet.rorg != 0xA5:
|
if packet.rorg != 0xA5:
|
||||||
return
|
return
|
||||||
humidity = packet.data[2] * 100 / 250
|
humidity = packet.data[2] * 100 / 250
|
||||||
self._state = round(humidity, 1)
|
self._attr_native_value = round(humidity, 1)
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
|
||||||
@ -246,20 +237,16 @@ class EnOceanWindowHandle(EnOceanSensor):
|
|||||||
- F6-10-00 (Mechanical handle / Hoppe AG)
|
- F6-10-00 (Mechanical handle / Hoppe AG)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, dev_id, dev_name):
|
|
||||||
"""Initialize the EnOcean window handle sensor device."""
|
|
||||||
super().__init__(dev_id, dev_name, SENSOR_TYPE_WINDOWHANDLE)
|
|
||||||
|
|
||||||
def value_changed(self, packet):
|
def value_changed(self, packet):
|
||||||
"""Update the internal state of the sensor."""
|
"""Update the internal state of the sensor."""
|
||||||
|
|
||||||
action = (packet.data[1] & 0x70) >> 4
|
action = (packet.data[1] & 0x70) >> 4
|
||||||
|
|
||||||
if action == 0x07:
|
if action == 0x07:
|
||||||
self._state = STATE_CLOSED
|
self._attr_native_value = STATE_CLOSED
|
||||||
if action in (0x04, 0x06):
|
if action in (0x04, 0x06):
|
||||||
self._state = STATE_OPEN
|
self._attr_native_value = STATE_OPEN
|
||||||
if action == 0x05:
|
if action == 0x05:
|
||||||
self._state = "tilt"
|
self._attr_native_value = "tilt"
|
||||||
|
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user