Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powerstrip support improved #239

Merged
merged 6 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions miio/powerstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
import enum
from typing import Dict, Any, Optional
from collections import defaultdict
from .device import Device
from .device import Device, DeviceException

_LOGGER = logging.getLogger(__name__)


class PowerStripException(DeviceException):
pass


class PowerMode(enum.Enum):
Eco = 'green'
Normal = 'normal'
Expand All @@ -16,10 +20,13 @@ class PowerStripStatus:
"""Container for status reports from the power strip."""

def __init__(self, data: Dict[str, Any]) -> None:
# Device model: qmi.powerstrip.v1, zimi.powerstrip.v2
#
# {'power': 'on', 'temperature': 48.11,
# 'current': 0.06, 'mode': 'green'}
"""
Supported device models: qmi.powerstrip.v1, zimi.powerstrip.v2

Response of a Power Strip 2 (zimi.powerstrip.v2):
{'power','on', 'temperature': 48.7, 'current': 0.05, 'mode': None,
'power_consume_rate': 4.09, 'wifi_led': 'on', 'power_price': 49}
"""
self.data = data

@property
Expand Down Expand Up @@ -58,14 +65,33 @@ def mode(self) -> Optional[PowerMode]:
return PowerMode(self.data["mode"])
return None

@property
def wifi_led(self) -> bool:
"""True if the wifi led is turned on."""
return self.data["wifi_led"] == "on"

@property
def power_price(self) -> Optional[int]:
"""The stored power price, if available."""
if self.data["power_price"] is not None:
return self.data["power_price"]
return None

def __repr__(self) -> str:
s = "<PowerStripStatus power=%s, temperature=%s, " \
"load_power=%s, current=%s, mode=%s>" % \
s = "<PowerStripStatus power=%s, " \
"temperature=%s, " \
"load_power=%s, " \
"current=%s, " \
"mode=%s, " \
"wifi_led=%s, " \
"power_price=%s>" % \
(self.power,
self.temperature,
self.load_power,
self.current,
self.mode)
self.mode,
self.wifi_led,
self.power_price)
return s


Expand All @@ -75,7 +101,7 @@ class PowerStrip(Device):
def status(self) -> PowerStripStatus:
"""Retrieve properties."""
properties = ['power', 'temperature', 'current', 'mode',
'power_consume_rate']
'power_consume_rate', 'wifi_led', 'power_price', ]
values = self.send(
"get_prop",
properties
Expand All @@ -101,7 +127,21 @@ def off(self):
return self.send("set_power", ["off"])

def set_power_mode(self, mode: PowerMode):
"""Set mode."""
"""Set the power mode."""

# green, normal
return self.send("set_power_mode", [mode.value])

def set_wifi_led(self, led: bool):
"""Set the wifi led on/off."""
if led:
return self.send("set_wifi_led", ["on"])
else:
return self.send("set_wifi_led", ["off"])

def set_power_price(self, price: int):
"""Set the power price."""
if price < 0:
raise PowerStripException("Invalid power price: %s" % price)

return self.send("set_power_price", [price])
43 changes: 42 additions & 1 deletion miio/tests/test_powerstrip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase
from miio import PowerStrip
from miio.powerstrip import PowerMode
from miio.powerstrip import PowerMode, PowerStripException
from .dummies import DummyDevice
import pytest

Expand All @@ -13,11 +13,15 @@ def __init__(self, *args, **kwargs):
'temperature': 32.5,
'current': 25.5,
'power_consume_rate': 12.5,
'wifi_led': 'off',
'power_price': 49,
}
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),
}
super().__init__(args, kwargs)

Expand Down Expand Up @@ -87,3 +91,40 @@ def mode():
assert mode() == PowerMode.Eco
self.device.set_power_mode(PowerMode.Normal)
assert mode() == PowerMode.Normal

def test_status_without_mode(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefinition of unused 'test_status_without_mode' from line 80

self.device._reset_state()

# The Power Strip 2 doesn't support power modes
self.device.state["mode"] = None
assert self.state().mode is None

def test_set_wifi_led(self):
def wifi_led():
return self.device.status().wifi_led

self.device.set_wifi_led(True)
assert wifi_led() is True

self.device.set_wifi_led(False)
assert wifi_led() is False

def test_set_power_price(self):
def power_price():
return self.device.status().power_price

self.device.set_power_price(0)
assert power_price() == 0
self.device.set_power_price(1)
assert power_price() == 1
self.device.set_power_price(2)
assert power_price() == 2

with pytest.raises(PowerStripException):
self.device.set_power_price(-1)

def test_status_without_power_price(self):
self.device._reset_state()

self.device.state["power_price"] = None
assert self.state().power_price is None