Skip to content

Commit

Permalink
Added buzzer_volumw and handling missing features for miot
Browse files Browse the repository at this point in the history
  • Loading branch information
foxel committed Feb 29, 2020
1 parent 6feed42 commit 31b0248
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
34 changes: 31 additions & 3 deletions miio/airpurifier_miot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
"filter_hours_used": {"siid": 4, "piid": 5},
# Alarm (siid=5)
"buzzer": {"siid": 5, "piid": 1},
"buzzer_volume": {"siid": 5, "piid": 2},
# Indicator Light (siid=6)
"led_brightness": {"siid": 6, "piid": 1},
"led": {"siid": 6, "piid": 6},
# Physical Control Locked (siid=7)
"child_lock": {"siid": 7, "piid": 1},
# Motor Speed (siid=10)
"favorite_level": {"siid": 10, "piid": 10},
"set_favorite_rpm": {"siid": 10, "piid": 7},
"favorite_rpm": {"siid": 10, "piid": 7},
"motor_speed": {"siid": 10, "piid": 8},
# Use time (siid=12)
"use_time": {"siid": 12, "piid": 1},
Expand Down Expand Up @@ -137,6 +138,14 @@ def buzzer(self) -> Optional[bool]:

return None

@property
def buzzer_volume(self) -> Optional[int]:
"""Return buzzer volume."""
if self.data["buzzer_volume"] is not None:
return self.data["buzzer_volume"]

return None

@property
def child_lock(self) -> bool:
"""Return True if child lock is on."""
Expand Down Expand Up @@ -206,6 +215,7 @@ def __repr__(self) -> str:
"led=%s, "
"led_brightness=%s, "
"buzzer=%s, "
"buzzer_volume=%s, "
"child_lock=%s, "
"favorite_level=%s, "
"filter_life_remaining=%s, "
Expand All @@ -227,6 +237,7 @@ def __repr__(self) -> str:
self.led,
self.led_brightness,
self.buzzer,
self.buzzer_volume,
self.child_lock,
self.favorite_level,
self.filter_life_remaining,
Expand Down Expand Up @@ -271,6 +282,7 @@ def __init__(
"LED: {result.led}\n"
"LED brightness: {result.led_brightness}\n"
"Buzzer: {result.buzzer}\n"
"Buzzer vol.: {result.buzzer_volume}\n"
"Child lock: {result.child_lock}\n"
"Favorite level: {result.favorite_level}\n"
"Filter life remaining: {result.filter_life_remaining} %\n"
Expand All @@ -287,7 +299,10 @@ def status(self) -> AirPurifierMiotStatus:
"""Retrieve properties."""

return AirPurifierMiotStatus(
{prop["did"]: prop["value"] for prop in self.get_properties()}
{
prop["did"]: prop["value"] if prop["code"] == 0 else None
for prop in self.get_properties()
}
)

@command(default_output=format_output("Powering on"))
Expand Down Expand Up @@ -322,7 +337,20 @@ def set_favorite_rpm(self, rpm: int):
"Invalid favorite motor speed: %s. Must be between 300 and 2300 and divisible by 10"
% rpm
)
return self.set_property("set_favorite_rpm", rpm)
return self.set_property("favorite_rpm", rpm)

@command(
click.argument("volume", type=int),
default_output=format_output("Setting buzzer volume '{volume}'"),
)
def set_buzzer_volume(self, volume: int):
"""Set favorite motor speed."""
# Note: documentation says the maximum is 2300, however, the purifier may return an error for rpm over 2200.
if volume < 0 or volume > 100:
raise AirPurifierMiotException(
"Invalid buzzer volume: %s. Must be between 0 and 100" % volume
)
return self.set_property("buzzer_volume", volume)

@command(
click.argument("mode", type=EnumType(OperationMode, False)),
Expand Down
2 changes: 1 addition & 1 deletion miio/tests/dummies.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DummyMiotDevice(DummyDevice):

def __init__(self, *args, **kwargs):
# {prop["did"]: prop["value"] for prop in self.miot_client.get_properties()}
self.state = [{"did": k, "value": v} for k, v in self.state.items()]
self.state = [{"did": k, "value": v, "code": 0} for k, v in self.state.items()]
super().__init__(*args, **kwargs)

def get_properties(self):
Expand Down
1 change: 1 addition & 0 deletions miio/tests/test_airpurifier_miot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"led": True,
"led_brightness": 1,
"buzzer": False,
"buzzer_volume": 0,
"child_lock": False,
"favorite_level": 10,
"filter_life_remaining": 80,
Expand Down

0 comments on commit 31b0248

Please sign in to comment.