diff --git a/miio/powerstrip.py b/miio/powerstrip.py index 84ecd6d07..522139af3 100644 --- a/miio/powerstrip.py +++ b/miio/powerstrip.py @@ -77,21 +77,48 @@ def power_price(self) -> Optional[int]: return self.data["power_price"] return None + @property + def leakage_current(self) -> Optional[int]: + """The leakage current, if available.""" + if self.data["elec_leakage"] is not None: + return self.data["elec_leakage"] + return None + + @property + def voltage(self) -> Optional[int]: + """The voltage, if available.""" + if self.data["voltage"] is not None: + return self.data["voltage"] + return None + + @property + def power_factor(self) -> Optional[int]: + """The power factor, if available.""" + if self.data["power_factor"] is not None: + return self.data["power_factor"] + return None + def __repr__(self) -> str: s = "" % \ + "wifi_led=%s>" % \ (self.power, self.temperature, - self.load_power, + self.voltage, self.current, + self.load_power, + self.power_factor, + self.power_price, + self.leakage_current, self.mode, - self.wifi_led, - self.power_price) + self.wifi_led) return s @@ -101,7 +128,8 @@ class PowerStrip(Device): def status(self) -> PowerStripStatus: """Retrieve properties.""" properties = ['power', 'temperature', 'current', 'mode', - 'power_consume_rate', 'wifi_led', 'power_price', ] + 'power_consume_rate', 'wifi_led', 'power_price', + 'voltage', 'power_factor', 'elec_leakage'] values = self.send( "get_prop", properties @@ -145,3 +173,10 @@ def set_power_price(self, price: int): raise PowerStripException("Invalid power price: %s" % price) return self.send("set_power_price", [price]) + + def set_realtime_power(self, power: bool): + """Set the realtime power on/off.""" + if power: + return self.send("set_rt_power", [1]) + else: + return self.send("set_rt_power", [0]) diff --git a/miio/tests/test_powerstrip.py b/miio/tests/test_powerstrip.py index 18b9e9460..7a1a11855 100644 --- a/miio/tests/test_powerstrip.py +++ b/miio/tests/test_powerstrip.py @@ -15,6 +15,9 @@ def __init__(self, *args, **kwargs): 'power_consume_rate': 12.5, 'wifi_led': 'off', 'power_price': 49, + 'voltage': 230, + 'elec_leakage': 0, + 'power_factor': 1 } self.return_values = { 'get_prop': self._get_state, @@ -22,6 +25,7 @@ def __init__(self, *args, **kwargs): 'set_power_mode': lambda x: self._set_state("mode", x), 'set_wifi_led': lambda x: self._set_state("wifi_led", x), 'set_power_price': lambda x: self._set_state("power_price", x), + 'set_rt_power': lambda x: True, } super().__init__(args, kwargs) @@ -64,6 +68,9 @@ def test_status(self): 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"] + assert self.state().voltage == self.device.start_state["voltage"] + assert self.state().power_factor == self.device.start_state["power_factor"] + assert self.state().leakage_current == self.device.start_state["elec_leakage"] def test_status_without_power_consume_rate(self): self.device._reset_state() @@ -125,3 +132,9 @@ def test_status_without_power_price(self): self.device.state["power_price"] = None assert self.state().power_price is None + + def test_set_realtime_power(self): + """The method is open-loop. The new state cannot be retrieved.""" + self.device.set_realtime_power(True) + self.device.set_realtime_power(False) +