Skip to content

Commit

Permalink
tests: bring back return_values to simplify the diff
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Kotek committed Jan 10, 2020
1 parent 61afef5 commit d8399a2
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 415 deletions.
9 changes: 6 additions & 3 deletions miio/tests/dummies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ class DummyProtocol:
DummyProtocol allows you mock Protocol.
"""

def __init__(self, return_values):
self.return_values = return_values
def __init__(self, dummy_device):
# TODO: Ideally, return_values should be passed in here. Passing in dummy_device (which must have
# return_values) is a temporary workaround to minimize diff size.
self.dummy_device = dummy_device

def send(self, command: str, parameters=None, retry_count=3):
"""Overridden send() to return values from `self.return_values`."""
return self.return_values[command](parameters)
return self.dummy_device.return_values[command](parameters)


class DummyDevice:
Expand Down Expand Up @@ -36,6 +38,7 @@ class DummyDevice:

def __init__(self, *args, **kwargs):
self.start_state = self.state.copy()
self.protocol = DummyProtocol(self)

def _reset_state(self):
"""Revert back to the original state."""
Expand Down
40 changes: 19 additions & 21 deletions miio/tests/test_airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Power,
SwingMode,
)
from miio.tests.dummies import DummyProtocol
from miio.tests.dummies import DummyDevice

STATE_ON = ["on"]
STATE_OFF = ["off"]
Expand Down Expand Up @@ -52,23 +52,22 @@ def default(self, obj):
return json.JSONEncoder.default(self, obj)


class DummyAirConditioningCompanion(AirConditioningCompanion):
class DummyAirConditioningCompanion(DummyDevice, AirConditioningCompanion):
def __init__(self, *args, **kwargs):
self.state = ["010500978022222102", "01020119A280222221", "2"]
self.last_ir_played = None

self.protocol = DummyProtocol(
{
"get_model_and_state": self._get_state,
"start_ir_learn": lambda x: True,
"end_ir_learn": lambda x: True,
"get_ir_learn_result": lambda x: True,
"send_ir_code": lambda x: self._send_input_validation(x),
"send_cmd": lambda x: self._send_input_validation(x),
"set_power": lambda x: self._set_power(x),
}
)
self.return_values = {
"get_model_and_state": self._get_state,
"start_ir_learn": lambda x: True,
"end_ir_learn": lambda x: True,
"get_ir_learn_result": lambda x: True,
"send_ir_code": lambda x: self._send_input_validation(x),
"send_cmd": lambda x: self._send_input_validation(x),
"set_power": lambda x: self._set_power(x),
}
self.start_state = self.state.copy()
super().__init__(args, kwargs)

def _reset_state(self):
"""Revert back to the original state."""
Expand Down Expand Up @@ -208,22 +207,21 @@ def test_send_configuration(self):
self.assertSequenceEqual(self.device.get_last_ir_played(), args["out"])


class DummyAirConditioningCompanionV3(AirConditioningCompanionV3):
class DummyAirConditioningCompanionV3(DummyDevice, AirConditioningCompanionV3):
def __init__(self, *args, **kwargs):
self.state = ["010507950000257301", "011001160100002573", "807"]
self.device_prop = {"lumi.0": {"plug_state": ["on"]}}
self.model = MODEL_ACPARTNER_V3
self.last_ir_played = None

self.protocol = DummyProtocol(
{
"get_model_and_state": self._get_state,
"get_device_prop": self._get_device_prop,
"toggle_plug": self._toggle_plug,
}
)
self.return_values = {
"get_model_and_state": self._get_state,
"get_device_prop": self._get_device_prop,
"toggle_plug": self._toggle_plug,
}
self.start_state = self.state.copy()
self.start_device_prop = self.device_prop.copy()
super().__init__(args, kwargs)

def _reset_state(self):
"""Revert back to the original state."""
Expand Down
26 changes: 12 additions & 14 deletions miio/tests/test_airdehumidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from miio.device import DeviceInfo

from .dummies import DummyDevice, DummyProtocol
from .dummies import DummyDevice


class DummyAirDehumidifierV1(DummyDevice, AirDehumidifier):
Expand Down Expand Up @@ -56,19 +56,17 @@ def __init__(self, *args, **kwargs):
"auto": 50,
}

self.protocol = DummyProtocol(
{
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("on_off", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_led": lambda x: self._set_state("led", x),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_fan_speed": lambda x: self._set_state("fan_st", x),
"set_auto": lambda x: self._set_state("auto", x),
"miIO.info": self._get_device_info,
}
)
self.return_values = {
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("on_off", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_led": lambda x: self._set_state("led", x),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_fan_speed": lambda x: self._set_state("fan_st", x),
"set_auto": lambda x: self._set_state("auto", x),
"miIO.info": self._get_device_info,
}
super().__init__(args, kwargs)

def _get_device_info(self, _):
Expand Down
32 changes: 15 additions & 17 deletions miio/tests/test_airfresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
OperationMode,
)

from .dummies import DummyDevice, DummyProtocol
from .dummies import DummyDevice


class DummyAirFresh(DummyDevice, AirFresh):
Expand All @@ -36,22 +36,20 @@ def __init__(self, *args, **kwargs):
"favorite_level": None,
"led": "on",
}
self.protocol = DummyProtocol(
{
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_led": lambda x: self._set_state("led", x),
"set_led_level": lambda x: self._set_state("led_level", x),
"reset_filter1": lambda x: (
self._set_state("f1_hour_used", [0]),
self._set_state("filter_life", [100]),
),
"set_app_extra": lambda x: self._set_state("app_extra", x),
}
)
self.return_values = {
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_led": lambda x: self._set_state("led", x),
"set_led_level": lambda x: self._set_state("led_level", x),
"reset_filter1": lambda x: (
self._set_state("f1_hour_used", [0]),
self._set_state("filter_life", [100]),
),
"set_app_extra": lambda x: self._set_state("app_extra", x),
}
super().__init__(args, kwargs)


Expand Down
32 changes: 13 additions & 19 deletions miio/tests/test_airfresh_t2017.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
PtcLevel,
)

from .dummies import DummyDevice, DummyProtocol
from .dummies import DummyDevice


class DummyAirFreshT2017(DummyDevice, AirFreshT2017):
Expand All @@ -38,24 +38,18 @@ def __init__(self, *args, **kwargs):
"display": False,
"screen_direction": "forward",
}
self.protocol = DummyProtocol(
{
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", [(x[0] == "on")]),
"set_mode": lambda x: self._set_state("mode", x),
"set_sound": lambda x: self._set_state("sound", [(x[0] == "on")]),
"set_child_lock": lambda x: self._set_state(
"child_lock", [(x[0] == "on")]
),
"set_display": lambda x: self._set_state("display", [(x[0] == "on")]),
"set_screen_direction": lambda x: self._set_state(
"screen_direction", x
),
"set_ptc_level": lambda x: self._set_state("ptc_level", x),
"set_favourite_speed": lambda x: self._set_state("favourite_speed", x),
"set_filter_reset": lambda x: self._set_filter_reset(x),
}
)
self.return_values = {
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", [(x[0] == "on")]),
"set_mode": lambda x: self._set_state("mode", x),
"set_sound": lambda x: self._set_state("sound", [(x[0] == "on")]),
"set_child_lock": lambda x: self._set_state("child_lock", [(x[0] == "on")]),
"set_display": lambda x: self._set_state("display", [(x[0] == "on")]),
"set_screen_direction": lambda x: self._set_state("screen_direction", x),
"set_ptc_level": lambda x: self._set_state("ptc_level", x),
"set_favourite_speed": lambda x: self._set_state("favourite_speed", x),
"set_filter_reset": lambda x: self._set_filter_reset(x),
}
super().__init__(args, kwargs)

def _set_filter_reset(self, value: str):
Expand Down
72 changes: 33 additions & 39 deletions miio/tests/test_airhumidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
from miio.device import DeviceInfo

from .dummies import DummyDevice, DummyProtocol
from .dummies import DummyDevice


class DummyAirHumidifierV1(DummyDevice, AirHumidifier):
Expand Down Expand Up @@ -56,18 +56,16 @@ def __init__(self, *args, **kwargs):
"button_pressed": "led",
"hw_version": 0,
}
self.protocol = DummyProtocol(
{
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_led_b": lambda x: self._set_state("led_b", x),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_limit_hum": lambda x: self._set_state("limit_hum", x),
"miIO.info": self._get_device_info,
}
)
self.return_values = {
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_led_b": lambda x: self._set_state("led_b", x),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_limit_hum": lambda x: self._set_state("limit_hum", x),
"miIO.info": self._get_device_info,
}
super().__init__(args, kwargs)

def _get_device_info(self, _):
Expand Down Expand Up @@ -275,19 +273,17 @@ def __init__(self, *args, **kwargs):
"depth": 1,
"dry": "off",
}
self.protocol = DummyProtocol(
{
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_led_b": lambda x: self._set_state("led_b", [int(x[0])]),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_limit_hum": lambda x: self._set_state("limit_hum", x),
"set_dry": lambda x: self._set_state("dry", x),
"miIO.info": self._get_device_info,
}
)
self.return_values = {
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_led_b": lambda x: self._set_state("led_b", [int(x[0])]),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_limit_hum": lambda x: self._set_state("limit_hum", x),
"set_dry": lambda x: self._set_state("dry", x),
"miIO.info": self._get_device_info,
}
super().__init__(args, kwargs)

def _get_device_info(self, _):
Expand Down Expand Up @@ -509,19 +505,17 @@ def __init__(self, *args, **kwargs):
"depth": 1,
"dry": "off",
}
self.protocol = DummyProtocol(
{
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_led_b": lambda x: self._set_state("led_b", [int(x[0])]),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_limit_hum": lambda x: self._set_state("limit_hum", x),
"set_dry": lambda x: self._set_state("dry", x),
"miIO.info": self._get_device_info,
}
)
self.return_values = {
"get_prop": self._get_state,
"set_power": lambda x: self._set_state("power", x),
"set_mode": lambda x: self._set_state("mode", x),
"set_led_b": lambda x: self._set_state("led_b", [int(x[0])]),
"set_buzzer": lambda x: self._set_state("buzzer", x),
"set_child_lock": lambda x: self._set_state("child_lock", x),
"set_limit_hum": lambda x: self._set_state("limit_hum", x),
"set_dry": lambda x: self._set_state("dry", x),
"miIO.info": self._get_device_info,
}
super().__init__(args, kwargs)

def _get_device_info(self, _):
Expand Down
20 changes: 9 additions & 11 deletions miio/tests/test_airhumidifier_mjjsq.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
OperationMode,
)

from .dummies import DummyDevice, DummyProtocol
from .dummies import DummyDevice


class DummyAirHumidifierMjjsq(DummyDevice, AirHumidifierMjjsq):
Expand All @@ -27,16 +27,14 @@ def __init__(self, *args, **kwargs):
"waterstatus": 1,
"watertankstatus": 1,
}
self.protocol = DummyProtocol(
{
"get_prop": self._get_state,
"Set_OnOff": lambda x: self._set_state("OnOff_State", x),
"Set_HumidifierGears": lambda x: self._set_state("Humidifier_Gear", x),
"SetLedState": lambda x: self._set_state("Led_State", x),
"SetTipSound_Status": lambda x: self._set_state("TipSound_State", x),
"Set_HumiValue": lambda x: self._set_state("HumiSet_Value", x),
}
)
self.return_values = {
"get_prop": self._get_state,
"Set_OnOff": lambda x: self._set_state("OnOff_State", x),
"Set_HumidifierGears": lambda x: self._set_state("Humidifier_Gear", x),
"SetLedState": lambda x: self._set_state("Led_State", x),
"SetTipSound_Status": lambda x: self._set_state("TipSound_State", x),
"Set_HumiValue": lambda x: self._set_state("HumiSet_Value", x),
}
super().__init__(args, kwargs)


Expand Down
Loading

0 comments on commit d8399a2

Please sign in to comment.