From 8d558510999524b2952f99282dade69ee1d974fa Mon Sep 17 00:00:00 2001 From: Patrik Lindgren <21142447+ggravlingen@users.noreply.github.com> Date: Wed, 29 Dec 2021 11:16:33 +0100 Subject: [PATCH] Refactor air purifier (#400) * Refactor purifier * Remove commend * Change property * Add tests * Fix typing --- pytradfri/VERSION | 2 +- pytradfri/const.py | 3 ++- pytradfri/device/air_purifier.py | 25 ++++++++++++------------ pytradfri/device/air_purifier_control.py | 22 +++++++++++---------- tests/test_air_purifiers.py | 11 +++++++++-- 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/pytradfri/VERSION b/pytradfri/VERSION index b26a34e4..ae9a76b9 100644 --- a/pytradfri/VERSION +++ b/pytradfri/VERSION @@ -1 +1 @@ -7.2.1 +8.0.0 diff --git a/pytradfri/const.py b/pytradfri/const.py index 65193952..94b763c2 100644 --- a/pytradfri/const.py +++ b/pytradfri/const.py @@ -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" @@ -144,7 +145,7 @@ RANGE_BLIND = (0, 100) -RANGE_AIR_PURIFIER = (0, 50) +RANGE_AIR_PURIFIER = (2, 50) # XY color RANGE_X = (0, 65535) diff --git a/pytradfri/device/air_purifier.py b/pytradfri/device/air_purifier.py index c68f1348..5c8544c1 100644 --- a/pytradfri/device/air_purifier.py +++ b/pytradfri/device/air_purifier.py @@ -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 @@ -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]) diff --git a/pytradfri/device/air_purifier_control.py b/pytradfri/device/air_purifier_control.py index b14063e7..1af9c5bf 100644 --- a/pytradfri/device/air_purifier_control.py +++ b/pytradfri/device/air_purifier_control.py @@ -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, ) @@ -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) diff --git a/tests/test_air_purifiers.py b/tests/test_air_purifiers.py index 3912447c..f8a998e3 100644 --- a/tests/test_air_purifiers.py +++ b/tests/test_air_purifiers.py @@ -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):