Skip to content

Commit

Permalink
Device support of the Xiaomi Air Conditioning Companion improved (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Feb 26, 2018
1 parent 774cb75 commit fc5799a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
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

0 comments on commit fc5799a

Please sign in to comment.