Skip to content

Commit

Permalink
Refactor air purifier (#400)
Browse files Browse the repository at this point in the history
* Refactor purifier

* Remove commend

* Change property

* Add tests

* Fix typing
  • Loading branch information
ggravlingen authored Dec 29, 2021
1 parent 99c207d commit 8d55851
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pytradfri/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.2.1
8.0.0
3 changes: 2 additions & 1 deletion pytradfri/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ATTR_BLIND_TRIGGER = "5523"

ATTR_AIR_PURIFIER_MODE = "5900"
ATTR_AIR_PURIFIER_MODE_AUTO = 1
ATTR_AIR_PURIFIER_CONTROLS_LOCKED = "5905"
ATTR_AIR_PURIFIER_LEDS_OFF = "5906"
ATTR_AIR_PURIFIER_AIR_QUALITY = "5907"
Expand Down Expand Up @@ -144,7 +145,7 @@

RANGE_BLIND = (0, 100)

RANGE_AIR_PURIFIER = (0, 50)
RANGE_AIR_PURIFIER = (2, 50)

# XY color
RANGE_X = (0, 65535)
Expand Down
25 changes: 13 additions & 12 deletions pytradfri/device/air_purifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ATTR_AIR_PURIFIER_FAN_SPEED,
ATTR_AIR_PURIFIER_LEDS_OFF,
ATTR_AIR_PURIFIER_MODE,
ATTR_AIR_PURIFIER_MODE_AUTO,
ROOT_AIR_PURIFIER,
)
from ..resource import TypeRaw, TypeRawList
Expand All @@ -35,25 +36,25 @@ def raw(self) -> TypeRaw:
)

@property
def mode(self) -> int:
"""Return the current mode of the air purifier.
0: off
1: Fan level auto
10: Fan level 1
20: Fan level 2
30: Fan level 3
40: Fan level 4
50: Fan level 5
def is_auto_mode(self) -> bool:
"""
return cast(int, self.raw[ATTR_AIR_PURIFIER_MODE])
Return auto mode on or off.
Auto mode sets the fan speed automatically based on the air quality.
"""
return self.raw[ATTR_AIR_PURIFIER_MODE] == ATTR_AIR_PURIFIER_MODE_AUTO

@property
def state(self) -> bool:
"""Return device state, ie on or off."""
return cast(int, self.raw[ATTR_AIR_PURIFIER_MODE]) > 0

@property
def fan_speed(self) -> int:
"""Get the current fan speed of the air purifier.
0: Device is off
10..50: Fan speed with a step size of 5
2..50: Fan speed with a step size of 1.
"""
return cast(int, self.raw[ATTR_AIR_PURIFIER_FAN_SPEED])

Expand Down
22 changes: 12 additions & 10 deletions pytradfri/device/air_purifier_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ATTR_AIR_PURIFIER_CONTROLS_LOCKED,
ATTR_AIR_PURIFIER_LEDS_OFF,
ATTR_AIR_PURIFIER_MODE,
ATTR_AIR_PURIFIER_MODE_AUTO,
RANGE_AIR_PURIFIER,
ROOT_AIR_PURIFIER,
)
Expand All @@ -29,17 +30,18 @@ def air_purifiers(self) -> list[AirPurifier]:
"""Return air purifier objects of the air purifier control."""
return [AirPurifier(self._device, i) for i in range(len(self.raw))]

def set_mode(self, mode: int, *, index=0) -> Command:
"""Set mode of a air purifier.
def turn_off(self, *, index=0) -> Command:
"""Turn the device off."""
return self.set_value({ATTR_AIR_PURIFIER_MODE: 0}, index=index)

0: off
1: Fan level auto
10: Fan level 1
20: Fan level 2
30: Fan level 3
40: Fan level 4
50: Fan level 5
"""
def turn_on_auto_mode(self, *, index=0) -> Command:
"""Turn on auto mode."""
return self.set_value(
{ATTR_AIR_PURIFIER_MODE: ATTR_AIR_PURIFIER_MODE_AUTO}, index=index
)

def set_fan_speed(self, mode: int, *, index=0) -> Command:
"""Set the fan speed of the purifier."""
self._value_validate(mode, RANGE_AIR_PURIFIER, "Air Purifier mode")
return self.set_value({ATTR_AIR_PURIFIER_MODE: mode}, index=index)

Expand Down
11 changes: 9 additions & 2 deletions tests/test_air_purifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ def test_device_info_properties(device):
assert info.serial == ""


def test_mode(device):
def test_state(device):
"""Test air purifier mode."""

air_purifier = device.air_purifier_control.air_purifiers[0]
assert air_purifier.mode == 1
assert air_purifier.state is True


def test_is_auto_mode(device):
"""Test air purifier mode."""

air_purifier = device.air_purifier_control.air_purifiers[0]
assert air_purifier.is_auto_mode is True


def test_air_quality(device):
Expand Down

0 comments on commit 8d55851

Please sign in to comment.