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
55 changes: 50 additions & 5 deletions miio/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

_LOGGER = logging.getLogger(__name__)

GATEWAY_MODEL_CHINA = "lumi.gateway.v3"
GATEWAY_MODEL_EU = "lumi.gateway.mieu01"
GATEWAY_MODEL_ZIG3 = "lumi.gateway.mgl03"
GATEWAY_MODEL_AQARA = "lumi.gateway.aqhm01"

color_map = {
"red": (255, 0, 0),
Expand Down Expand Up @@ -166,6 +170,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 +198,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 +265,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 == GATEWAY_MODEL_EU:
_LOGGER.warning(
"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 +728,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 +738,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 +783,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 @@ -846,10 +875,26 @@ def unpair(self):

@command()
def get_battery(self):
"""Update the battery level and return the new value."""
self._battery = self.send("get_battery").pop()
"""Update the battery level, if available."""
if self._gw.model != GATEWAY_MODEL_EU:
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, if available."""
if self._gw.model == GATEWAY_MODEL_EU:
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