From c2e87ef01de184b69b714ee945c487f8ad2f4d20 Mon Sep 17 00:00:00 2001 From: starkillerOG Date: Mon, 8 Feb 2021 00:24:42 +0100 Subject: [PATCH] add support for EU_Gateway --- miio/gateway/gateway.py | 55 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/miio/gateway/gateway.py b/miio/gateway/gateway.py index 65e69e28d..a218d375c 100644 --- a/miio/gateway/gateway.py +++ b/miio/gateway/gateway.py @@ -97,6 +97,7 @@ def __init__( self._devices = {} self._info = None self._subdevice_model_map = None + self._did = None def _get_unknown_model(self): for model_info in self.subdevice_model_map: @@ -129,10 +130,16 @@ def devices(self): """Return a dict of the already discovered devices.""" return self._devices + @property + def mac(self): + """Return the mac address of the gateway.""" + if self._info is None: + self._info = self.info() + return self._info.mac_address + @property def model(self): """Return the zigbee model of the gateway.""" - # Check if catch already has the gateway info, otherwise get it from the device if self._info is None: self._info = self.info() return self._info.model @@ -154,7 +161,8 @@ def discover_devices(self): # Skip the models which do not support getting the device list if self.model == GATEWAY_MODEL_EU: _LOGGER.warning( - "Gateway model '%s' does not (yet) support getting the device list", + "Gateway model '%s' does not (yet) support getting the device list, " + "try using the get_devices_from_dict function with micloud", self.model, ) return self._devices @@ -190,6 +198,49 @@ def discover_devices(self): return self._devices + @command() + def get_devices_from_dict(self, device_dict): + """Get SubDevices from a dict containing at least "mac", "did", "parent_id" and + "model". + + This dict can be obtained with the micloud package: + https://github.com/squachen/micloud + """ + + self._devices = {} + + # find the gateway + for device in device_dict: + if device["mac"] == self.mac: + self._did = device["did"] + break + + # check if the gateway is found + if self._did is None: + _LOGGER.error( + "Could not find gateway with ip '%s', mac '%s', model '%s' in the cloud device list response", + self.ip, + self.mac, + 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"]) + + # 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 + @command(click.argument("zigbee_model", "sid")) def match_zigbee_model(self, zigbee_model, sid): """Match the zigbee_model to obtain the model_info."""