1 """Support for Modern Forms Fan Fans."""
3 from __future__
import annotations
7 from aiomodernforms.const
import FAN_POWER_OFF, FAN_POWER_ON
8 import voluptuous
as vol
16 percentage_to_ranged_value,
17 ranged_value_to_percentage,
21 from .
import modernforms_exception_handler
28 SERVICE_CLEAR_FAN_SLEEP_TIMER,
29 SERVICE_SET_FAN_SLEEP_TIMER,
31 from .coordinator
import ModernFormsDataUpdateCoordinator
32 from .entity
import ModernFormsDeviceEntity
37 config_entry: ConfigEntry,
38 async_add_entities: AddEntitiesCallback,
40 """Set up a Modern Forms platform from config entry."""
42 coordinator: ModernFormsDataUpdateCoordinator = hass.data[DOMAIN][
46 platform = entity_platform.async_get_current_platform()
48 platform.async_register_entity_service(
49 SERVICE_SET_FAN_SLEEP_TIMER,
51 vol.Required(ATTR_SLEEP_TIME): vol.All(
52 vol.Coerce(int), vol.Range(min=1, max=1440)
55 "async_set_fan_sleep_timer",
58 platform.async_register_entity_service(
59 SERVICE_CLEAR_FAN_SLEEP_TIMER,
61 "async_clear_fan_sleep_timer",
70 """Defines a Modern Forms light."""
74 _attr_supported_features = (
75 FanEntityFeature.DIRECTION
76 | FanEntityFeature.SET_SPEED
77 | FanEntityFeature.TURN_OFF
78 | FanEntityFeature.TURN_ON
80 _attr_translation_key =
"fan"
81 _enable_turn_on_off_backwards_compatibility =
False
84 self, entry_id: str, coordinator: ModernFormsDataUpdateCoordinator
86 """Initialize Modern Forms light."""
89 coordinator=coordinator,
95 """Return the current speed percentage."""
97 if bool(self.coordinator.data.state.fan_on):
99 self.
SPEED_RANGESPEED_RANGE, self.coordinator.data.state.fan_speed
105 """Return the current direction of the fan."""
106 return self.coordinator.data.state.fan_direction
110 """Return the number of speeds the fan supports."""
115 """Return the state of the fan."""
116 return bool(self.coordinator.data.state.fan_on)
118 @modernforms_exception_handler
120 """Set the direction of the fan."""
121 await self.coordinator.modern_forms.fan(direction=direction)
123 @modernforms_exception_handler
125 """Set the speed percentage of the fan."""
131 @modernforms_exception_handler
134 percentage: int |
None =
None,
135 preset_mode: str |
None =
None,
138 """Turn on the fan."""
139 data = {OPT_ON: FAN_POWER_ON}
142 data[OPT_SPEED] = round(
145 await self.coordinator.modern_forms.fan(**data)
147 @modernforms_exception_handler
149 """Turn the fan off."""
150 await self.coordinator.modern_forms.fan(on=FAN_POWER_OFF)
152 @modernforms_exception_handler
157 """Set a Modern Forms light sleep timer."""
158 await self.coordinator.modern_forms.fan(sleep=sleep_time * 60)
160 @modernforms_exception_handler
164 """Clear a Modern Forms fan sleep timer."""
165 await self.coordinator.modern_forms.fan(sleep=CLEAR_TIMER)
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_turn_on(self, **Any kwargs)
float percentage_to_ranged_value(tuple[float, float] low_high_range, float percentage)
int ranged_value_to_percentage(tuple[float, float] low_high_range, float value)
int int_states_in_range(tuple[float, float] low_high_range)