1 """Support for the Vallox ventilation unit fan."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from typing
import Any, NamedTuple
8 from vallox_websocket_api
import Vallox, ValloxApiException, ValloxInvalidInputException
20 METRIC_KEY_PROFILE_FAN_SPEED_AWAY,
21 METRIC_KEY_PROFILE_FAN_SPEED_BOOST,
22 METRIC_KEY_PROFILE_FAN_SPEED_HOME,
25 PRESET_MODE_TO_VALLOX_PROFILE,
26 VALLOX_PROFILE_TO_PRESET_MODE,
28 from .coordinator
import ValloxDataUpdateCoordinator
29 from .entity
import ValloxEntity
33 """Extra state attribute details."""
39 EXTRA_STATE_ATTRIBUTES = (
41 description=
"fan_speed_home", metric_key=METRIC_KEY_PROFILE_FAN_SPEED_HOME
44 description=
"fan_speed_away", metric_key=METRIC_KEY_PROFILE_FAN_SPEED_AWAY
47 description=
"fan_speed_boost", metric_key=METRIC_KEY_PROFILE_FAN_SPEED_BOOST
53 if isinstance(value, (int, float)):
60 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
62 """Set up the fan device."""
63 data = hass.data[DOMAIN][entry.entry_id]
65 client = data[
"client"]
77 """Representation of the fan."""
80 _attr_supported_features = (
81 FanEntityFeature.PRESET_MODE
82 | FanEntityFeature.SET_SPEED
83 | FanEntityFeature.TURN_OFF
84 | FanEntityFeature.TURN_ON
86 _enable_turn_on_off_backwards_compatibility =
False
92 coordinator: ValloxDataUpdateCoordinator,
94 """Initialize the fan."""
104 """Return if device is on."""
105 return self.coordinator.data.get(METRIC_KEY_MODE) == MODE_ON
109 """Return the current preset mode."""
110 vallox_profile = self.coordinator.data.profile
111 return VALLOX_PROFILE_TO_PRESET_MODE.get(vallox_profile)
115 """Return the current speed as a percentage."""
117 vallox_profile = self.coordinator.data.profile
119 return _convert_to_int(self.coordinator.data.get_fan_speed(vallox_profile))
120 except ValloxInvalidInputException:
125 """Return device specific state attributes."""
126 data = self.coordinator.data
130 for attr
in EXTRA_STATE_ATTRIBUTES
134 """Set new preset mode."""
144 percentage: int |
None =
None,
145 preset_mode: str |
None =
None,
148 """Turn the device on."""
149 update_needed =
False
157 if percentage
is not None:
159 percentage, preset_mode
168 """Turn the device off."""
178 """Set the speed of the fan, as a percentage."""
190 await self.
_client_client.set_values(
191 {METRIC_KEY_MODE: MODE_ON
if mode
else MODE_OFF}
193 except ValloxApiException
as err:
199 """Set new preset mode.
201 Returns true if the mode has been changed, false otherwise.
207 profile = PRESET_MODE_TO_VALLOX_PROFILE[preset_mode]
208 await self.
_client_client.set_profile(profile)
210 except ValloxApiException
as err:
216 self, percentage: int, preset_mode: str |
None =
None
218 """Set fan speed percentage for current profile.
220 Returns true if speed has been changed, false otherwise.
223 PRESET_MODE_TO_VALLOX_PROFILE[preset_mode]
224 if preset_mode
is not None
225 else self.coordinator.data.profile
229 await self.
_client_client.set_fan_speed(vallox_profile, percentage)
230 except ValloxInvalidInputException
as err:
233 f
"{vallox_profile} profile does not support setting the fan speed"
235 except ValloxApiException
as err:
str|None preset_mode(self)
None async_set_percentage(self, int percentage)
int|None percentage(self)
None async_turn_off(self, **Any kwargs)
bool _async_set_percentage_internal(self, int percentage, str|None preset_mode=None)
str|None preset_mode(self)
bool _async_set_preset_mode_internal(self, str preset_mode)
None __init__(self, str name, Vallox client, ValloxDataUpdateCoordinator coordinator)
None async_set_preset_mode(self, str preset_mode)
bool _async_set_power(self, bool mode)
Mapping[str, int|None] extra_state_attributes(self)
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None async_request_refresh(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
int|None _convert_to_int(StateType value)