Add battery entity for Maxcube devices (#58699)

This commit is contained in:
Peeter N 2021-12-17 16:02:28 +02:00 committed by GitHub
parent 9cd82e0f00
commit 571b245b7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 22 deletions

View file

@ -1,8 +1,9 @@
"""Support for MAX! binary sensors via MAX! Cube."""
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_WINDOW,
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.helpers.entity import EntityCategory
from . import DATA_KEY
@ -12,6 +13,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
devices = []
for handler in hass.data[DATA_KEY].values():
for device in handler.cube.devices:
devices.append(MaxCubeBattery(handler, device))
# Only add Window Shutters
if device.is_windowshutter():
devices.append(MaxCubeShutter(handler, device))
@ -20,36 +22,53 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
add_entities(devices)
class MaxCubeShutter(BinarySensorEntity):
"""Representation of a MAX! Cube Binary Sensor device."""
class MaxCubeBinarySensorBase(BinarySensorEntity):
"""Base class for maxcube binary sensors."""
_attr_entity_category = EntityCategory.DIAGNOSTIC
def __init__(self, handler, device):
"""Initialize MAX! Cube BinarySensorEntity."""
room = handler.cube.room_by_id(device.room_id)
self._name = f"{room.name} {device.name}"
self._cubehandle = handler
self._device = device
self._room = handler.cube.room_by_id(device.room_id)
@property
def name(self):
"""Return the name of the BinarySensorEntity."""
return self._name
def update(self):
"""Get latest data from MAX! Cube."""
self._cubehandle.update()
@property
def unique_id(self):
"""Return a unique ID."""
return self._device.serial
@property
def device_class(self):
"""Return the class of this sensor."""
return DEVICE_CLASS_WINDOW
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
@property
def is_on(self):
"""Return true if the binary sensor is on/open."""
return self._device.is_open
def update(self):
"""Get latest data from MAX! Cube."""
self._cubehandle.update()
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