Skip to content

Commit

Permalink
gateway: prevent errors on "lumi.gateway.mieu01" (rytilahti#732)
Browse files Browse the repository at this point in the history
* prevent errors on "lumi.gateway.mieu01"

* add voltage and better support "lumi.gateway.mieu01"

* styling

* fix isort

* Update comments

Co-authored-by: Teemu R. <tpr@iki.fi>

* add log message

* fix black

* add gateway model constants

* black formatting

* change info to warning log

Co-authored-by: Teemu R. <tpr@iki.fi>
  • Loading branch information
2 people authored and xvlady committed May 9, 2021
1 parent 9ac1beb commit 986c42d
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions miio/gateway.py
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 @@ -169,6 +173,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 @@ -196,6 +201,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 @@ -255,9 +268,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

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 @@ -709,6 +731,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 @@ -718,14 +741,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 @@ -762,9 +786,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 @@ -849,10 +878,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

0 comments on commit 986c42d

Please sign in to comment.