From 2876a03477889d95f0ed2e598aaef03937db1ca2 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 27 Nov 2017 08:40:23 +0100 Subject: [PATCH 1/4] Unit tests of the Xiaomi Power Strip extended. Property current re-introduced for completeness. --- miio/powerstrip.py | 7 +++++++ miio/tests/test_powerstrip.py | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/miio/powerstrip.py b/miio/powerstrip.py index ab5ae9359..3ad556828 100644 --- a/miio/powerstrip.py +++ b/miio/powerstrip.py @@ -37,6 +37,13 @@ def temperature(self) -> float: """Current temperature.""" return self.data["temperature"] + @property + def current(self) -> Optional[float]: + """Current, if available. Meaning and voltage reference unknown.""" + if self.data["current"] is not None: + return self.data["current"] + return None + @property def load_power(self) -> Optional[float]: """Current power load, if available.""" diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py index e75406cee..b8165e71e 100644 --- a/miio/tests/test_powerstrip.py +++ b/miio/tests/test_powerstrip.py @@ -1,5 +1,6 @@ from unittest import TestCase from miio import PowerStrip +from miio.powerstrip import PowerMode from .dummies import DummyDevice import pytest @@ -10,12 +11,13 @@ def __init__(self, *args, **kwargs): 'power': 'on', 'mode': 'normal', 'temperature': 32.5, - 'current': 123, + 'current': 25.5, 'power_consume_rate': 12.5, } self.return_values = { 'get_prop': self._get_state, 'set_power': lambda x: self._set_state("power", x), + 'set_power_mode': lambda x: self._set_state("mode", x), } super().__init__(args, kwargs) @@ -54,10 +56,22 @@ def test_status(self): self.device._reset_state() assert self.is_on() is True + assert self.state().mode == PowerMode(self.device.start_state["mode"]) assert self.state().temperature == self.device.start_state["temperature"] + assert self.state().current == self.device.start_state["current"] assert self.state().load_power == self.device.start_state["power_consume_rate"] def test_status_without_power_consume_rate(self): del self.device.state["power_consume_rate"] assert self.state().load_power is None + + def test_set_power_mode(self): + def mode(): + return self.device.status().mode + + self.device.set_power_mode(PowerMode.Eco) + assert mode() == PowerMode.Eco + self.device.set_power_mode(PowerMode.Normal) + assert mode() == PowerMode.Normal + From 4cefb0418734a934fc93b2e19b63d00cd7194404 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 27 Nov 2017 08:41:49 +0100 Subject: [PATCH 2/4] Blank line removed. --- miio/tests/test_powerstrip.py | 1 - 1 file changed, 1 deletion(-) diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py index b8165e71e..8ceccf733 100644 --- a/miio/tests/test_powerstrip.py +++ b/miio/tests/test_powerstrip.py @@ -74,4 +74,3 @@ def mode(): assert mode() == PowerMode.Eco self.device.set_power_mode(PowerMode.Normal) assert mode() == PowerMode.Normal - From 3cc213ee28be7e32d57f71e64e2a28e02f36dcd5 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 27 Nov 2017 14:17:20 +0100 Subject: [PATCH 3/4] Two additional code paths added. --- miio/tests/test_powerstrip.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py index 8ceccf733..f1fa6e8de 100644 --- a/miio/tests/test_powerstrip.py +++ b/miio/tests/test_powerstrip.py @@ -66,6 +66,16 @@ def test_status_without_power_consume_rate(self): assert self.state().load_power is None + def test_status_without_current(self): + del self.device.state["current"] + + assert self.state().current is None + + def test_status_without_mode(self): + del self.device.state["mode"] + + assert self.state().mode is None + def test_set_power_mode(self): def mode(): return self.device.status().mode From 11aac11ccf56ecc160e34b812f1a21ca1a8b6ad9 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 27 Nov 2017 21:25:59 +0100 Subject: [PATCH 4/4] Tests for None values fixed. --- miio/tests/test_powerstrip.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py index f1fa6e8de..a0b2228e3 100644 --- a/miio/tests/test_powerstrip.py +++ b/miio/tests/test_powerstrip.py @@ -62,18 +62,21 @@ def test_status(self): assert self.state().load_power == self.device.start_state["power_consume_rate"] def test_status_without_power_consume_rate(self): - del self.device.state["power_consume_rate"] + self.device._reset_state() + self.device.state["power_consume_rate"] = None assert self.state().load_power is None def test_status_without_current(self): - del self.device.state["current"] + self.device._reset_state() + self.device.state["current"] = None assert self.state().current is None def test_status_without_mode(self): - del self.device.state["mode"] + self.device._reset_state() + self.device.state["mode"] = None assert self.state().mode is None def test_set_power_mode(self):