mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add binary_sensor platform for Sensibo (#68088)
This commit is contained in:
parent
b18096fc54
commit
8e76948297
@ -1011,6 +1011,7 @@ omit =
|
|||||||
homeassistant/components/senseme/light.py
|
homeassistant/components/senseme/light.py
|
||||||
homeassistant/components/senseme/switch.py
|
homeassistant/components/senseme/switch.py
|
||||||
homeassistant/components/sensibo/__init__.py
|
homeassistant/components/sensibo/__init__.py
|
||||||
|
homeassistant/components/sensibo/binary_sensor.py
|
||||||
homeassistant/components/sensibo/climate.py
|
homeassistant/components/sensibo/climate.py
|
||||||
homeassistant/components/sensibo/coordinator.py
|
homeassistant/components/sensibo/coordinator.py
|
||||||
homeassistant/components/sensibo/diagnostics.py
|
homeassistant/components/sensibo/diagnostics.py
|
||||||
|
179
homeassistant/components/sensibo/binary_sensor.py
Normal file
179
homeassistant/components/sensibo/binary_sensor.py
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
"""Binary Sensor platform for Sensibo integration."""
|
||||||
|
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.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .const import DOMAIN, LOGGER
|
||||||
|
from .coordinator import MotionSensor, SensiboDataUpdateCoordinator
|
||||||
|
from .entity import SensiboDeviceBaseEntity, SensiboMotionBaseEntity
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MotionBaseEntityDescriptionMixin:
|
||||||
|
"""Mixin for required Sensibo base description keys."""
|
||||||
|
|
||||||
|
value_fn: Callable[[MotionSensor], bool | None]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class DeviceBaseEntityDescriptionMixin:
|
||||||
|
"""Mixin for required Sensibo base description keys."""
|
||||||
|
|
||||||
|
value_fn: Callable[[dict[str, Any]], bool | None]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SensiboMotionBinarySensorEntityDescription(
|
||||||
|
BinarySensorEntityDescription, MotionBaseEntityDescriptionMixin
|
||||||
|
):
|
||||||
|
"""Describes Sensibo Motion sensor entity."""
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SensiboDeviceBinarySensorEntityDescription(
|
||||||
|
BinarySensorEntityDescription, DeviceBaseEntityDescriptionMixin
|
||||||
|
):
|
||||||
|
"""Describes Sensibo Motion sensor entity."""
|
||||||
|
|
||||||
|
|
||||||
|
MOTION_SENSOR_TYPES: tuple[SensiboMotionBinarySensorEntityDescription, ...] = (
|
||||||
|
SensiboMotionBinarySensorEntityDescription(
|
||||||
|
key="alive",
|
||||||
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
name="Alive",
|
||||||
|
icon="mdi:wifi",
|
||||||
|
value_fn=lambda data: data.alive,
|
||||||
|
),
|
||||||
|
SensiboMotionBinarySensorEntityDescription(
|
||||||
|
key="is_main_sensor",
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
name="Main Sensor",
|
||||||
|
icon="mdi:connection",
|
||||||
|
value_fn=lambda data: data.is_main_sensor,
|
||||||
|
),
|
||||||
|
SensiboMotionBinarySensorEntityDescription(
|
||||||
|
key="motion",
|
||||||
|
device_class=BinarySensorDeviceClass.MOTION,
|
||||||
|
name="Motion",
|
||||||
|
icon="mdi:motion-sensor",
|
||||||
|
value_fn=lambda data: data.motion,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
DEVICE_SENSOR_TYPES: tuple[SensiboDeviceBinarySensorEntityDescription, ...] = (
|
||||||
|
SensiboDeviceBinarySensorEntityDescription(
|
||||||
|
key="room_occupied",
|
||||||
|
device_class=BinarySensorDeviceClass.MOTION,
|
||||||
|
name="Room Occupied",
|
||||||
|
icon="mdi:motion-sensor",
|
||||||
|
value_fn=lambda data: data["room_occupied"],
|
||||||
|
),
|
||||||
|
SensiboDeviceBinarySensorEntityDescription(
|
||||||
|
key="update_available",
|
||||||
|
device_class=BinarySensorDeviceClass.UPDATE,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
name="Update Available",
|
||||||
|
icon="mdi:rocket-launch",
|
||||||
|
value_fn=lambda data: data["update_available"],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Set up Sensibo binary sensor platform."""
|
||||||
|
|
||||||
|
coordinator: SensiboDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
|
entities: list[SensiboMotionSensor | SensiboDeviceSensor] = []
|
||||||
|
LOGGER.debug("parsed data: %s", coordinator.data.parsed)
|
||||||
|
entities.extend(
|
||||||
|
SensiboMotionSensor(coordinator, device_id, sensor_id, sensor_data, description)
|
||||||
|
for device_id, device_data in coordinator.data.parsed.items()
|
||||||
|
for sensor_id, sensor_data in device_data["motion_sensors"].items()
|
||||||
|
for description in MOTION_SENSOR_TYPES
|
||||||
|
if device_data["motion_sensors"]
|
||||||
|
)
|
||||||
|
LOGGER.debug("start device %s", entities)
|
||||||
|
entities.extend(
|
||||||
|
SensiboDeviceSensor(coordinator, device_id, description)
|
||||||
|
for description in DEVICE_SENSOR_TYPES
|
||||||
|
for device_id, device_data in coordinator.data.parsed.items()
|
||||||
|
if device_data[description.key] is not None
|
||||||
|
)
|
||||||
|
LOGGER.debug("list: %s", entities)
|
||||||
|
|
||||||
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
|
class SensiboMotionSensor(SensiboMotionBaseEntity, BinarySensorEntity):
|
||||||
|
"""Representation of a Sensibo Motion Binary Sensor."""
|
||||||
|
|
||||||
|
entity_description: SensiboMotionBinarySensorEntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: SensiboDataUpdateCoordinator,
|
||||||
|
device_id: str,
|
||||||
|
sensor_id: str,
|
||||||
|
sensor_data: MotionSensor,
|
||||||
|
entity_description: SensiboMotionBinarySensorEntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Initiate Sensibo Motion Binary Sensor."""
|
||||||
|
super().__init__(
|
||||||
|
coordinator,
|
||||||
|
device_id,
|
||||||
|
sensor_id,
|
||||||
|
sensor_data,
|
||||||
|
entity_description.name,
|
||||||
|
)
|
||||||
|
self.entity_description = entity_description
|
||||||
|
self._attr_unique_id = f"{sensor_id}-{entity_description.key}"
|
||||||
|
self._attr_name = (
|
||||||
|
f"{self.device_data['name']} Motion Sensor {entity_description.name}"
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool | None:
|
||||||
|
"""Return true if the binary sensor is on."""
|
||||||
|
return self.entity_description.value_fn(self.sensor_data)
|
||||||
|
|
||||||
|
|
||||||
|
class SensiboDeviceSensor(SensiboDeviceBaseEntity, BinarySensorEntity):
|
||||||
|
"""Representation of a Sensibo Device Binary Sensor."""
|
||||||
|
|
||||||
|
entity_description: SensiboDeviceBinarySensorEntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: SensiboDataUpdateCoordinator,
|
||||||
|
device_id: str,
|
||||||
|
entity_description: SensiboDeviceBinarySensorEntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Initiate Sensibo Device Binary Sensor."""
|
||||||
|
super().__init__(
|
||||||
|
coordinator,
|
||||||
|
device_id,
|
||||||
|
)
|
||||||
|
self.entity_description = entity_description
|
||||||
|
self._attr_unique_id = f"{device_id}-{entity_description.key}"
|
||||||
|
self._attr_name = f"{self.device_data['name']} {entity_description.name}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool | None:
|
||||||
|
"""Return true if the binary sensor is on."""
|
||||||
|
return self.entity_description.value_fn(self.device_data)
|
@ -12,7 +12,13 @@ LOGGER = logging.getLogger(__package__)
|
|||||||
|
|
||||||
DEFAULT_SCAN_INTERVAL = 60
|
DEFAULT_SCAN_INTERVAL = 60
|
||||||
DOMAIN = "sensibo"
|
DOMAIN = "sensibo"
|
||||||
PLATFORMS = [Platform.CLIMATE, Platform.NUMBER, Platform.SELECT, Platform.SENSOR]
|
PLATFORMS = [
|
||||||
|
Platform.BINARY_SENSOR,
|
||||||
|
Platform.CLIMATE,
|
||||||
|
Platform.NUMBER,
|
||||||
|
Platform.SELECT,
|
||||||
|
Platform.SENSOR,
|
||||||
|
]
|
||||||
ALL = ["all"]
|
ALL = ["all"]
|
||||||
DEFAULT_NAME = "Sensibo"
|
DEFAULT_NAME = "Sensibo"
|
||||||
TIMEOUT = 8
|
TIMEOUT = 8
|
||||||
|
@ -173,6 +173,12 @@ class SensiboDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
pure_boost_enabled = pure_conf.get("enabled") if pure_conf else None
|
pure_boost_enabled = pure_conf.get("enabled") if pure_conf else None
|
||||||
pm25 = dev["measurements"].get("pm25")
|
pm25 = dev["measurements"].get("pm25")
|
||||||
|
|
||||||
|
# Binary sensors for main device
|
||||||
|
room_occupied = dev["roomIsOccupied"]
|
||||||
|
update_available = bool(
|
||||||
|
dev["firmwareVersion"] != dev["currentlyAvailableFirmwareVersion"]
|
||||||
|
)
|
||||||
|
|
||||||
device_data[unique_id] = {
|
device_data[unique_id] = {
|
||||||
"id": unique_id,
|
"id": unique_id,
|
||||||
"mac": mac,
|
"mac": mac,
|
||||||
@ -209,6 +215,8 @@ class SensiboDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
"pure_sensitivity": pure_sensitivity,
|
"pure_sensitivity": pure_sensitivity,
|
||||||
"pure_boost_enabled": pure_boost_enabled,
|
"pure_boost_enabled": pure_boost_enabled,
|
||||||
"pm25": pm25,
|
"pm25": pm25,
|
||||||
|
"room_occupied": room_occupied,
|
||||||
|
"update_available": update_available,
|
||||||
}
|
}
|
||||||
|
|
||||||
return SensiboData(raw=data, parsed=device_data)
|
return SensiboData(raw=data, parsed=device_data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user