Skip to content

Commit

Permalink
Tests of the Air Purifier improved to cover the features of the Air P…
Browse files Browse the repository at this point in the history
…urifier 2 and Air Purifier Pro.

Tests for the new properties (volume, brightness) added.
  • Loading branch information
syssi committed Jan 23, 2018
1 parent 9202f1d commit ac0d5b0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
10 changes: 8 additions & 2 deletions miio/airpurifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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])
54 changes: 42 additions & 12 deletions miio/tests/test_airpurifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down Expand Up @@ -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():
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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

0 comments on commit ac0d5b0

Please sign in to comment.