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

Load power of the PowerStrip fixed and removed from the Plug #117

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 3 additions & 13 deletions miio/plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<PlugStatus power=%s, temperature=%s, load_power=%s>" % \
s = "<PlugStatus power=%s, temperature=%s>" % \
(self.power,
self.temperature,
self.load_power)
self.temperature)
return s


Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions miio/powerstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,10 +51,11 @@ def mode(self) -> PowerMode:

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

Expand All @@ -66,7 +65,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
Expand Down
7 changes: 0 additions & 7 deletions miio/tests/test_plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
62 changes: 62 additions & 0 deletions miio/tests/test_powerstrip.py
Original file line number Diff line number Diff line change
@@ -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"]

Choose a reason for hiding this comment

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

line too long (81 > 79 characters)

Choose a reason for hiding this comment

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

line too long (81 > 79 characters)

Choose a reason for hiding this comment

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

line too long (81 > 79 characters)

assert self.state().load_power == self.device.start_state["power_consume_rate"]

Choose a reason for hiding this comment

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

line too long (87 > 79 characters)

Choose a reason for hiding this comment

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

line too long (87 > 79 characters)

Choose a reason for hiding this comment

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

line too long (87 > 79 characters)


def test_status_without_power_consume_rate(self):
del self.device.state["power_consume_rate"]

assert self.state().load_power is None