From 73ec545e45d10983ac8572b9ed2d75f10c0e8ea4 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 26 Nov 2017 19:30:47 +0100 Subject: [PATCH 1/7] Unit tests for philip lights introduced. --- miio/philips_eyecare.py | 2 +- miio/tests/test_ceil.py | 53 +++++++++++ miio/tests/test_philips_bulb.py | 53 +++++++++++ miio/tests/test_philips_eyecare.py | 148 +++++++++++++++++++++++++++++ 4 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 miio/tests/test_ceil.py create mode 100644 miio/tests/test_philips_bulb.py create mode 100644 miio/tests/test_philips_eyecare.py diff --git a/miio/philips_eyecare.py b/miio/philips_eyecare.py index 8f56c4bab..38b49f0f5 100644 --- a/miio/philips_eyecare.py +++ b/miio/philips_eyecare.py @@ -11,7 +11,7 @@ class PhilipsEyecareStatus: def __init__(self, data: Dict[str, Any]) -> None: # ['power': 'off', 'bright': 5, 'notifystatus': 'off', - # 'ambstatus': 'off': 'ambvalue': 41, 'eyecare': 'on', + # 'ambstatus': 'off', 'ambvalue': 41, 'eyecare': 'on', # 'scene_num': 3, 'bls': 'on', 'dvalue': 0] self.data = data diff --git a/miio/tests/test_ceil.py b/miio/tests/test_ceil.py new file mode 100644 index 000000000..07c20c3a3 --- /dev/null +++ b/miio/tests/test_ceil.py @@ -0,0 +1,53 @@ +from unittest import TestCase +from miio import Ceil +from .dummies import DummyDevice +import pytest + + +class DummyCeil(DummyDevice, Ceil): + def __init__(self, *args, **kwargs): + self.state = { + 'power': 'on', + } + self.return_values = { + 'get_prop': self._get_state, + 'set_power': lambda x: self._set_state("power", x), + } + super().__init__(args, kwargs) + + +@pytest.fixture(scope="class") +def ceil(request): + request.cls.device = DummyCeil() + # TODO add ability to test on a real device + + +@pytest.mark.usefixtures("ceil") +class TestCeil(TestCase): + def is_on(self): + return self.device.status().is_on + + def state(self): + return self.device.status() + + def test_on(self): + self.device.off() # ensure off + + start_state = self.is_on() + assert start_state is False + + self.device.on() + assert self.is_on() is True + + def test_off(self): + self.device.on() # ensure on + + assert self.is_on() is True + self.device.off() + assert self.is_on() is False + + def test_status(self): + self.device._reset_state() + + assert self.is_on() is True + diff --git a/miio/tests/test_philips_bulb.py b/miio/tests/test_philips_bulb.py new file mode 100644 index 000000000..32fb97ae7 --- /dev/null +++ b/miio/tests/test_philips_bulb.py @@ -0,0 +1,53 @@ +from unittest import TestCase +from miio import PhilipsBulb +from .dummies import DummyDevice +import pytest + + +class DummyPhilipsBulb(DummyDevice, PhilipsBulb): + def __init__(self, *args, **kwargs): + self.state = { + 'power': 'on', + } + self.return_values = { + 'get_prop': self._get_state, + 'set_power': lambda x: self._set_state("power", x), + } + super().__init__(args, kwargs) + + +@pytest.fixture(scope="class") +def philips_bulb(request): + request.cls.device = DummyPhilipsBulb() + # TODO add ability to test on a real device + + +@pytest.mark.usefixtures("philips_bulb") +class TestPhilipsBulb(TestCase): + def is_on(self): + return self.device.status().is_on + + def state(self): + return self.device.status() + + def test_on(self): + self.device.off() # ensure off + + start_state = self.is_on() + assert start_state is False + + self.device.on() + assert self.is_on() is True + + def test_off(self): + self.device.on() # ensure on + + assert self.is_on() is True + self.device.off() + assert self.is_on() is False + + def test_status(self): + self.device._reset_state() + + assert self.is_on() is True + diff --git a/miio/tests/test_philips_eyecare.py b/miio/tests/test_philips_eyecare.py new file mode 100644 index 000000000..2a3e9363d --- /dev/null +++ b/miio/tests/test_philips_eyecare.py @@ -0,0 +1,148 @@ +from unittest import TestCase +from miio import PhilipsEyecare +from .dummies import DummyDevice +import pytest + + +class DummyPhilipsEyecare(DummyDevice, PhilipsEyecare): + def __init__(self, *args, **kwargs): + self.state = { + 'power': 'on', + 'bright': 5, + 'notifystatus': 'off', + 'ambstatus': 'off', + 'ambvalue': 41, + 'eyecare': 'on', + 'scene_num': 3, + 'bls': 'on', + 'dvalue': 0, + } + self.return_values = { + 'get_prop': self._get_state, + 'set_power': lambda x: self._set_state("power", x), + 'set_eyecare': lambda x: self._set_state("eyecare", x), + 'set_bright': lambda x: self._set_state("bright", x), + 'set_user_scene': lambda x: self._set_state("scene_num", x), + 'delay_off': lambda x: self._set_state("dvalue", x), + 'enable_bl': lambda x: self._set_state("bls", x), + 'set_notifyuser': lambda x: self._set_state("notifystatus", x), + 'enable_amb': lambda x: self._set_state("ambstatus", x), + 'set_amb_bright': lambda x: self._set_state("ambvalue", x), + } + super().__init__(args, kwargs) + + +@pytest.fixture(scope="class") +def philips_eyecare(request): + request.cls.device = DummyPhilipsEyecare() + # TODO add ability to test on a real device + + +@pytest.mark.usefixtures("philips_eyecare") +class TestPhilipsEyecare(TestCase): + def is_on(self): + return self.device.status().is_on + + def state(self): + return self.device.status() + + def test_on(self): + self.device.off() # ensure off + + start_state = self.is_on() + assert start_state is False + + self.device.on() + assert self.is_on() is True + + def test_off(self): + self.device.on() # ensure on + + assert self.is_on() is True + self.device.off() + assert self.is_on() is False + + def test_status(self): + self.device._reset_state() + + assert self.is_on() is True + assert self.state().brightness == self.device.start_state["bright"] + assert self.state().reminder == (self.device.start_state["notifystatus"] == 'on') + assert self.state().ambient == (self.device.start_state["ambstatus"] == 'on') + assert self.state().ambient_brightness == self.device.start_state["ambvalue"] + assert self.state().eyecare == (self.device.start_state["eyecare"] == 'on') + assert self.state().scene == self.device.start_state["scene_num"] + assert self.state().smart_night_light == (self.device.start_state["bls"] == 'on') + assert self.state().delay_off_countdown == self.device.start_state["dvalue"] + + def test_eyecare(self): + def eyecare(): + return self.device.status().eyecare + + self.device.eyecare_on() + assert eyecare() == True + self.device.eyecare_off() + assert eyecare() == False + + def test_set_brightness(self): + def brightness(): + return self.device.status().brightness + + self.device.set_brightness(10) + assert brightness() == 10 + self.device.set_brightness(20) + assert brightness() == 20 + + def test_set_scene(self): + def scene(): + return self.device.status().scene + + self.device.set_scene(1) + assert scene() == 1 + self.device.set_scene(2) + assert scene() == 2 + + def test_delay_off(self): + def delay_off_countdown(): + return self.device.status().delay_off_countdown + + self.device.delay_off(100) + assert delay_off_countdown() == 100 + self.device.delay_off(200) + assert delay_off_countdown() == 200 + + def test_smart_night_light(self): + def smart_night_light(): + return self.device.status().smart_night_light + + self.device.smart_night_light_on() + assert smart_night_light() == True + self.device.smart_night_light_off() + assert smart_night_light() == False + + def test_reminder(self): + def reminder(): + return self.device.status().reminder + + self.device.reminder_on() + assert reminder() == True + self.device.reminder_off() + assert reminder() == False + + def test_ambient(self): + def ambient(): + return self.device.status().ambient + + self.device.ambient_on() + assert ambient() == True + self.device.ambient_off() + assert ambient() == False + + def test_set_ambient_brightness(self): + def ambient_brightness(): + return self.device.status().ambient_brightness + + self.device.set_ambient_brightness(10) + assert ambient_brightness() == 10 + self.device.set_ambient_brightness(20) + assert ambient_brightness() == 20 From 49184f1cfa9943bc3d89afcc1bce127289bf2185 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 26 Nov 2017 19:54:40 +0100 Subject: [PATCH 2/7] Unit tests for the Xiaomi Ceiling Lamp added. --- miio/ceil.py | 15 ++++---- miio/tests/test_ceil.py | 79 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 10 deletions(-) diff --git a/miio/ceil.py b/miio/ceil.py index 441f3c05d..543d99fc1 100644 --- a/miio/ceil.py +++ b/miio/ceil.py @@ -10,9 +10,10 @@ class CeilStatus: """Container for status reports from Xiaomi Philips LED Ceiling Lamp.""" def __init__(self, data: Dict[str, Any]) -> None: - # ['power', 'bright', 'snm', 'dv', 'cctsw', 'bl', 'mb', 'ac', 'ms' - # 'sw', 'cct'] - # ['off', 0, 4, 0, [[0, 3], [0, 2], [0, 1]], 1, 1, 1, 1, 99] + # {'power': 'off', 'bright': 0, 'snm': 4, 'dv': 0, + # 'cctsw': [[0, 3], [0, 2], [0, 1]], 'bl': 1, + # 'mb': 1, 'ac': 1, 'mssw': 1, 'cct': 99} + # NOTE: Only 8 properties can be requested at the same time self.data = data @@ -47,14 +48,14 @@ def color_temperature(self) -> int: return self.data["cct"] @property - def smart_night_light(self) -> int: + def smart_night_light(self) -> bool: """Smart night mode state.""" - return self.data["bl"] + return self.data["bl"] == 1 @property - def automatic_color_temperature(self) -> int: + def automatic_color_temperature(self) -> bool: """Automatic color temperature state.""" - return self.data["ac"] + return self.data["ac"] == 1 def __str__(self) -> str: s = " Date: Sun, 26 Nov 2017 20:00:56 +0100 Subject: [PATCH 3/7] Unit tests for the Xiaomi Smart Light Bulb added. --- miio/philips_bulb.py | 2 +- miio/tests/test_philips_bulb.py | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/miio/philips_bulb.py b/miio/philips_bulb.py index 1ddc5f39e..8a0f7f7b8 100644 --- a/miio/philips_bulb.py +++ b/miio/philips_bulb.py @@ -10,7 +10,7 @@ class PhilipsBulbStatus: """Container for status reports from Xiaomi Philips LED Ceiling Lamp""" def __init__(self, data: Dict[str, Any]) -> None: - # ['power': 'on', 'bright': 85, 'cct': 9, 'snm': 0, 'dv': 0] + # {'power': 'on', 'bright': 85, 'cct': 9, 'snm': 0, 'dv': 0} self.data = data @property diff --git a/miio/tests/test_philips_bulb.py b/miio/tests/test_philips_bulb.py index 32fb97ae7..34db88319 100644 --- a/miio/tests/test_philips_bulb.py +++ b/miio/tests/test_philips_bulb.py @@ -8,6 +8,10 @@ class DummyPhilipsBulb(DummyDevice, PhilipsBulb): def __init__(self, *args, **kwargs): self.state = { 'power': 'on', + 'bright': 85, + 'cct': 9, + 'snm': 0, + 'dv': 0 } self.return_values = { 'get_prop': self._get_state, @@ -50,4 +54,46 @@ def test_status(self): self.device._reset_state() assert self.is_on() is True + assert self.state().brightness == self.device.start_state["bright"] + assert self.state().color_temperature == self.device.start_state["cct"] + assert self.state().scene == self.device.start_state["snm"] + assert self.state().delay_off_countdown == self.device.start_state["dv"] + + def set_brightness(self): + def brightness(): + return self.device.status().brightness + + self.device.set_brightness(10) + assert brightness() == 10 + self.device.set_brightness(20) + assert brightness() == 20 + + def set_color_temperature(self): + def color_temperature(): + return self.device.status().color_temperature + + self.device.set_color_temperature(30) + assert color_temperature() == 30 + self.device.set_color_temperature(20) + assert color_temperature() == 20 + + def delay_off(self): + def delay_off_countdown(): + return self.device.status().delay_off_countdown + + self.device.delay_off(100) + assert delay_off_countdown() == 100 + self.device.delay_off(200) + assert delay_off_countdown() == 200 + + def set_scene(self): + def scene(): + return self.device.status().scene + + self.device.set_scene(1) + assert scene() == 1 + self.device.set_scene(2) + assert scene() == 2 + + From dd0fed8a6c27afefa9c26ad3ac3fa878aae5c525 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 26 Nov 2017 20:12:01 +0100 Subject: [PATCH 4/7] Missing remapping added. --- miio/tests/test_ceil.py | 2 +- miio/tests/test_philips_bulb.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/miio/tests/test_ceil.py b/miio/tests/test_ceil.py index 7b6288f88..20100f7e5 100644 --- a/miio/tests/test_ceil.py +++ b/miio/tests/test_ceil.py @@ -123,4 +123,4 @@ def automatic_color_temperature(): self.device.automatic_color_temperature_on() assert automatic_color_temperature() == True self.device.automatic_color_temperature_off() - assert automatic_color_temperature() == False \ No newline at end of file + assert automatic_color_temperature() == False diff --git a/miio/tests/test_philips_bulb.py b/miio/tests/test_philips_bulb.py index 34db88319..bfb842ecf 100644 --- a/miio/tests/test_philips_bulb.py +++ b/miio/tests/test_philips_bulb.py @@ -16,6 +16,10 @@ def __init__(self, *args, **kwargs): self.return_values = { 'get_prop': self._get_state, 'set_power': lambda x: self._set_state("power", x), + 'set_bright': lambda x: self._set_state("bright", x), + 'set_cct': lambda x: self._set_state("cct", x), + 'delay_off': lambda x: self._set_state("dv", x), + 'apply_fixed_scene': lambda x: self._set_state("snm", x), } super().__init__(args, kwargs) @@ -94,6 +98,3 @@ def scene(): assert scene() == 1 self.device.set_scene(2) assert scene() == 2 - - - From 8fd6e183d11d99e6054551d99f166440731776ac Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 26 Nov 2017 20:37:39 +0100 Subject: [PATCH 5/7] Missing test_ prefixes added. --- miio/tests/test_philips_bulb.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/miio/tests/test_philips_bulb.py b/miio/tests/test_philips_bulb.py index bfb842ecf..9800d4765 100644 --- a/miio/tests/test_philips_bulb.py +++ b/miio/tests/test_philips_bulb.py @@ -63,7 +63,7 @@ def test_status(self): assert self.state().scene == self.device.start_state["snm"] assert self.state().delay_off_countdown == self.device.start_state["dv"] - def set_brightness(self): + def test_set_brightness(self): def brightness(): return self.device.status().brightness @@ -72,7 +72,7 @@ def brightness(): self.device.set_brightness(20) assert brightness() == 20 - def set_color_temperature(self): + def test_set_color_temperature(self): def color_temperature(): return self.device.status().color_temperature @@ -81,7 +81,7 @@ def color_temperature(): self.device.set_color_temperature(20) assert color_temperature() == 20 - def delay_off(self): + def test_delay_off(self): def delay_off_countdown(): return self.device.status().delay_off_countdown @@ -90,7 +90,7 @@ def delay_off_countdown(): self.device.delay_off(200) assert delay_off_countdown() == 200 - def set_scene(self): + def test_set_scene(self): def scene(): return self.device.status().scene From 7987a8785179fa3f4a9c2937975d48fbcd902c18 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 26 Nov 2017 21:09:01 +0100 Subject: [PATCH 6/7] Proper handling and testing of the limits added. --- miio/ceil.py | 22 ++++++++++-- miio/philips_bulb.py | 58 +++++++++++++++++++----------- miio/philips_eyecare.py | 23 ++++++++++-- miio/tests/test_philips_bulb.py | 52 ++++++++++++++++++++++----- miio/tests/test_philips_eyecare.py | 55 ++++++++++++++++++++++------ 5 files changed, 168 insertions(+), 42 deletions(-) diff --git a/miio/ceil.py b/miio/ceil.py index 543d99fc1..58fd882cf 100644 --- a/miio/ceil.py +++ b/miio/ceil.py @@ -6,6 +6,10 @@ _LOGGER = logging.getLogger(__name__) +class CeilException(Exception): + pass + + class CeilStatus: """Container for status reports from Xiaomi Philips LED Ceiling Lamp.""" @@ -101,19 +105,33 @@ def off(self): def set_brightness(self, level: int): """Set brightness level.""" + if level < 1 or level > 100: + raise CeilException("Invalid brightness: %s" % level) + return self.send("set_bright", [level]) def set_color_temperature(self, level: int): """Set Correlated Color Temperature.""" + if level < 1 or level > 100: + raise CeilException("Invalid color temperature: %s" % level) + return self.send("set_cct", [level]) def delay_off(self, seconds: int): """Set delay off seconds.""" + + if seconds < 1: + raise CeilException( + "Invalid value for a delayed turn off: %s" % seconds) + return self.send("delay_off", [seconds]) - def set_scene(self, num: int): + def set_scene(self, number: int): """Set scene number.""" - return self.send("apply_fixed_scene", [num]) + if number < 1 or number > 4: + raise CeilException("Invalid fixed scene number: %s" % number) + + return self.send("apply_fixed_scene", [number]) def smart_night_light_on(self): """Smart Night Light On.""" diff --git a/miio/philips_bulb.py b/miio/philips_bulb.py index 8a0f7f7b8..e1e3c05a5 100644 --- a/miio/philips_bulb.py +++ b/miio/philips_bulb.py @@ -6,6 +6,10 @@ _LOGGER = logging.getLogger(__name__) +class PhilipsBulbException(Exception): + pass + + class PhilipsBulbStatus: """Container for status reports from Xiaomi Philips LED Ceiling Lamp""" @@ -48,6 +52,25 @@ def __str__(self) -> str: class PhilipsBulb(Device): """Main class representing Xiaomi Philips LED Ball Lamp.""" + def status(self) -> PhilipsBulbStatus: + """Retrieve properties.""" + properties = ['power', 'bright', 'cct', 'snm', 'dv', ] + values = self.send( + "get_prop", + properties + ) + + properties_count = len(properties) + values_count = len(values) + if properties_count != values_count: + _LOGGER.debug( + "Count (%s) of requested properties does not match the " + "count (%s) of received values.", + properties_count, values_count) + + return PhilipsBulbStatus( + defaultdict(lambda: None, zip(properties, values))) + def on(self): """Power on.""" return self.send("set_power", ["on"]) @@ -58,35 +81,30 @@ def off(self): def set_brightness(self, level: int): """Set brightness level.""" + if level < 1 or level > 100: + raise PhilipsBulbException("Invalid brightness: %s" % level) + return self.send("set_bright", [level]) def set_color_temperature(self, level: int): """Set Correlated Color Temperature.""" + if level < 1 or level > 100: + raise PhilipsBulbException("Invalid color temperature: %s" % level) + return self.send("set_cct", [level]) def delay_off(self, seconds: int): """Set delay off seconds.""" - return self.send("delay_off", [seconds]) - def set_scene(self, num: int): - """Set scene number.""" - return self.send("apply_fixed_scene", [num]) + if seconds < 1: + raise PhilipsBulbException( + "Invalid value for a delayed turn off: %s" % seconds) - def status(self) -> PhilipsBulbStatus: - """Retrieve properties.""" - properties = ['power', 'bright', 'cct', 'snm', 'dv', ] - values = self.send( - "get_prop", - properties - ) + return self.send("delay_off", [seconds]) - properties_count = len(properties) - values_count = len(values) - if properties_count != values_count: - _LOGGER.debug( - "Count (%s) of requested properties does not match the " - "count (%s) of received values.", - properties_count, values_count) + def set_scene(self, number: int): + """Set scene number.""" + if number < 1 or number > 4: + raise PhilipsBulbException("Invalid fixed scene number: %s" % number) - return PhilipsBulbStatus( - defaultdict(lambda: None, zip(properties, values))) + return self.send("apply_fixed_scene", [number]) diff --git a/miio/philips_eyecare.py b/miio/philips_eyecare.py index 38b49f0f5..5a73455c2 100644 --- a/miio/philips_eyecare.py +++ b/miio/philips_eyecare.py @@ -6,6 +6,10 @@ _LOGGER = logging.getLogger(__name__) +class PhilipsEyecareException(Exception): + pass + + class PhilipsEyecareStatus: """Container for status reports from Xiaomi Philips Eyecare Smart Lamp 2""" @@ -118,14 +122,25 @@ def eyecare_off(self): def set_brightness(self, level: int): """Set brightness level.""" + if level < 1 or level > 100: + raise PhilipsEyecareException("Invalid brightness: %s" % level) + return self.send("set_bright", [level]) - def set_scene(self, num: int): + def set_scene(self, number: int): """Set eyecare user scene.""" - return self.send("set_user_scene", [num]) + if number < 1 or number > 4: + raise PhilipsEyecareException("Invalid fixed scene number: %s" % number) + + return self.send("set_user_scene", [number]) def delay_off(self, minutes: int): """Set delay off minutes.""" + + if minutes < 0: + raise PhilipsEyecareException( + "Invalid value for a delayed turn off: %s" % minutes) + return self.send("delay_off", [minutes]) def smart_night_light_on(self): @@ -154,4 +169,8 @@ def ambient_off(self): def set_ambient_brightness(self, level: int): """Set Ambient Light brightness level.""" + if level < 1 or level > 100: + raise PhilipsEyecareException( + "Invalid ambient brightness: %s" % level) + return self.send("set_amb_bright", [level]) diff --git a/miio/tests/test_philips_bulb.py b/miio/tests/test_philips_bulb.py index 9800d4765..7c2398674 100644 --- a/miio/tests/test_philips_bulb.py +++ b/miio/tests/test_philips_bulb.py @@ -1,5 +1,6 @@ from unittest import TestCase from miio import PhilipsBulb +from miio.philips_bulb import PhilipsBulbException from .dummies import DummyDevice import pytest @@ -8,8 +9,8 @@ class DummyPhilipsBulb(DummyDevice, PhilipsBulb): def __init__(self, *args, **kwargs): self.state = { 'power': 'on', - 'bright': 85, - 'cct': 9, + 'bright': 100, + 'cct': 10, 'snm': 0, 'dv': 0 } @@ -67,19 +68,39 @@ def test_set_brightness(self): def brightness(): return self.device.status().brightness - self.device.set_brightness(10) - assert brightness() == 10 - self.device.set_brightness(20) - assert brightness() == 20 + self.device.set_brightness(1) + assert brightness() == 1 + self.device.set_brightness(50) + assert brightness() == 50 + self.device.set_brightness(100) + + with pytest.raises(PhilipsBulbException): + self.device.set_brightness(-1) + + with pytest.raises(PhilipsBulbException): + self.device.set_brightness(0) + + with pytest.raises(PhilipsBulbException): + self.device.set_brightness(101) def test_set_color_temperature(self): def color_temperature(): return self.device.status().color_temperature - self.device.set_color_temperature(30) - assert color_temperature() == 30 self.device.set_color_temperature(20) assert color_temperature() == 20 + self.device.set_color_temperature(30) + assert color_temperature() == 30 + self.device.set_color_temperature(10) + + with pytest.raises(PhilipsBulbException): + self.device.set_color_temperature(-1) + + with pytest.raises(PhilipsBulbException): + self.device.set_color_temperature(0) + + with pytest.raises(PhilipsBulbException): + self.device.set_color_temperature(101) def test_delay_off(self): def delay_off_countdown(): @@ -90,6 +111,12 @@ def delay_off_countdown(): self.device.delay_off(200) assert delay_off_countdown() == 200 + with pytest.raises(PhilipsBulbException): + self.device.delay_off(-1) + + with pytest.raises(PhilipsBulbException): + self.device.delay_off(0) + def test_set_scene(self): def scene(): return self.device.status().scene @@ -98,3 +125,12 @@ def scene(): assert scene() == 1 self.device.set_scene(2) assert scene() == 2 + + with pytest.raises(PhilipsBulbException): + self.device.set_scene(-1) + + with pytest.raises(PhilipsBulbException): + self.device.set_scene(0) + + with pytest.raises(PhilipsBulbException): + self.device.set_scene(5) diff --git a/miio/tests/test_philips_eyecare.py b/miio/tests/test_philips_eyecare.py index 2a3e9363d..b6fbd8c2f 100644 --- a/miio/tests/test_philips_eyecare.py +++ b/miio/tests/test_philips_eyecare.py @@ -1,5 +1,6 @@ from unittest import TestCase from miio import PhilipsEyecare +from miio.philips_eyecare import PhilipsEyecareException from .dummies import DummyDevice import pytest @@ -8,10 +9,10 @@ class DummyPhilipsEyecare(DummyDevice, PhilipsEyecare): def __init__(self, *args, **kwargs): self.state = { 'power': 'on', - 'bright': 5, + 'bright': 100, 'notifystatus': 'off', 'ambstatus': 'off', - 'ambvalue': 41, + 'ambvalue': 100, 'eyecare': 'on', 'scene_num': 3, 'bls': 'on', @@ -88,10 +89,20 @@ def test_set_brightness(self): def brightness(): return self.device.status().brightness - self.device.set_brightness(10) - assert brightness() == 10 - self.device.set_brightness(20) - assert brightness() == 20 + self.device.set_brightness(1) + assert brightness() == 1 + self.device.set_brightness(50) + assert brightness() == 50 + self.device.set_brightness(100) + + with pytest.raises(PhilipsEyecareException): + self.device.set_brightness(-1) + + with pytest.raises(PhilipsEyecareException): + self.device.set_brightness(0) + + with pytest.raises(PhilipsEyecareException): + self.device.set_brightness(101) def test_set_scene(self): def scene(): @@ -102,15 +113,29 @@ def scene(): self.device.set_scene(2) assert scene() == 2 + with pytest.raises(PhilipsEyecareException): + self.device.set_scene(-1) + + with pytest.raises(PhilipsEyecareException): + self.device.set_scene(0) + + with pytest.raises(PhilipsEyecareException): + self.device.set_scene(5) + def test_delay_off(self): def delay_off_countdown(): return self.device.status().delay_off_countdown + self.device.delay_off(1) + assert delay_off_countdown() == 1 self.device.delay_off(100) assert delay_off_countdown() == 100 self.device.delay_off(200) assert delay_off_countdown() == 200 + with pytest.raises(PhilipsEyecareException): + self.device.delay_off(-1) + def test_smart_night_light(self): def smart_night_light(): return self.device.status().smart_night_light @@ -142,7 +167,17 @@ def test_set_ambient_brightness(self): def ambient_brightness(): return self.device.status().ambient_brightness - self.device.set_ambient_brightness(10) - assert ambient_brightness() == 10 - self.device.set_ambient_brightness(20) - assert ambient_brightness() == 20 + self.device.set_ambient_brightness(1) + assert ambient_brightness() == 1 + self.device.set_ambient_brightness(50) + assert ambient_brightness() == 50 + self.device.set_ambient_brightness(100) + + with pytest.raises(PhilipsEyecareException): + self.device.set_ambient_brightness(-1) + + with pytest.raises(PhilipsEyecareException): + self.device.set_ambient_brightness(0) + + with pytest.raises(PhilipsEyecareException): + self.device.set_ambient_brightness(101) From 0bf3dca5999da716310c9defd09cc7f7db7e49d3 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 26 Nov 2017 21:29:55 +0100 Subject: [PATCH 7/7] Some hound errors fixed. --- miio/tests/test_ceil.py | 12 ++++++------ miio/tests/test_philips_eyecare.py | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/miio/tests/test_ceil.py b/miio/tests/test_ceil.py index 20100f7e5..9fd0d64d2 100644 --- a/miio/tests/test_ceil.py +++ b/miio/tests/test_ceil.py @@ -68,8 +68,8 @@ def test_status(self): assert self.state().color_temperature == self.device.start_state["cct"] assert self.state().scene == self.device.start_state["snm"] assert self.state().delay_off_countdown == self.device.start_state["dv"] - assert self.state().smart_night_light == (self.device.start_state["bl"] == 1) - assert self.state().automatic_color_temperature == (self.device.start_state["ac"] == 1) + assert self.state().smart_night_light is (self.device.start_state["bl"] == 1) + assert self.state().automatic_color_temperature is (self.device.start_state["ac"] == 1) def test_set_brightness(self): def brightness(): @@ -112,15 +112,15 @@ def smart_night_light(): return self.device.status().smart_night_light self.device.smart_night_light_off() - assert smart_night_light() == False + assert smart_night_light() is False self.device.smart_night_light_on() - assert smart_night_light() == True + assert smart_night_light() is True def test_automatic_color_temperature_on(self): def automatic_color_temperature(): return self.device.status().automatic_color_temperature self.device.automatic_color_temperature_on() - assert automatic_color_temperature() == True + assert automatic_color_temperature() is True self.device.automatic_color_temperature_off() - assert automatic_color_temperature() == False + assert automatic_color_temperature() is False diff --git a/miio/tests/test_philips_eyecare.py b/miio/tests/test_philips_eyecare.py index b6fbd8c2f..647bbd77f 100644 --- a/miio/tests/test_philips_eyecare.py +++ b/miio/tests/test_philips_eyecare.py @@ -68,12 +68,12 @@ def test_status(self): assert self.is_on() is True assert self.state().brightness == self.device.start_state["bright"] - assert self.state().reminder == (self.device.start_state["notifystatus"] == 'on') - assert self.state().ambient == (self.device.start_state["ambstatus"] == 'on') + assert self.state().reminder is (self.device.start_state["notifystatus"] == 'on') + assert self.state().ambient is (self.device.start_state["ambstatus"] == 'on') assert self.state().ambient_brightness == self.device.start_state["ambvalue"] - assert self.state().eyecare == (self.device.start_state["eyecare"] == 'on') + assert self.state().eyecare is (self.device.start_state["eyecare"] == 'on') assert self.state().scene == self.device.start_state["scene_num"] - assert self.state().smart_night_light == (self.device.start_state["bls"] == 'on') + assert self.state().smart_night_light is (self.device.start_state["bls"] == 'on') assert self.state().delay_off_countdown == self.device.start_state["dvalue"] def test_eyecare(self): @@ -81,9 +81,9 @@ def eyecare(): return self.device.status().eyecare self.device.eyecare_on() - assert eyecare() == True + assert eyecare() is True self.device.eyecare_off() - assert eyecare() == False + assert eyecare() is False def test_set_brightness(self): def brightness(): @@ -141,27 +141,27 @@ def smart_night_light(): return self.device.status().smart_night_light self.device.smart_night_light_on() - assert smart_night_light() == True + assert smart_night_light() is True self.device.smart_night_light_off() - assert smart_night_light() == False + assert smart_night_light() is False def test_reminder(self): def reminder(): return self.device.status().reminder self.device.reminder_on() - assert reminder() == True + assert reminder() is True self.device.reminder_off() - assert reminder() == False + assert reminder() is False def test_ambient(self): def ambient(): return self.device.status().ambient self.device.ambient_on() - assert ambient() == True + assert ambient() is True self.device.ambient_off() - assert ambient() == False + assert ambient() is False def test_set_ambient_brightness(self): def ambient_brightness():