Add battery binary sensor to homematic (#23067)

* first proposal

* parameter rename

* retrigger CI

* remove separate binary sensor

* remove batter_sensor

* battery device distinction at binary sensor discovery
This commit is contained in:
sander76 2019-05-09 19:38:51 +02:00 committed by Paulus Schoutsen
parent c7a78ed522
commit 8ef3c6d4d3
2 changed files with 58 additions and 7 deletions

View file

@ -2,12 +2,16 @@
import logging
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.const import STATE_UNKNOWN
from homeassistant.components.homematic import ATTR_BATTERY_DEVICES
from homeassistant.const import STATE_UNKNOWN, DEVICE_CLASS_BATTERY
from . import ATTR_DISCOVER_DEVICES, HMDevice
_LOGGER = logging.getLogger(__name__)
ATTR_LOW_BAT = 'LOW_BAT'
ATTR_LOWBAT = 'LOWBAT'
SENSOR_TYPES_CLASS = {
'IPShutterContact': 'opening',
'MaxShutterContact': 'opening',
@ -30,8 +34,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
return
devices = []
battery_devices = discovery_info[ATTR_BATTERY_DEVICES]
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
new_device = HMBinarySensor(conf)
if battery_devices:
battery_device = conf.get(ATTR_LOWBAT) or conf.get(ATTR_LOW_BAT)
if battery_device:
new_device = HMBatterySensor(conf)
else:
new_device = HMBinarySensor(conf)
devices.append(new_device)
add_entities(devices)
@ -60,3 +71,26 @@ class HMBinarySensor(HMDevice, BinarySensorDevice):
# Add state to data struct
if self._state:
self._data.update({self._state: STATE_UNKNOWN})
class HMBatterySensor(HMDevice, BinarySensorDevice):
"""Representation of an HomeMatic low battery sensor."""
@property
def device_class(self):
"""Return battery as a device class."""
return DEVICE_CLASS_BATTERY
@property
def is_on(self):
"""Return True if battery is low."""
is_on = self._data.get(ATTR_LOW_BAT, False) or self._data.get(
ATTR_LOWBAT, False
)
return is_on
def _init_data_struct(self):
"""Generate the data dictionary (self._data) from metadata."""
# Add state to data struct
if self._state:
self._data.update({self._state: STATE_UNKNOWN})