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

prevent errors on "lumi.gateway.mieu01" #732

Merged
merged 10 commits into from Jun 26, 2020
49 changes: 45 additions & 4 deletions miio/gateway.py
Expand Up @@ -166,6 +166,7 @@ def __init__(
self._zigbee = GatewayZigbee(parent=self)
self._light = GatewayLight(parent=self)
self._devices = {}
self._info = None

@property
def alarm(self) -> "GatewayAlarm":
Expand Down Expand Up @@ -193,6 +194,14 @@ def devices(self):
"""Return a dict of the already discovered devices."""
return self._devices

@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

@command()
def discover_devices(self):
"""
Expand Down Expand Up @@ -252,9 +261,18 @@ def discover_devices(self):
DeviceType.D1WallSwitchTripleNN: D1WallSwitchTripleNN,
DeviceType.ThermostatS2: ThermostatS2,
}
devices_raw = self.get_prop("device_list")
self._devices = {}

# Skip the models which do not support getting the device list
if self.model == "lumi.gateway.mieu01":
_LOGGER.info(
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
"Gateway model '%s' does not (yet) support getting the device list",
self.model,
)
return self._devices
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved

devices_raw = self.get_prop("device_list")

for x in range(0, len(devices_raw), 5):
# Extract discovered information
dev_info = SubDeviceInfo(*devices_raw[x : x + 5])
Expand Down Expand Up @@ -706,6 +724,7 @@ def __init__(self, gw: Gateway = None, dev_info: SubDeviceInfo = None,) -> None:
self._gw = gw
self.sid = dev_info.sid
self._battery = None
self._voltage = None
self._fw_ver = dev_info.fw_ver
self._props = self.props()
try:
Expand All @@ -715,14 +734,15 @@ def __init__(self, gw: Gateway = None, dev_info: SubDeviceInfo = None,) -> None:

def __repr__(self):
return (
"<Subdevice %s: %s, model: %s, zigbee: %s, fw: %s, bat: %s, props: %s>"
"<Subdevice %s: %s, model: %s, zigbee: %s, fw: %s, bat: %s, vol: %s, props: %s>"
% (
self.device_type,
self.sid,
self.model,
self.zigbee_model,
self.firmware_version,
self.get_battery(),
self.get_voltage(),
self.status,
)
)
Expand Down Expand Up @@ -759,9 +779,14 @@ def firmware_version(self):

@property
def battery(self):
"""Return the battery level."""
"""Return the battery level in %."""
return self._battery

@property
def voltage(self):
"""Return the battery voltage in V."""
return self._voltage

@command()
def update(self):
"""Update the device-specific properties."""
Expand Down Expand Up @@ -847,9 +872,25 @@ def unpair(self):
@command()
def get_battery(self):
"""Update the battery level and return the new value."""
self._battery = self.send("get_battery").pop()
if self._gw.model != "lumi.gateway.mieu01":
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
self._battery = self.send("get_battery").pop()
else:
_LOGGER.info(
"Gateway model '%s' does not (yet) support get_battery", self._gw.model,
)
return self._battery

@command()
def get_voltage(self):
"""Update the battery voltage and return the new value."""
if self._gw.model == "lumi.gateway.mieu01":
self._voltage = self.get_property("voltage").pop() / 1000
else:
_LOGGER.info(
"Gateway model '%s' does not (yet) support get_voltage", self._gw.model,
)
return self._voltage

@command()
def get_firmware_version(self) -> Optional[int]:
"""Returns firmware version."""
Expand Down