diff --git a/miio/airconditioningcompanion.py b/miio/airconditioningcompanion.py index dbe1abec7..03424cf8d 100644 --- a/miio/airconditioningcompanion.py +++ b/miio/airconditioningcompanion.py @@ -78,12 +78,20 @@ def __init__(self, data): # # Response of "get_model_and_state": # ['010500978022222102', '010201190280222221', '2'] + # + # AC turned on by set_power=on: + # ['010507950000257301', '011001160100002573', '807'] + # + # AC turned off by set_power=off: + # ['010507950000257301', '010001160100002573', '6'] + # ... + # ['010507950000257301', '010001160100002573', '1'] self.data = data @property - def air_condition_power(self) -> str: - """Current power state of the air conditioner.""" - return str(self.data[2]) + def load_power(self) -> int: + """Current power load of the air conditioner.""" + return int(self.data[2]) @property def air_condition_model(self) -> str: @@ -140,6 +148,14 @@ def status(self) -> AirConditioningCompanionStatus: status = self.send("get_model_and_state", []) return AirConditioningCompanionStatus(status) + def on(self): + """Turn the air condition on by infrared.""" + return self.send("set_power", ["on"]) + + def off(self): + """Turn the air condition off by infared.""" + return self.send("set_power", ["off"]) + def learn(self, slot: int=STORAGE_SLOT_ID): """Learn an infrared command.""" return self.send("start_ir_learn", [slot]) @@ -170,7 +186,7 @@ def send_configuration(self, model: str, power: Power, swing_mode: SwingMode): # Static turn off command available? - if (power is False) and (model in DEVICE_COMMAND_TEMPLATES) and \ + if (power is Power.Off) and (model in DEVICE_COMMAND_TEMPLATES) and \ (POWER_OFF in DEVICE_COMMAND_TEMPLATES[model]): return self.send_command( model + DEVICE_COMMAND_TEMPLATES[model][POWER_OFF]) diff --git a/miio/tests/test_airconditioningcompanion.py b/miio/tests/test_airconditioningcompanion.py index ce40db84d..70fee626f 100644 --- a/miio/tests/test_airconditioningcompanion.py +++ b/miio/tests/test_airconditioningcompanion.py @@ -5,6 +5,8 @@ SwingMode, STORAGE_SLOT_ID import pytest +STATE_ON = ['on'] +STATE_OFF = ['off'] class DummyAirConditioningCompanion(AirConditioningCompanion): def __init__(self, *args, **kwargs): @@ -17,6 +19,7 @@ def __init__(self, *args, **kwargs): 'get_ir_learn_result': lambda x: True, 'send_ir_code': lambda x: True, 'send_cmd': self._send_cmd_input_validation, + 'set_power': lambda x: self._set_power(x), } self.start_state = self.state.copy() @@ -32,6 +35,14 @@ def _get_state(self, props): """Return the requested data""" return self.state + def _set_power(self, value: str): + """Set the requested power state""" + if value == STATE_ON: + self.state[1] = self.state[1][:2] + '1' + self.state[1][3:] + + if value == STATE_OFF: + self.state[1] = self.state[1][:2] + '0' + self.state[1][3:] + def _send_cmd_input_validation(self, props): return all(c in string.hexdigits for c in props[0]) @@ -50,11 +61,25 @@ def is_on(self): def state(self): return self.device.status() + def test_on(self): + self.device.off() # ensure off + assert self.is_on() 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 False - assert self.state().air_condition_power == '2' + assert self.state().load_power == 2 assert self.state().air_condition_model == '0180222221' assert self.state().temperature == 25 assert self.state().swing_mode is False