1 """Support for Homekit fans."""
3 from __future__
import annotations
7 from aiohomekit.model.characteristics
import CharacteristicsTypes
8 from aiohomekit.model.services
import Service, ServicesTypes
9 from propcache
import cached_property
22 percentage_to_ranged_value,
23 ranged_value_to_percentage,
26 from .
import KNOWN_DEVICES
27 from .connection
import HKDevice
28 from .entity
import HomeKitEntity
36 HK_DIRECTION_TO_HA = {v: k
for (k, v)
in DIRECTION_TO_HK.items()}
40 """Representation of a Homekit fan."""
44 on_characteristic: str
45 _enable_turn_on_off_backwards_compatibility =
False
49 """Reconfigure entity."""
62 """Define the homekit characteristics the entity cares about."""
64 CharacteristicsTypes.SWING_MODE,
65 CharacteristicsTypes.ROTATION_DIRECTION,
66 CharacteristicsTypes.ROTATION_SPEED,
67 self.on_characteristic,
72 """Return true if device is on."""
73 return self.
serviceservice.value(self.on_characteristic) == 1
77 """Return the speed range."""
82 """Return the minimum speed."""
84 round(self.
serviceservice[CharacteristicsTypes.ROTATION_SPEED].minValue
or 0) + 1
89 """Return the minimum speed."""
90 return round(self.
serviceservice[CharacteristicsTypes.ROTATION_SPEED].maxValue
or 100)
94 """Return the current speed percentage."""
99 self.
_speed_range_speed_range, self.
serviceservice.value(CharacteristicsTypes.ROTATION_SPEED)
104 """Return the current direction of the fan."""
105 direction = self.
serviceservice.value(CharacteristicsTypes.ROTATION_DIRECTION)
106 return HK_DIRECTION_TO_HA[direction]
110 """Return whether or not the fan is currently oscillating."""
111 oscillating = self.
serviceservice.value(CharacteristicsTypes.SWING_MODE)
112 return oscillating == 1
116 """Flag supported features."""
117 features = FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON
119 if self.
serviceservice.has(CharacteristicsTypes.ROTATION_DIRECTION):
120 features |= FanEntityFeature.DIRECTION
122 if self.
serviceservice.has(CharacteristicsTypes.ROTATION_SPEED):
123 features |= FanEntityFeature.SET_SPEED
125 if self.
serviceservice.has(CharacteristicsTypes.SWING_MODE):
126 features |= FanEntityFeature.OSCILLATE
132 """Speed count for the fan."""
135 /
max(1, self.
serviceservice[CharacteristicsTypes.ROTATION_SPEED].minStep
or 0)
139 """Set the direction of the fan."""
141 {CharacteristicsTypes.ROTATION_DIRECTION: DIRECTION_TO_HK[direction]}
145 """Set the speed of the fan."""
152 CharacteristicsTypes.ROTATION_SPEED: round(
159 """Oscillate the fan."""
161 {CharacteristicsTypes.SWING_MODE: 1
if oscillating
else 0}
166 percentage: int |
None =
None,
167 preset_mode: str |
None =
None,
170 """Turn the specified fan on."""
171 characteristics: dict[str, Any] = {}
174 characteristics[self.on_characteristic] =
True
177 percentage
is not None
180 characteristics[CharacteristicsTypes.ROTATION_SPEED] = round(
188 """Turn the specified fan off."""
193 """Implement fan support for public.hap.service.fan."""
195 on_characteristic = CharacteristicsTypes.ON
199 """Implement fan support for public.hap.service.fanv2."""
201 on_characteristic = CharacteristicsTypes.ACTIVE
205 ServicesTypes.FAN: HomeKitFanV1,
206 ServicesTypes.FAN_V2: HomeKitFanV2,
207 ServicesTypes.AIR_PURIFIER: HomeKitFanV2,
213 config_entry: ConfigEntry,
214 async_add_entities: AddEntitiesCallback,
216 """Set up Homekit fans."""
217 hkid: str = config_entry.data[
"AccessoryPairingID"]
218 conn: HKDevice = hass.data[KNOWN_DEVICES][hkid]
221 def async_add_service(service: Service) -> bool:
222 if not (entity_class := ENTITY_TYPES.get(service.type)):
224 info = {
"aid": service.accessory.aid,
"iid": service.iid}
225 entity: HomeKitEntity = entity_class(conn, info)
226 conn.async_migrate_unique_id(
227 entity.old_unique_id, entity.unique_id, Platform.FAN
232 conn.add_listener(async_add_service)
FanEntityFeature supported_features(self)
None async_put_characteristics(self, dict[str, Any] characteristics)
None _async_clear_property_cache(self, tuple[str,...] properties)
None async_oscillate(self, bool oscillating)
FanEntityFeature supported_features(self)
None async_set_percentage(self, int percentage)
None async_turn_off(self, **Any kwargs)
tuple[int, int] _speed_range(self)
None async_turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
str current_direction(self)
None _async_reconfigure(self)
list[str] get_characteristic_types(self)
None async_set_direction(self, str direction)
int|None supported_features(self)
None async_turn_off(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
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)