From 7c56ee8e0ccee2014018ee482ea337ed92313a91 Mon Sep 17 00:00:00 2001 From: Markus Bong <2Fake1987@gmail.com> Date: Mon, 7 Sep 2020 15:48:14 +0200 Subject: [PATCH] Add devolo remote devices as binary sensors (#39105) --- .../devolo_home_control/binary_sensor.py | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/devolo_home_control/binary_sensor.py b/homeassistant/components/devolo_home_control/binary_sensor.py index d3da333b407..e05350dbbac 100644 --- a/homeassistant/components/devolo_home_control/binary_sensor.py +++ b/homeassistant/components/devolo_home_control/binary_sensor.py @@ -41,7 +41,20 @@ async def async_setup_entry( element_uid=binary_sensor, ) ) - + for device in hass.data[DOMAIN]["homecontrol"].devices.values(): + if hasattr(device, "remote_control_property"): + for remote in device.remote_control_property: + for index in range( + 1, device.remote_control_property[remote].key_count + 1 + ): + entities.append( + DevoloRemoteControl( + homecontrol=hass.data[DOMAIN]["homecontrol"], + device_instance=device, + element_uid=remote, + key=index, + ) + ) async_add_entities(entities, False) @@ -97,3 +110,48 @@ class DevoloBinaryDeviceEntity(DevoloDeviceEntity, BinarySensorEntity): else: _LOGGER.debug("No valid message received: %s", message) self.schedule_update_ha_state() + + +class DevoloRemoteControl(DevoloDeviceEntity, BinarySensorEntity): + """Representation of a remote control within devolo Home Control.""" + + def __init__(self, homecontrol, device_instance, element_uid, key): + """Initialize a devolo remote control.""" + self._remote_control_property = device_instance.remote_control_property.get( + element_uid + ) + super().__init__( + homecontrol=homecontrol, + device_instance=device_instance, + element_uid=f"{element_uid}_{key}", + name=device_instance.item_name, + sync=self._sync, + ) + + self._key = key + + self._state = False + + self._subscriber = None + + @property + def is_on(self): + """Return the state.""" + return self._state + + def _sync(self, message=None): + """Update the binary sensor state.""" + if ( + message[0] == self._remote_control_property.element_uid + and message[1] == self._key + ): + self._state = True + elif ( + message[0] == self._remote_control_property.element_uid and message[1] == 0 + ): + self._state = False + elif message[0].startswith("hdm"): + self._available = self._device_instance.is_online() + else: + _LOGGER.debug("No valid message received: %s", message) + self.schedule_update_ha_state()