mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Use entity descriptions in mysensors binary sensor (#84897)
This commit is contained in:
parent
6b6f115d7e
commit
fd78373b5c
@ -1,12 +1,17 @@
|
|||||||
"""Support for MySensors binary sensors."""
|
"""Support for MySensors binary sensors."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
BinarySensorDeviceClass,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -15,15 +20,49 @@ from .. import mysensors
|
|||||||
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
|
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
|
||||||
from .helpers import on_unload
|
from .helpers import on_unload
|
||||||
|
|
||||||
SENSORS = {
|
|
||||||
"S_DOOR": BinarySensorDeviceClass.DOOR,
|
@dataclass
|
||||||
"S_MOTION": BinarySensorDeviceClass.MOTION,
|
class MySensorsBinarySensorDescription(BinarySensorEntityDescription):
|
||||||
"S_SMOKE": BinarySensorDeviceClass.SMOKE,
|
"""Describe a MySensors binary sensor entity."""
|
||||||
"S_SPRINKLER": BinarySensorDeviceClass.SAFETY,
|
|
||||||
"S_WATER_LEAK": BinarySensorDeviceClass.SAFETY,
|
is_on: Callable[[int, dict[int, str]], bool] = (
|
||||||
"S_SOUND": BinarySensorDeviceClass.SOUND,
|
lambda value_type, values: values[value_type] == "1"
|
||||||
"S_VIBRATION": BinarySensorDeviceClass.VIBRATION,
|
)
|
||||||
"S_MOISTURE": BinarySensorDeviceClass.MOISTURE,
|
|
||||||
|
|
||||||
|
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):
|
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
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return True if the binary sensor is on."""
|
"""Return True if the binary sensor is on."""
|
||||||
return self._values.get(self.value_type) == STATE_ON
|
return self.entity_description.is_on(self.value_type, self._child.values)
|
||||||
|
|
||||||
@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)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user