diff --git a/miio/gateway/gateway.py b/miio/gateway/gateway.py index c9fa3cb5d..c8bdbe8a4 100644 --- a/miio/gateway/gateway.py +++ b/miio/gateway/gateway.py @@ -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: @@ -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 @@ -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