Add channel-mapping for HomeMatic (#31178)

* Update entity.py

* Fix Black

* Fix Black

* Update entity.py

* Use new style python

* Simplify

* Allow multible attribute with different channels

* Black

Co-authored-by: Pascal Vizeli <pascal.vizeli@syshack.ch>
This commit is contained in:
Daniel Perna 2020-01-29 12:02:36 +01:00 committed by GitHub
parent bcdef4e500
commit 9902b648fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,6 +40,7 @@ class HMDevice(Entity):
self._hmdevice = None
self._connected = False
self._available = False
self._channel_map = set()
# Set parameter to uppercase
if self._state:
@ -110,13 +111,10 @@ class HMDevice(Entity):
def _hm_event_callback(self, device, caller, attribute, value):
"""Handle all pyhomematic device events."""
_LOGGER.debug("%s received event '%s' value: %s", self._name, attribute, value)
has_changed = False
# Is data needed for this instance?
if attribute in self._data:
# Did data change?
if self._data[attribute] != value:
if f"{attribute}:{device.partition(':')[2]}" in self._channel_map:
self._data[attribute] = value
has_changed = True
@ -131,9 +129,6 @@ class HMDevice(Entity):
def _subscribe_homematic_events(self):
"""Subscribe all required events to handle job."""
channels_to_sub = set()
# Push data to channels_to_sub from hmdevice metadata
for metadata in (
self._hmdevice.SENSORNODE,
self._hmdevice.BINARYNODE,
@ -150,19 +145,11 @@ class HMDevice(Entity):
channel = channels[0]
else:
channel = self._channel
# Prepare for subscription
try:
channels_to_sub.add(int(channel))
except (ValueError, TypeError):
_LOGGER.error("Invalid channel in metadata from %s", self._name)
# Remember the channel for this attribute to ignore invalid events later
self._channel_map.add(f"{node}:{channel!s}")
# Set callbacks
for channel in channels_to_sub:
_LOGGER.debug("Subscribe channel %d from %s", channel, self._name)
self._hmdevice.setEventCallback(
callback=self._hm_event_callback, bequeath=False, channel=channel
)
self._hmdevice.setEventCallback(callback=self._hm_event_callback, bequeath=True)
def _load_data_from_hm(self):
"""Load first value from pyhomematic."""