Skip to content

Commit

Permalink
Power Strip support extended by additional attributes (Closes: rytila…
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Mar 30, 2018
1 parent f2d6de0 commit c5f0369
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
49 changes: 42 additions & 7 deletions miio/powerstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<PowerStripStatus power=%s, " \
"temperature=%s, " \
"load_power=%s, " \
"voltage=%s, " \
"current=%s, " \
"load_power=%s, " \
"power_factor=%s " \
"power_price=%s, " \
"leakage_current=%s, " \
"mode=%s, " \
"wifi_led=%s, " \
"power_price=%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


Expand All @@ -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
Expand Down Expand Up @@ -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])
13 changes: 13 additions & 0 deletions miio/tests/test_powerstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ 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,
'set_power': lambda x: self._set_state("power", x),
'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)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

0 comments on commit c5f0369

Please sign in to comment.