Skip to content

Commit

Permalink
Improve gateway get_devices_from_dict (#1456)
Browse files Browse the repository at this point in the history
  • Loading branch information
starkillerOG authored Jul 15, 2022
1 parent ec64f0c commit 8402039
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions miio/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def __init__(
self._devices: Dict[str, SubDevice] = {}
self._info = None
self._subdevice_model_map = None
self._did = None

def _get_unknown_model(self):
for model_info in self.subdevice_model_map:
Expand Down Expand Up @@ -210,6 +209,14 @@ def discover_devices(self):

return self._devices

def _get_device_by_did(self, device_dict, device_did):
"""Get a device by its did from a device dict."""
for device in device_dict:
if device["did"] == device_did:
return device

return None

@command()
def get_devices_from_dict(self, device_dict):
"""Get SubDevices from a dict containing at least "mac", "did", "parent_id" and
Expand All @@ -222,34 +229,40 @@ def get_devices_from_dict(self, device_dict):
self._devices = {}

# find the gateway
for device in device_dict:
if device["mac"] == self.mac:
self._did = device["did"]
break
gateway = self._get_device_by_did(device_dict, str(self.device_id))
if gateway is None:
_LOGGER.error(
"Could not find gateway with ip '%s', mac '%s', did '%i', model '%s' in the cloud device list response",
self.ip,
self.mac,
self.device_id,
self.model,
)
return self._devices

# check if the gateway is found
if self._did is None:
if gateway["mac"] != self.mac:
_LOGGER.error(
"Could not find gateway with ip '%s', mac '%s', model '%s' in the cloud device list response",
"Mac and device id of gateway with ip '%s', mac '%s', did '%i', model '%s' did not match in the cloud device list response",
self.ip,
self.mac,
self.device_id,
self.model,
)
return self._devices

# find the subdevices belonging to this gateway
for device in device_dict:
if device.get("parent_id") == self._did:
# Match 'model' to get the type_id
model_info = self.match_zigbee_model(device["model"], device["did"])
if device.get("parent_id") != str(self.device_id):
continue

# Extract discovered information
dev_info = SubDeviceInfo(
device["did"], model_info["type_id"], -1, -1, -1
)
# Match 'model' to get the type_id
model_info = self.match_zigbee_model(device["model"], device["did"])

# Setup the device
self.setup_device(dev_info, model_info)
# Extract discovered information
dev_info = SubDeviceInfo(device["did"], model_info["type_id"], -1, -1, -1)

# Setup the device
self.setup_device(dev_info, model_info)

return self._devices

Expand Down

0 comments on commit 8402039

Please sign in to comment.