1 """Represent an air purifier."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from typing
import Any, cast
8 from pytradfri.command
import Command
15 from .const
import CONF_GATEWAY_ID, COORDINATOR, COORDINATOR_LIST, DOMAIN, KEY_API
16 from .coordinator
import TradfriDeviceDataUpdateCoordinator
17 from .entity
import TradfriBaseEntity
20 ATTR_MAX_FAN_STEPS = 49
24 """Convert percent to a value that the Tradfri API understands."""
25 return round(
max(2, (percentage / 100 * ATTR_MAX_FAN_STEPS) + 1))
29 """Convert the Tradfri API fan speed to a percentage value."""
30 return max(round((fan_speed - 1) / ATTR_MAX_FAN_STEPS * 100), 0)
35 config_entry: ConfigEntry,
36 async_add_entities: AddEntitiesCallback,
38 """Load Tradfri switches based on a config entry."""
39 gateway_id = config_entry.data[CONF_GATEWAY_ID]
40 coordinator_data = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
41 api = coordinator_data[KEY_API]
49 for device_coordinator
in coordinator_data[COORDINATOR_LIST]
50 if device_coordinator.device.has_air_purifier_control
55 """The platform class required by Home Assistant."""
58 _attr_supported_features = (
59 FanEntityFeature.PRESET_MODE
60 | FanEntityFeature.SET_SPEED
61 | FanEntityFeature.TURN_ON
62 | FanEntityFeature.TURN_OFF
64 _attr_preset_modes = [ATTR_AUTO]
71 _attr_speed_count = ATTR_MAX_FAN_STEPS
72 _enable_turn_on_off_backwards_compatibility =
False
76 device_coordinator: TradfriDeviceDataUpdateCoordinator,
77 api: Callable[[Command | list[Command]], Any],
80 """Initialize a switch."""
82 device_coordinator=device_coordinator,
84 gateway_id=gateway_id,
91 """Refresh the device."""
92 self.
_device_data_device_data = self.coordinator.data.air_purifier_control.air_purifiers[0]
96 """Return true if switch is on."""
103 """Return the current speed percentage."""
114 """Return the current preset mode."""
124 """Set the preset mode of the fan."""
134 percentage: int |
None =
None,
135 preset_mode: str |
None =
None,
138 """Turn on the fan. Auto-mode if no argument is given."""
142 if percentage
is not None:
146 preset_mode = preset_mode
or ATTR_AUTO
150 """Set the speed percentage of the fan."""
163 """Turn off the fan."""
None async_set_percentage(self, int percentage)
None async_set_preset_mode(self, str preset_mode)
None __init__(self, TradfriDeviceDataUpdateCoordinator device_coordinator, Callable[[Command|list[Command]], Any] api, str gateway_id)
None async_set_preset_mode(self, str preset_mode)
int|None percentage(self)
str|None preset_mode(self)
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
None async_set_percentage(self, int percentage)
None async_turn_off(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None turn_off(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
int _from_fan_speed(int fan_speed)
int _from_fan_percentage(int percentage)