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

Device support of the Xiaomi Air Conditioning Companion improved #233

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 20 additions & 4 deletions miio/airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down
27 changes: 26 additions & 1 deletion miio/tests/test_airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
SwingMode, STORAGE_SLOT_ID
import pytest

STATE_ON = ['on']
STATE_OFF = ['off']

class DummyAirConditioningCompanion(AirConditioningCompanion):
def __init__(self, *args, **kwargs):
Expand All @@ -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()

Expand All @@ -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])

Expand All @@ -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
Expand Down