diff --git a/miio/airpurifier.py b/miio/airpurifier.py index b6b9a35f5..42a3a100d 100644 --- a/miio/airpurifier.py +++ b/miio/airpurifier.py @@ -109,9 +109,12 @@ def illuminance(self) -> Optional[int]: return self.data["bright"] @property - def buzzer(self) -> bool: + def buzzer(self) -> Optional[bool]: """Return True if buzzer is on.""" - return self.data["buzzer"] == "on" + if self.data["buzzer"] is not None: + return self.data["buzzer"] == "on" + + return None @property def child_lock(self) -> bool: @@ -279,4 +282,7 @@ def set_child_lock(self, lock: bool): def set_volume(self, volume: int): """Set volume of sound notifications [0-100].""" + if volume < 0 or volume > 100: + raise AirPurifierException("Invalid volume: %s" % volume) + return self.send("set_volume", [volume]) diff --git a/miio/tests/test_airpurifier.py b/miio/tests/test_airpurifier.py index 48069b110..94c2a55aa 100644 --- a/miio/tests/test_airpurifier.py +++ b/miio/tests/test_airpurifier.py @@ -23,9 +23,10 @@ def __init__(self, *args, **kwargs): 'f1_hour': 3500, 'led': 'off', 'led_b': 2, - 'bright': None, + 'bright': 83, 'buzzer': 'off', - 'child_lock': 'off' + 'child_lock': 'off', + 'volume': 50, } self.return_values = { 'get_prop': self._get_state, @@ -37,6 +38,7 @@ def __init__(self, *args, **kwargs): 'set_level_favorite': lambda x: self._set_state("favorite_level", x), 'set_led_b': lambda x: self._set_state("led_b", x), + 'set_volume': lambda x: self._set_state("volume", x), } super().__init__(args, kwargs) @@ -91,6 +93,8 @@ def test_status(self): assert self.state().led_brightness == LedBrightness(self.device.start_state["led_b"]) assert self.state().buzzer == (self.device.start_state["buzzer"] == 'on') assert self.state().child_lock == (self.device.start_state["child_lock"] == 'on') + assert self.state().illuminance == self.device.start_state["bright"] + assert self.state().volume == self.device.start_state["volume"] def test_set_mode(self): def mode(): @@ -117,6 +121,7 @@ def favorite_level(): self.device.set_favorite_level(6) assert favorite_level() == 6 self.device.set_favorite_level(10) + assert favorite_level() == 10 with pytest.raises(AirPurifierException): self.device.set_favorite_level(-1) @@ -141,7 +146,6 @@ def test_set_led(self): def led(): return self.device.status().led - # The LED brightness of a Air Purifier Pro cannot be set so far. self.device.set_led(True) assert led() is True @@ -168,24 +172,50 @@ def child_lock(): self.device.set_child_lock(False) assert child_lock() is False - def test_status_without_led_b_and_with_bright(self): - self.device._reset_state() + def test_set_volume(self): + def volume(): + return self.device.status().volume + + self.device.set_volume(0) + assert volume() == 0 + self.device.set_volume(35) + assert volume() == 35 + self.device.set_volume(100) + assert volume() == 100 + + with pytest.raises(AirPurifierException): + self.device.set_volume(-1) - self.device.state["bright"] = self.device.state["led_b"] - del self.device.state["led_b"] + with pytest.raises(AirPurifierException): + self.device.set_volume(101) + + def test_status_without_volume(self): + self.device._reset_state() - assert self.state().led_brightness == LedBrightness( - self.device.start_state["led_b"]) + # The Air Purifier 2 doesn't support volume + self.device.state["volume"] = None + assert self.state().volume is None - def test_status_without_led_brightness_at_all(self): + def test_status_without_led_brightness(self): self.device._reset_state() + # The Air Purifier Pro doesn't support LED brightness self.device.state["led_b"] = None - self.device.state["bright"] = None assert self.state().led_brightness is None def test_status_without_temperature(self): self.device._reset_state() self.device.state["temp_dec"] = None - assert self.state().temperature is None + + def test_status_without_illuminance(self): + self.device._reset_state() + # The Air Purifier 2 doesn't provide illuminance + self.device.state["bright"] = None + assert self.state().illuminance is None + + def test_status_without_buzzer(self): + self.device._reset_state() + # The Air Purifier Pro doesn't provide the buzzer property + self.device.state["buzzer"] = None + assert self.state().buzzer is None