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

add method to load subdevices from dict (EU gateway support) #936

Merged
merged 1 commit into from
Feb 12, 2021
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
55 changes: 53 additions & 2 deletions miio/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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."""
Expand Down