Use entity descriptions in mysensors binary sensor (#84897)
parent
6b6f115d7e
commit
fd78373b5c
|
@ -1,12 +1,17 @@
|
|||
"""Support for MySensors binary sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import STATE_ON, Platform
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -15,15 +20,49 @@ from .. import mysensors
|
|||
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
|
||||
from .helpers import on_unload
|
||||
|
||||
SENSORS = {
|
||||
"S_DOOR": BinarySensorDeviceClass.DOOR,
|
||||
"S_MOTION": BinarySensorDeviceClass.MOTION,
|
||||
"S_SMOKE": BinarySensorDeviceClass.SMOKE,
|
||||
"S_SPRINKLER": BinarySensorDeviceClass.SAFETY,
|
||||
"S_WATER_LEAK": BinarySensorDeviceClass.SAFETY,
|
||||
"S_SOUND": BinarySensorDeviceClass.SOUND,
|
||||
"S_VIBRATION": BinarySensorDeviceClass.VIBRATION,
|
||||
"S_MOISTURE": BinarySensorDeviceClass.MOISTURE,
|
||||
|
||||
@dataclass
|
||||
class MySensorsBinarySensorDescription(BinarySensorEntityDescription):
|
||||
"""Describe a MySensors binary sensor entity."""
|
||||
|
||||
is_on: Callable[[int, dict[int, str]], bool] = (
|
||||
lambda value_type, values: values[value_type] == "1"
|
||||
)
|
||||
|
||||
|
||||
SENSORS: dict[str, MySensorsBinarySensorDescription] = {
|
||||
"S_DOOR": MySensorsBinarySensorDescription(
|
||||
key="S_DOOR",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
),
|
||||
"S_MOTION": MySensorsBinarySensorDescription(
|
||||
key="S_MOTION",
|
||||
device_class=BinarySensorDeviceClass.MOTION,
|
||||
),
|
||||
"S_SMOKE": MySensorsBinarySensorDescription(
|
||||
key="S_SMOKE",
|
||||
device_class=BinarySensorDeviceClass.SMOKE,
|
||||
),
|
||||
"S_SPRINKLER": MySensorsBinarySensorDescription(
|
||||
key="S_SPRINKLER",
|
||||
device_class=BinarySensorDeviceClass.SAFETY,
|
||||
),
|
||||
"S_WATER_LEAK": MySensorsBinarySensorDescription(
|
||||
key="S_WATER_LEAK",
|
||||
device_class=BinarySensorDeviceClass.SAFETY,
|
||||
),
|
||||
"S_SOUND": MySensorsBinarySensorDescription(
|
||||
key="S_SOUND",
|
||||
device_class=BinarySensorDeviceClass.SOUND,
|
||||
),
|
||||
"S_VIBRATION": MySensorsBinarySensorDescription(
|
||||
key="S_VIBRATION",
|
||||
device_class=BinarySensorDeviceClass.VIBRATION,
|
||||
),
|
||||
"S_MOISTURE": MySensorsBinarySensorDescription(
|
||||
key="S_MOISTURE",
|
||||
device_class=BinarySensorDeviceClass.MOISTURE,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
|
@ -57,15 +96,17 @@ async def async_setup_entry(
|
|||
|
||||
|
||||
class MySensorsBinarySensor(mysensors.device.MySensorsEntity, BinarySensorEntity):
|
||||
"""Representation of a MySensors Binary Sensor child node."""
|
||||
"""Representation of a MySensors binary sensor child node."""
|
||||
|
||||
entity_description: MySensorsBinarySensorDescription
|
||||
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
"""Set up the instance."""
|
||||
super().__init__(*args, **kwargs)
|
||||
pres = self.gateway.const.Presentation
|
||||
self.entity_description = SENSORS[pres(self.child_type).name]
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return True if the binary sensor is on."""
|
||||
return self._values.get(self.value_type) == STATE_ON
|
||||
|
||||
@property
|
||||
def device_class(self) -> BinarySensorDeviceClass | None:
|
||||
"""Return the class of this sensor, from DEVICE_CLASSES."""
|
||||
pres = self.gateway.const.Presentation
|
||||
return SENSORS.get(pres(self.child_type).name)
|
||||
return self.entity_description.is_on(self.value_type, self._child.values)
|
||||
|
|
Loading…
Reference in New Issue