Skip to content

Commit

Permalink
Tests for reprs of the status classes (#266)
Browse files Browse the repository at this point in the history
* Test the __repr__ of the status class.

* Import fixed.

* Access the correct object.

* Repr tests for all devices added.

* Yeelight tests refactored.

* Repr test of the vacuum fixed.

* Missing __repr__ method added.

* __str__ methods renamed to __repr__.

* Yeelight refactoring reverted.

* Start state extended by get power output.

* Dict extended properly.

* Reformatted.

* Format updated.
  • Loading branch information
syssi authored and rytilahti committed Mar 16, 2018
1 parent 8a1a730 commit 17e8e69
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 17 deletions.
20 changes: 20 additions & 0 deletions miio/airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ def mode(self) -> Optional[OperationMode]:
except TypeError:
return None

def __repr__(self) -> str:
s = "<AirConditioningCompanionStatus " \
"power=%s, " \
"load_power=%s, " \
"air_condition_model=%s, " \
"led=%s, " \
"temperature=%s, " \
"swing_mode=%s, " \
"fan_speed=%s, " \
"mode=%s>" % \
(self.power,
self.load_power,
self.air_condition_model,
self.led,
self.temperature,
self.swing_mode,
self.fan_speed,
self.mode)
return s


class AirConditioningCompanion(Device):
"""Main class representing Xiaomi Air Conditioning Companion."""
Expand Down
2 changes: 1 addition & 1 deletion miio/airhumidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def button_pressed(self) -> Optional[str]:
"""Last pressed button."""
return self.data["button_pressed"]

def __str__(self) -> str:
def __repr__(self) -> str:
s = "<AirHumidiferStatus power=%s, " \
"mode=%s, " \
"temperature=%s, " \
Expand Down
2 changes: 1 addition & 1 deletion miio/plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def temperature(self) -> float:
"""Return temperature."""
return self.data["temperature"]

def __str__(self) -> str:
def __repr__(self) -> str:
s = "<PlugStatus power=%s, temperature=%s>" % \
(self.power,
self.temperature)
Expand Down
2 changes: 1 addition & 1 deletion miio/plug_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def usb_power(self) -> bool:
def temperature(self) -> float:
return self.data["temperature"]

def __str__(self) -> str:
def __repr__(self) -> str:
s = "<PlugV1Status power=%s, usb_power=%s, temperature=%s>" % \
(self.power,
self.usb_power,
Expand Down
2 changes: 1 addition & 1 deletion miio/plug_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def wifi_led(self) -> bool:
"""True if the wifi led is turned on."""
return self.data["wifi_led"] == "on"

def __str__(self) -> str:
def __repr__(self) -> str:
s = "<PlugV3Status " \
"power=%s, " \
"usb_power=%s, " \
Expand Down
6 changes: 4 additions & 2 deletions miio/tests/test_airconditioningcompanion.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import string
from unittest import TestCase
from miio import AirConditioningCompanion
from miio.airconditioningcompanion import OperationMode, FanSpeed, Power, \
SwingMode, Led, STORAGE_SLOT_ID
from miio.airconditioningcompanion import (OperationMode, FanSpeed, Power, SwingMode, Led,
AirConditioningCompanionStatus, STORAGE_SLOT_ID, )
import pytest

STATE_ON = ['on']
Expand Down Expand Up @@ -78,6 +78,8 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(AirConditioningCompanionStatus(self.device.start_state))

assert self.is_on() is False
assert self.state().load_power == 2
assert self.state().air_condition_model == '010500978022222102'
Expand Down
5 changes: 4 additions & 1 deletion miio/tests/test_airhumidifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import TestCase
from miio import AirHumidifier
from miio.airhumidifier import OperationMode, LedBrightness, AirHumidifierException
from miio.airhumidifier import (OperationMode, LedBrightness,
AirHumidifierStatus, AirHumidifierException)
from .dummies import DummyDevice
import pytest

Expand Down Expand Up @@ -69,6 +70,8 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(AirHumidifierStatus(self.device.start_state))

assert self.is_on() is True
assert self.state().temperature == self.device.start_state["temp_dec"] / 10.0
assert self.state().humidity == self.device.start_state["humidity"]
Expand Down
7 changes: 4 additions & 3 deletions miio/tests/test_airpurifier.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from unittest import TestCase
from miio import AirPurifier
from miio.airpurifier import (
OperationMode, LedBrightness, FilterType, SleepMode, AirPurifierException
)
from miio.airpurifier import (OperationMode, LedBrightness, FilterType,
SleepMode, AirPurifierStatus, AirPurifierException)
from .dummies import DummyDevice
import pytest

Expand Down Expand Up @@ -93,6 +92,8 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(AirPurifierStatus(self.device.start_state))

assert self.is_on() is True
assert self.state().aqi == self.device.start_state["aqi"]
assert self.state().average_aqi == self.device.start_state["average_aqi"]
Expand Down
3 changes: 3 additions & 0 deletions miio/tests/test_airqualitymonitor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase
from miio import AirQualityMonitor
from miio.airqualitymonitor import AirQualityMonitorStatus
from .dummies import DummyDevice
import pytest

Expand Down Expand Up @@ -51,6 +52,8 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(AirQualityMonitorStatus(self.device.start_state))

assert self.is_on() is True
assert self.state().aqi == self.device.start_state["aqi"]
assert self.state().battery == self.device.start_state["battery"]
Expand Down
4 changes: 3 additions & 1 deletion miio/tests/test_ceil.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase
from miio import Ceil
from miio.ceil import CeilException
from miio.ceil import CeilStatus, CeilException
from .dummies import DummyDevice
import pytest

Expand Down Expand Up @@ -66,6 +66,8 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(CeilStatus(self.device.start_state))

assert self.is_on() is True
assert self.state().brightness == self.device.start_state["bright"]
assert self.state().color_temperature == self.device.start_state["cct"]
Expand Down
4 changes: 3 additions & 1 deletion miio/tests/test_philips_bulb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase
from miio import PhilipsBulb
from miio.philips_bulb import PhilipsBulbException
from miio.philips_bulb import PhilipsBulbStatus, PhilipsBulbException
from .dummies import DummyDevice
import pytest

Expand Down Expand Up @@ -60,6 +60,8 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(PhilipsBulbStatus(self.device.start_state))

assert self.is_on() is True
assert self.state().brightness == self.device.start_state["bright"]
assert self.state().color_temperature == self.device.start_state["cct"]
Expand Down
4 changes: 3 additions & 1 deletion miio/tests/test_philips_eyecare.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase
from miio import PhilipsEyecare
from miio.philips_eyecare import PhilipsEyecareException
from miio.philips_eyecare import PhilipsEyecareStatus, PhilipsEyecareException
from .dummies import DummyDevice
import pytest

Expand Down Expand Up @@ -64,6 +64,8 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(PhilipsEyecareStatus(self.device.start_state))

assert self.is_on() is True
assert self.state().brightness == self.device.start_state["bright"]
assert self.state().reminder is (self.device.start_state["notifystatus"] == 'on')
Expand Down
3 changes: 3 additions & 0 deletions miio/tests/test_plug.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase
from miio import Plug
from miio.plug import PlugStatus
from .dummies import DummyDevice
import pytest

Expand Down Expand Up @@ -48,5 +49,7 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(PlugStatus(self.device.start_state))

assert self.is_on() is True
assert self.state().temperature == self.device.start_state["temperature"]
3 changes: 3 additions & 0 deletions miio/tests/test_plug_v1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase
from miio import PlugV1
from miio.plug_v1 import PlugV1Status
import pytest


Expand Down Expand Up @@ -67,6 +68,8 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(PlugV1Status(self.device.start_state))

assert self.is_on() is True
assert self.state().usb_power is True
assert self.state().temperature == self.device.start_state[
Expand Down
12 changes: 9 additions & 3 deletions miio/tests/test_plug_v3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase
from miio import PlugV3
from miio.plug_v3 import PlugV3Status
from .dummies import DummyDevice
import pytest

Expand Down Expand Up @@ -62,11 +63,16 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

load_power = self.device._get_load_power().pop(0)

start_state_extended = self.device.start_state.copy()
start_state_extended['load_power'] = load_power
assert repr(self.state()) == repr(PlugV3Status(start_state_extended))

assert self.is_on() is True
assert self.state().usb_power is True
assert self.state().temperature == self.device.start_state[
"temperature"]
assert self.state().load_power == self.device._get_load_power().pop(0)
assert self.state().temperature == self.device.start_state["temperature"]
assert self.state().load_power == load_power

def test_usb_on(self):
self.device.usb_off() # ensure off
Expand Down
4 changes: 3 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, PowerStripException
from miio.powerstrip import PowerMode, PowerStripStatus, PowerStripException
from .dummies import DummyDevice
import pytest

Expand Down Expand Up @@ -57,6 +57,8 @@ def test_off(self):
def test_status(self):
self.device._reset_state()

assert repr(self.state()) == repr(PowerStripStatus(self.device.start_state))

assert self.is_on() is True
assert self.state().mode == PowerMode(self.device.start_state["mode"])
assert self.state().temperature == self.device.start_state["temperature"]
Expand Down
3 changes: 3 additions & 0 deletions miio/tests/test_vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def status(self):

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

assert repr(self.status()) == repr(VacuumStatus(self.device.start_state))

status = self.status()
assert status.is_on is False
assert status.dnd is True
Expand Down
3 changes: 3 additions & 0 deletions miio/tests/test_yeelight.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class TestYeelight(TestCase):
def test_status(self):
self.device._reset_state()
status = self.device.status() # type: YeelightStatus

assert repr(status) == repr(YeelightStatus(self.device.start_state))

assert status.name == self.device.start_state["name"]
assert status.is_on is False
assert status.brightness == 100
Expand Down

0 comments on commit 17e8e69

Please sign in to comment.