2019-02-14 04:35:12 +00:00
|
|
|
"""Support for MAX! binary sensors via MAX! Cube."""
|
2022-01-05 14:11:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-09-12 02:24:23 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-17 14:02:28 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 02:24:23 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2023-02-09 19:15:37 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
2022-01-05 14:11:38 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import DATA_KEY
|
2017-02-27 05:35:33 +00:00
|
|
|
|
|
|
|
|
2022-01-05 14:11:38 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-04-22 19:13:04 +00:00
|
|
|
"""Iterate through all MAX! Devices and add window shutters."""
|
2022-01-05 14:11:38 +00:00
|
|
|
devices: list[MaxCubeBinarySensorBase] = []
|
2018-04-19 07:11:38 +00:00
|
|
|
for handler in hass.data[DATA_KEY].values():
|
2021-03-27 23:21:20 +00:00
|
|
|
for device in handler.cube.devices:
|
2021-12-17 14:02:28 +00:00
|
|
|
devices.append(MaxCubeBattery(handler, device))
|
2018-04-19 07:11:38 +00:00
|
|
|
# Only add Window Shutters
|
2021-01-14 10:33:02 +00:00
|
|
|
if device.is_windowshutter():
|
2021-03-27 23:21:20 +00:00
|
|
|
devices.append(MaxCubeShutter(handler, device))
|
2017-02-27 05:35:33 +00:00
|
|
|
|
2022-10-17 19:11:58 +00:00
|
|
|
add_entities(devices)
|
2017-02-27 05:35:33 +00:00
|
|
|
|
|
|
|
|
2021-12-17 14:02:28 +00:00
|
|
|
class MaxCubeBinarySensorBase(BinarySensorEntity):
|
|
|
|
"""Base class for maxcube binary sensors."""
|
|
|
|
|
|
|
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
2017-02-27 05:35:33 +00:00
|
|
|
|
2021-03-27 23:21:20 +00:00
|
|
|
def __init__(self, handler, device):
|
2020-04-23 19:57:07 +00:00
|
|
|
"""Initialize MAX! Cube BinarySensorEntity."""
|
2018-04-19 07:11:38 +00:00
|
|
|
self._cubehandle = handler
|
2021-03-27 23:21:20 +00:00
|
|
|
self._device = device
|
2021-12-17 14:02:28 +00:00
|
|
|
self._room = handler.cube.room_by_id(device.room_id)
|
2017-02-27 05:35:33 +00:00
|
|
|
|
2022-09-05 08:59:36 +00:00
|
|
|
def update(self) -> None:
|
2021-12-17 14:02:28 +00:00
|
|
|
"""Get latest data from MAX! Cube."""
|
|
|
|
self._cubehandle.update()
|
2017-02-27 05:35:33 +00:00
|
|
|
|
2021-04-14 20:19:24 +00:00
|
|
|
|
2021-12-17 14:02:28 +00:00
|
|
|
class MaxCubeShutter(MaxCubeBinarySensorBase):
|
|
|
|
"""Representation of a MAX! Cube Binary Sensor device."""
|
|
|
|
|
|
|
|
_attr_device_class = BinarySensorDeviceClass.WINDOW
|
|
|
|
|
|
|
|
def __init__(self, handler, device):
|
|
|
|
"""Initialize MAX! Cube BinarySensorEntity."""
|
|
|
|
super().__init__(handler, device)
|
|
|
|
|
|
|
|
self._attr_name = f"{self._room.name} {self._device.name}"
|
|
|
|
self._attr_unique_id = self._device.serial
|
2017-02-27 05:35:33 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the binary sensor is on/open."""
|
2021-03-27 23:21:20 +00:00
|
|
|
return self._device.is_open
|
2017-02-27 05:35:33 +00:00
|
|
|
|
2021-12-17 14:02:28 +00:00
|
|
|
|
|
|
|
class MaxCubeBattery(MaxCubeBinarySensorBase):
|
|
|
|
"""Representation of a MAX! Cube Binary Sensor device."""
|
|
|
|
|
|
|
|
_attr_device_class = BinarySensorDeviceClass.BATTERY
|
|
|
|
|
|
|
|
def __init__(self, handler, device):
|
|
|
|
"""Initialize MAX! Cube BinarySensorEntity."""
|
|
|
|
super().__init__(handler, device)
|
|
|
|
|
|
|
|
self._attr_name = f"{self._room.name} {device.name} battery"
|
|
|
|
self._attr_unique_id = f"{self._device.serial}_battery"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the binary sensor is on/open."""
|
|
|
|
return self._device.battery == 1
|