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

Tests for the Xiaomi Air Conditioning Companion #182

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 15 additions & 11 deletions miio/airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ def is_on(self) -> bool:
return self.power == 'on'

@property
def temperature(self) -> int:
def temperature(self) -> Optional[int]:
"""Current temperature."""
return int(self.data[1][6:8], 16)
try:
return int(self.data[1][6:8], 16)
except ValueError:
return None

@property
def swing_mode(self) -> bool:
Expand All @@ -113,20 +116,20 @@ def swing_mode(self) -> bool:
@property
def fan_speed(self) -> Optional[FanSpeed]:
"""Current fan speed."""
speed = int(self.data[1][4:5])
if speed is not None:
try:
speed = int(self.data[1][4:5])
return FanSpeed(speed)

return None
except ValueError:
return None

@property
def mode(self) -> Optional[OperationMode]:
"""Current operation mode."""
mode = int(self.data[1][3:4])
if mode is not None:
try:
mode = int(self.data[1][3:4])
return OperationMode(mode)

return None
except ValueError:
return None


class AirConditioningCompanion(Device):
Expand Down Expand Up @@ -175,7 +178,8 @@ def send_configuration(self, model: str, power: Power,
if model in DEVICE_COMMAND_TEMPLATES:
configuration = model + DEVICE_COMMAND_TEMPLATES[model]['base']
else:
configuration = model + DEVICE_COMMAND_TEMPLATES['fallback']['base']
configuration = \
model + DEVICE_COMMAND_TEMPLATES['fallback']['base']

configuration = configuration.replace('[po]', power.value)
configuration = configuration.replace('[mo]', operation_mode.value)
Expand Down
16 changes: 16 additions & 0 deletions miio/tests/test_airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ def test_status(self):
assert self.state().swing_mode is False
assert self.state().fan_speed == FanSpeed.Low
assert self.state().mode == OperationMode.Auto

def test_status_without_temperature(self):
self.device._reset_state()
self.device.state[1] = None
assert self.state().temperature is None

def test_status_without_mode(self):
self.device._reset_state()
self.device.state[1] = None
assert self.state().mode is None

def test_status_without_fan_speed(self):
self.device._reset_state()
self.device.state[1] = None
assert self.state().fan_speed is None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line at end of file