Provide unique id for enocean devices (#71774)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
rhadamantys 2022-05-17 13:43:36 +02:00 committed by GitHub
parent 7d2deae592
commit c67bbd06d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 8 deletions

View File

@ -1,6 +1,7 @@
"""Support for EnOcean binary sensors.""" """Support for EnOcean binary sensors."""
from __future__ import annotations from __future__ import annotations
from enocean.utils import combine_hex
import voluptuous as vol import voluptuous as vol
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
@ -57,6 +58,7 @@ class EnOceanBinarySensor(EnOceanEntity, BinarySensorEntity):
self._device_class = device_class self._device_class = device_class
self.which = -1 self.which = -1
self.onoff = -1 self.onoff = -1
self._attr_unique_id = f"{combine_hex(dev_id)}-{device_class}"
@property @property
def name(self): def name(self):

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import math import math
from enocean.utils import combine_hex
import voluptuous as vol import voluptuous as vol
from homeassistant.components.light import ( from homeassistant.components.light import (
@ -58,6 +59,7 @@ class EnOceanLight(EnOceanEntity, LightEntity):
self._on_state = False self._on_state = False
self._brightness = 50 self._brightness = 50
self._sender_id = sender_id self._sender_id = sender_id
self._attr_unique_id = f"{combine_hex(dev_id)}"
@property @property
def name(self): def name(self):

View File

@ -1,6 +1,10 @@
"""Support for EnOcean sensors.""" """Support for EnOcean sensors."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from enocean.utils import combine_hex
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -40,37 +44,56 @@ SENSOR_TYPE_POWER = "powersensor"
SENSOR_TYPE_TEMPERATURE = "temperature" SENSOR_TYPE_TEMPERATURE = "temperature"
SENSOR_TYPE_WINDOWHANDLE = "windowhandle" SENSOR_TYPE_WINDOWHANDLE = "windowhandle"
SENSOR_DESC_TEMPERATURE = SensorEntityDescription(
@dataclass
class EnOceanSensorEntityDescriptionMixin:
"""Mixin for required keys."""
unique_id: Callable[[list[int]], str | None]
@dataclass
class EnOceanSensorEntityDescription(
SensorEntityDescription, EnOceanSensorEntityDescriptionMixin
):
"""Describes EnOcean sensor entity."""
SENSOR_DESC_TEMPERATURE = EnOceanSensorEntityDescription(
key=SENSOR_TYPE_TEMPERATURE, key=SENSOR_TYPE_TEMPERATURE,
name="Temperature", name="Temperature",
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
icon="mdi:thermometer", icon="mdi:thermometer",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
unique_id=lambda dev_id: f"{combine_hex(dev_id)}-{SENSOR_TYPE_TEMPERATURE}",
) )
SENSOR_DESC_HUMIDITY = SensorEntityDescription( SENSOR_DESC_HUMIDITY = EnOceanSensorEntityDescription(
key=SENSOR_TYPE_HUMIDITY, key=SENSOR_TYPE_HUMIDITY,
name="Humidity", name="Humidity",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
icon="mdi:water-percent", icon="mdi:water-percent",
device_class=SensorDeviceClass.HUMIDITY, device_class=SensorDeviceClass.HUMIDITY,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
unique_id=lambda dev_id: f"{combine_hex(dev_id)}-{SENSOR_TYPE_HUMIDITY}",
) )
SENSOR_DESC_POWER = SensorEntityDescription( SENSOR_DESC_POWER = EnOceanSensorEntityDescription(
key=SENSOR_TYPE_POWER, key=SENSOR_TYPE_POWER,
name="Power", name="Power",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
icon="mdi:power-plug", icon="mdi:power-plug",
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
unique_id=lambda dev_id: f"{combine_hex(dev_id)}-{SENSOR_TYPE_POWER}",
) )
SENSOR_DESC_WINDOWHANDLE = SensorEntityDescription( SENSOR_DESC_WINDOWHANDLE = EnOceanSensorEntityDescription(
key=SENSOR_TYPE_WINDOWHANDLE, key=SENSOR_TYPE_WINDOWHANDLE,
name="WindowHandle", name="WindowHandle",
icon="mdi:window", icon="mdi:window-open-variant",
unique_id=lambda dev_id: f"{combine_hex(dev_id)}-{SENSOR_TYPE_WINDOWHANDLE}",
) )
@ -132,11 +155,12 @@ def setup_platform(
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, description: SensorEntityDescription): def __init__(self, dev_id, dev_name, description: EnOceanSensorEntityDescription):
"""Initialize the EnOcean sensor device.""" """Initialize the EnOcean sensor device."""
super().__init__(dev_id, dev_name) super().__init__(dev_id, dev_name)
self.entity_description = description self.entity_description = description
self._attr_name = f"{description.name} {dev_name}" self._attr_name = f"{description.name} {dev_name}"
self._attr_unique_id = description.unique_id(dev_id)
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."""
@ -194,7 +218,7 @@ class EnOceanTemperatureSensor(EnOceanSensor):
self, self,
dev_id, dev_id,
dev_name, dev_name,
description: SensorEntityDescription, description: EnOceanSensorEntityDescription,
*, *,
scale_min, scale_min,
scale_max, scale_max,
@ -248,7 +272,6 @@ class EnOceanWindowHandle(EnOceanSensor):
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:

View File

@ -1,6 +1,7 @@
"""Support for EnOcean switches.""" """Support for EnOcean switches."""
from __future__ import annotations from __future__ import annotations
from enocean.utils import combine_hex
import voluptuous as vol import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
@ -48,6 +49,7 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity):
self._on_state = False self._on_state = False
self._on_state2 = False self._on_state2 = False
self.channel = channel self.channel = channel
self._attr_unique_id = f"{combine_hex(dev_id)}"
@property @property
def is_on(self): def is_on(self):