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

Xiaomi AC Companion: LED property added #248

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
52 changes: 30 additions & 22 deletions miio/airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class Power(enum.Enum):
Off = 0


class Led(enum.Enum):
On = '0'
Off = 'a'


STORAGE_SLOT_ID = 30
POWER_OFF = 'off'

Expand All @@ -36,36 +41,28 @@ class Power(enum.Enum):
DEVICE_COMMAND_TEMPLATES = {
'fallback': {
'deviceType': 'generic',
'base': '[po][mo][wi][sw][tt]a0'
},
'0180111111': {
'deviceType': 'media_1',
'base': '[po][mo][wi][sw][tt]02'
},
'0180222221': {
'deviceType': 'gree_1',
'base': '[po][mo][wi][sw][tt]02'
'base': '[po][mo][wi][sw][tt][li]'
},
'0100010727': {
'deviceType': 'gree_2',
'base': '[po][mo][wi][sw][tt]1100190[tt1]205002102000[tt7]0190[tt1]207002000000[tt4]0',
'base': '[po][mo][wi][sw][tt]1100190[tt1]205002102000[tt7]0190[tt1]207002000000[tt4]',
'off': '01011101004000205002112000D04000207002000000A0'
},
'0100004795': {
'deviceType': 'gree_8',
'base': '[po][mo][wi][sw][tt]0100090900005002'
'base': '[po][mo][wi][sw][tt][li]10009090000500'
},
'0180333331': {
'deviceType': 'haier_1',
'base': '[po][mo][wi][sw][tt]12'
'base': '[po][mo][wi][sw][tt]1'
},
'0180666661': {
'deviceType': 'aux_1',
'base': '[po][mo][wi][sw][tt]12'
'base': '[po][mo][wi][sw][tt]1'
},
'0180777771': {
'deviceType': 'chigo_1',
'base': '[po][mo][wi][sw][tt]12'
'base': '[po][mo][wi][sw][tt]1'
}
}

Expand Down Expand Up @@ -96,13 +93,18 @@ def load_power(self) -> int:
@property
def air_condition_model(self) -> str:
"""Model of the air conditioner."""
return str(self.data[0][0:2] + self.data[0][8:16])
return str(self.data[0])

@property
def power(self) -> str:
"""Current power state."""
return 'on' if (self.data[1][2:3] == '1') else 'off'

@property
def led(self) -> str:
"""Current LED state."""
return 'on' if (self.data[1][8:9] == '1') else 'off'

@property
def is_on(self) -> bool:
"""True if the device is turned on."""
Expand Down Expand Up @@ -183,26 +185,30 @@ def send_command(self, command: str):
def send_configuration(self, model: str, power: Power,
operation_mode: OperationMode,
target_temperature: float, fan_speed: FanSpeed,
swing_mode: SwingMode):
swing_mode: SwingMode, led: Led):

prefix = str(model[0:2] + model[8:16])
suffix = model[-1:]

# Static turn off command available?
if (power is Power.Off) and (model in DEVICE_COMMAND_TEMPLATES) and \
(POWER_OFF in DEVICE_COMMAND_TEMPLATES[model]):
if (power is Power.Off) and (prefix in DEVICE_COMMAND_TEMPLATES) and \
(POWER_OFF in DEVICE_COMMAND_TEMPLATES[prefix]):
return self.send_command(
model + DEVICE_COMMAND_TEMPLATES[model][POWER_OFF])
prefix + DEVICE_COMMAND_TEMPLATES[prefix][POWER_OFF])

if model in DEVICE_COMMAND_TEMPLATES:
configuration = model + DEVICE_COMMAND_TEMPLATES[model]['base']
if prefix in DEVICE_COMMAND_TEMPLATES:
configuration = prefix + DEVICE_COMMAND_TEMPLATES[prefix]['base']
else:
configuration = \
model + DEVICE_COMMAND_TEMPLATES['fallback']['base']
prefix + DEVICE_COMMAND_TEMPLATES['fallback']['base']

configuration = configuration.replace('[po]', str(power.value))
configuration = configuration.replace('[mo]', str(operation_mode.value))
configuration = configuration.replace('[wi]', str(fan_speed.value))
configuration = configuration.replace('[sw]', str(swing_mode.value))
configuration = configuration.replace(
'[tt]', hex(int(target_temperature))[2:])
configuration = configuration.replace('[li]', str(led.value))

temperature = (1 + int(target_temperature) - 17) % 16
temperature = hex(temperature)[2:].upper()
Expand All @@ -216,4 +222,6 @@ def send_configuration(self, model: str, power: Power,
temperature = hex(temperature)[2:].upper()
configuration = configuration.replace('[tt7]', temperature)

configuration = configuration + suffix

return self.send_command(configuration)
20 changes: 12 additions & 8 deletions miio/tests/test_airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest import TestCase
from miio import AirConditioningCompanion
from miio.airconditioningcompanion import OperationMode, FanSpeed, Power, \
SwingMode, STORAGE_SLOT_ID
SwingMode, Led, STORAGE_SLOT_ID
import pytest

STATE_ON = ['on']
Expand Down Expand Up @@ -80,11 +80,12 @@ def test_status(self):

assert self.is_on() is False
assert self.state().load_power == 2
assert self.state().air_condition_model == '0180222221'
assert self.state().air_condition_model == '010500978022222102'
assert self.state().temperature == 25
assert self.state().swing_mode is False
assert self.state().fan_speed == FanSpeed.Low
assert self.state().mode == OperationMode.Auto
assert self.state().led == 'off'

def test_status_without_temperature(self):
self.device._reset_state()
Expand Down Expand Up @@ -121,30 +122,33 @@ def test_send_command(self):
def test_send_configuration(self):
def send_configuration_known_aircondition():
return self.device.send_configuration(
'0100010727',
'010000000001072700', # best guess
Power.On,
OperationMode.Auto,
22.5,
FanSpeed.Low,
SwingMode.On)
SwingMode.On,
Led.Off)

def send_configuration_known_aircondition_turn_off():
return self.device.send_configuration(
'0100010727',
'010000000001072700', # best guess
Power.Off,
OperationMode.Auto,
22.5,
FanSpeed.Low,
SwingMode.On)
SwingMode.On,
Led.Off)

def send_configuration_unknown_aircondition():
return self.device.send_configuration(
'01000fffff',
'010507950000257301',
Power.On,
OperationMode.Auto,
22.5,
FanSpeed.Low,
SwingMode.On)
SwingMode.On,
Led.Off)

assert send_configuration_known_aircondition() is True
assert send_configuration_known_aircondition_turn_off() is True
Expand Down