Skip to content

Commit

Permalink
LED setter implemented.
Browse files Browse the repository at this point in the history
Code refactored.
  • Loading branch information
syssi committed Mar 6, 2018
1 parent b3831f4 commit 9cd9bfe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
47 changes: 25 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,7 +93,7 @@ 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:
Expand Down Expand Up @@ -188,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 @@ -221,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)
19 changes: 11 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,7 +80,7 @@ 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
Expand Down Expand Up @@ -122,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

0 comments on commit 9cd9bfe

Please sign in to comment.