From 47d59ac9ee3600af8a09d95d35cd1e71bd3e730a Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Wed, 8 Nov 2017 08:09:25 +0100 Subject: [PATCH 1/7] Property load_power removed from the plug. It's a feature of the PowerStrip. The Xiaomi plug doesn't provide load. Load power workaround of the PowerStrip replaced by a property called "power_consume_rate" which provides the same values as the android app. --- miio/plug.py | 16 +++------------- miio/powerstrip.py | 11 +++++------ 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/miio/plug.py b/miio/plug.py index ce7412c6d..df78d4411 100644 --- a/miio/plug.py +++ b/miio/plug.py @@ -27,20 +27,10 @@ def temperature(self) -> float: """Return temperature.""" return self.data["temperature"] - @property - def load_power(self) -> Optional[float]: - """Return current load power.""" - if self.data["current"] is not None: - # The constant of 110V is used intentionally. The current was - # calculated with a wrong reference voltage already. - return self.data["current"] * 110 - return None - def __str__(self) -> str: - s = "" % \ + s = "" % \ (self.power, - self.temperature, - self.load_power) + self.temperature) return s @@ -49,7 +39,7 @@ class Plug(Device): def status(self) -> PlugStatus: """Retrieve properties.""" - properties = ['power', 'temperature', 'current'] + properties = ['power', 'temperature'] values = self.send( "get_prop", properties diff --git a/miio/powerstrip.py b/miio/powerstrip.py index fe3dc6349..39e3d2a94 100644 --- a/miio/powerstrip.py +++ b/miio/powerstrip.py @@ -40,10 +40,8 @@ def temperature(self) -> float: @property def load_power(self) -> Optional[float]: """Current power load, if available.""" - if self.data["current"] is not None: - # The constant of 110V is used intentionally. The current was - # calculated with a wrong reference voltage already. - return self.data["current"] * 110 + if self.data["power_consume_rate"] is not None: + return self.data["power_consume_rate"] return None @property @@ -53,10 +51,11 @@ def mode(self) -> PowerMode: def __str__(self) -> str: s = "" % \ + "load_power=%s, current=%s, mode=%s>" % \ (self.power, self.temperature, self.load_power, + self.current, self.mode) return s @@ -66,7 +65,7 @@ class PowerStrip(Device): def status(self) -> PowerStripStatus: """Retrieve properties.""" - properties = ['power', 'temperature', 'current', 'mode'] + properties = ['power', 'temperature', 'current', 'mode', 'power_consume_rate'] values = self.send( "get_prop", properties From 44358b79a7a4f6fd35641973f83b5618035b4236 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sat, 11 Nov 2017 11:54:19 +0100 Subject: [PATCH 2/7] Line too long fixed. --- miio/powerstrip.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/miio/powerstrip.py b/miio/powerstrip.py index 39e3d2a94..cbb3b583b 100644 --- a/miio/powerstrip.py +++ b/miio/powerstrip.py @@ -65,7 +65,8 @@ class PowerStrip(Device): def status(self) -> PowerStripStatus: """Retrieve properties.""" - properties = ['power', 'temperature', 'current', 'mode', 'power_consume_rate'] + properties = ['power', 'temperature', 'current', 'mode', + 'power_consume_rate'] values = self.send( "get_prop", properties From ebac318760a0a71d7d2923fe473ee78c140fed82 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 19 Nov 2017 10:14:19 +0100 Subject: [PATCH 3/7] Unittest for the PowerStrip added. Unittest of the Plug updated. --- miio/tests/test_plug.py | 7 ---- miio/tests/test_powerstrip.py | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 miio/tests/test_powerstrip.py diff --git a/miio/tests/test_plug.py b/miio/tests/test_plug.py index 53469b552..a9e6d18b9 100644 --- a/miio/tests/test_plug.py +++ b/miio/tests/test_plug.py @@ -9,7 +9,6 @@ def __init__(self, *args, **kwargs): self.state = { 'power': 'on', 'temperature': 32, - 'current': 123, } self.return_values = { 'get_prop': self._get_state, @@ -53,9 +52,3 @@ def test_status(self): assert self.is_on() is True assert self.state().temperature == self.device.start_state["temperature"] - assert self.state().load_power == self.device.start_state["current"] * 110 - - def test_status_without_current(self): - del self.device.state["current"] - - assert self.state().load_power is None diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py new file mode 100644 index 000000000..085e4bd33 --- /dev/null +++ b/miio/tests/test_powerstrip.py @@ -0,0 +1,62 @@ +from unittest import TestCase +from miio import PowerStrip +from .dummies import DummyDevice +import pytest + + +class DummyPowerStrip(DummyDevice, PowerStrip): + def __init__(self, *args, **kwargs): + self.state = { + 'power': 'on', + 'temperature': 32, + 'current': 123, + 'power_consume_rate': 123, + } + 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 powerstrip(request): + request.cls.device = DummyPowerStrip() + # TODO add ability to test on a real device + + +@pytest.mark.usefixtures("powerstrip") +class TestPowerStrip(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().temperature == self.device.start_state["temperature"] + 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 From 1c09337d372b8042cbc8525f6f7a373297a77635 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 19 Nov 2017 10:57:28 +0100 Subject: [PATCH 4/7] Fixture updated. --- miio/tests/test_powerstrip.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py index 085e4bd33..b869809c5 100644 --- a/miio/tests/test_powerstrip.py +++ b/miio/tests/test_powerstrip.py @@ -8,9 +8,9 @@ class DummyPowerStrip(DummyDevice, PowerStrip): def __init__(self, *args, **kwargs): self.state = { 'power': 'on', - 'temperature': 32, + 'temperature': 32.5, 'current': 123, - 'power_consume_rate': 123, + 'power_consume_rate': 12.5, } self.return_values = { 'get_prop': self._get_state, From 537b6558c774a7a35afde3090f92752b4c65019c Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 19 Nov 2017 16:35:20 +0100 Subject: [PATCH 5/7] Fixture updated. Missing property ("mode") added. --- miio/tests/test_powerstrip.py | 1 + 1 file changed, 1 insertion(+) diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py index b869809c5..e75406cee 100644 --- a/miio/tests/test_powerstrip.py +++ b/miio/tests/test_powerstrip.py @@ -8,6 +8,7 @@ class DummyPowerStrip(DummyDevice, PowerStrip): def __init__(self, *args, **kwargs): self.state = { 'power': 'on', + 'mode': 'normal', 'temperature': 32.5, 'current': 123, 'power_consume_rate': 12.5, From 921bb719c65cc25b4efe5ba5c93d528f5f6826ed Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 19 Nov 2017 16:44:55 +0100 Subject: [PATCH 6/7] Make hound happy again. --- miio/tests/test_powerstrip.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py index e75406cee..2f41c5814 100644 --- a/miio/tests/test_powerstrip.py +++ b/miio/tests/test_powerstrip.py @@ -54,8 +54,10 @@ def test_status(self): self.device._reset_state() assert self.is_on() is True - assert self.state().temperature == self.device.start_state["temperature"] - assert self.state().load_power == self.device.start_state["power_consume_rate"] + assert self.state().temperature == \ + self.device.start_state["temperature"] + 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"] From 024c14731e8818a59d2233105d1b424b7b011f0c Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 19 Nov 2017 16:47:07 +0100 Subject: [PATCH 7/7] The hound error is fine for better readability of the assertion. --- miio/tests/test_powerstrip.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py index 2f41c5814..e75406cee 100644 --- a/miio/tests/test_powerstrip.py +++ b/miio/tests/test_powerstrip.py @@ -54,10 +54,8 @@ def test_status(self): self.device._reset_state() assert self.is_on() is True - assert self.state().temperature == \ - self.device.start_state["temperature"] - assert self.state().load_power == \ - self.device.start_state["power_consume_rate"] + assert self.state().temperature == self.device.start_state["temperature"] + 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"]