Provide unique id for enocean devices (#71774)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/72005/head
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."""
from __future__ import annotations
from enocean.utils import combine_hex
import voluptuous as vol
from homeassistant.components.binary_sensor import (
@ -57,6 +58,7 @@ class EnOceanBinarySensor(EnOceanEntity, BinarySensorEntity):
self._device_class = device_class
self.which = -1
self.onoff = -1
self._attr_unique_id = f"{combine_hex(dev_id)}-{device_class}"
@property
def name(self):

View File

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

View File

@ -1,6 +1,10 @@
"""Support for EnOcean sensors."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from enocean.utils import combine_hex
import voluptuous as vol
from homeassistant.components.sensor import (
@ -40,37 +44,56 @@ SENSOR_TYPE_POWER = "powersensor"
SENSOR_TYPE_TEMPERATURE = "temperature"
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,
name="Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
icon="mdi:thermometer",
device_class=SensorDeviceClass.TEMPERATURE,
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,
name="Humidity",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:water-percent",
device_class=SensorDeviceClass.HUMIDITY,
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,
name="Power",
native_unit_of_measurement=POWER_WATT,
icon="mdi:power-plug",
device_class=SensorDeviceClass.POWER,
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,
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):
"""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."""
super().__init__(dev_id, dev_name)
self.entity_description = description
self._attr_name = f"{description.name} {dev_name}"
self._attr_unique_id = description.unique_id(dev_id)
async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
@ -194,7 +218,7 @@ class EnOceanTemperatureSensor(EnOceanSensor):
self,
dev_id,
dev_name,
description: SensorEntityDescription,
description: EnOceanSensorEntityDescription,
*,
scale_min,
scale_max,
@ -248,7 +272,6 @@ class EnOceanWindowHandle(EnOceanSensor):
def value_changed(self, packet):
"""Update the internal state of the sensor."""
action = (packet.data[1] & 0x70) >> 4
if action == 0x07:

View File

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