A more robust way of splitting devices provided in the <ieee_address:value> format

This commit is contained in:
Fabio Manganiello 2022-11-02 22:49:19 +01:00
parent 7db84acd34
commit 636d1ced3a
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -1,4 +1,5 @@
import json import json
import re
import threading import threading
from queue import Queue from queue import Queue
@ -1396,6 +1397,7 @@ class ZigbeeMqttPlugin(MqttPlugin): # lgtm [py/missing-call-to-init]
) )
def _get_switch_info(self, device: str): def _get_switch_info(self, device: str):
device = self._ieee_address(device)
switches_info = self._get_switches_info() switches_info = self._get_switches_info()
info = switches_info.get(device) info = switches_info.get(device)
if info: if info:
@ -1430,11 +1432,19 @@ class ZigbeeMqttPlugin(MqttPlugin): # lgtm [py/missing-call-to-init]
) )
@staticmethod @staticmethod
def _ieee_address(device: dict) -> str: def _ieee_address(device: Union[dict, str]) -> str:
# Entity value IDs are stored in the `<address>:<property>` # Entity value IDs are stored in the `<address>:<property>`
# format. Therefore, we need to split by `:` if we want to # format. Therefore, we need to split by `:` if we want to
# retrieve the original address. # retrieve the original address.
return device['ieee_address'].split(':')[0] if isinstance(device, dict):
dev = device['ieee_address']
else:
dev = device
# IEEE address + property format
if re.search(r'^0x[0-9a-fA-F]{16}:', dev):
return dev.split(':')[0]
return dev
@classmethod @classmethod
def _get_switch_meta(cls, device_info: dict) -> dict: def _get_switch_meta(cls, device_info: dict) -> dict: