1 """Support for Tasmota fans."""
3 from __future__
import annotations
7 from hatasmota
import const
as tasmota_const, fan
as tasmota_fan
8 from hatasmota.entity
import TasmotaEntity
as HATasmotaEntity
9 from hatasmota.models
import DiscoveryHashType
21 ordered_list_item_to_percentage,
22 percentage_to_ordered_list_item,
25 from .const
import DATA_REMOVE_DISCOVER_COMPONENT
26 from .discovery
import TASMOTA_DISCOVERY_ENTITY_NEW
27 from .entity
import TasmotaAvailability, TasmotaDiscoveryUpdate
29 ORDERED_NAMED_FAN_SPEEDS = [
30 tasmota_const.FAN_SPEED_LOW,
31 tasmota_const.FAN_SPEED_MEDIUM,
32 tasmota_const.FAN_SPEED_HIGH,
38 config_entry: ConfigEntry,
39 async_add_entities: AddEntitiesCallback,
41 """Set up Tasmota fan dynamically through discovery."""
45 tasmota_entity: HATasmotaEntity, discovery_hash: DiscoveryHashType
47 """Discover and add a Tasmota fan."""
49 [
TasmotaFan(tasmota_entity=tasmota_entity, discovery_hash=discovery_hash)]
52 hass.data[DATA_REMOVE_DISCOVER_COMPONENT.format(FAN_DOMAIN)] = (
55 TASMOTA_DISCOVERY_ENTITY_NEW.format(FAN_DOMAIN),
63 TasmotaDiscoveryUpdate,
66 """Representation of a Tasmota fan."""
68 _attr_supported_features = (
69 FanEntityFeature.SET_SPEED
70 | FanEntityFeature.TURN_OFF
71 | FanEntityFeature.TURN_ON
73 _fan_speed = tasmota_const.FAN_SPEED_MEDIUM
74 _tasmota_entity: tasmota_fan.TasmotaFan
75 _enable_turn_on_off_backwards_compatibility =
False
78 """Initialize the Tasmota fan."""
79 self.
_state_state: int |
None =
None
86 """Subscribe to MQTT events."""
92 """Handle state updates."""
94 if self.
_state_state
is not None and self.
_state_state != 0:
101 """Return the number of speeds the fan supports."""
102 return len(ORDERED_NAMED_FAN_SPEEDS)
106 """Return the current speed percentage."""
107 if self.
_state_state
is None:
109 if self.
_state_state == 0:
111 return ordered_list_item_to_percentage(ORDERED_NAMED_FAN_SPEEDS, self.
_state_state)
114 """Set the speed of the fan."""
118 tasmota_speed = percentage_to_ordered_list_item(
119 ORDERED_NAMED_FAN_SPEEDS, percentage
125 percentage: int |
None =
None,
126 preset_mode: str |
None =
None,
129 """Turn the fan on."""
133 or ordered_list_item_to_percentage(
134 ORDERED_NAMED_FAN_SPEEDS, self.
_fan_speed_fan_speed
139 """Turn the fan off."""
140 await self.
_tasmota_entity_tasmota_entity.set_speed(tasmota_const.FAN_SPEED_OFF)
None async_set_percentage(self, int percentage)
None async_added_to_hass(self)
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
None fan_state_updated(self, int state, **Any kwargs)
None async_set_percentage(self, int percentage)
None __init__(self, **Any kwds)
int|None percentage(self)
None async_turn_off(self, **Any kwargs)
None async_write_ha_state(self)
None async_turn_off(self, **Any kwargs)
None async_discover(DiscoveryInfo discovery_info)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)