From beeef6ae874512bdeae1377e1f97cf87d3e11e31 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 19 Nov 2017 16:58:50 +0100 Subject: [PATCH] Load power of the PowerStrip fixed and removed from the Plug (#117) * 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. * Line too long fixed. * Unittest for the PowerStrip added. Unittest of the Plug updated. * Fixture updated. * Fixture updated. Missing property ("mode") added. * Make hound happy again. * The hound error is fine for better readability of the assertion. --- miio/plug.py | 16 ++------- miio/powerstrip.py | 12 +++---- miio/tests/test_plug.py | 7 ---- miio/tests/test_powerstrip.py | 63 +++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 miio/tests/test_powerstrip.py 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 90ad03839..ab5ae9359 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 @@ -55,10 +53,11 @@ def mode(self) -> Optional[PowerMode]: def __repr__(self) -> str: s = "" % \ + "load_power=%s, current=%s, mode=%s>" % \ (self.power, self.temperature, self.load_power, + self.current, self.mode) return s @@ -68,7 +67,8 @@ 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 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..e75406cee --- /dev/null +++ b/miio/tests/test_powerstrip.py @@ -0,0 +1,63 @@ +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', + 'mode': 'normal', + 'temperature': 32.5, + 'current': 123, + 'power_consume_rate': 12.5, + } + 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