Add devolo remote devices as binary sensors (#39105)

This commit is contained in:
Markus Bong 2020-09-07 15:48:14 +02:00 committed by GitHub
parent 007873153e
commit 7c56ee8e0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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()