Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve gateway get_devices_from_dict #1456

Merged
merged 4 commits into from
Jul 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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