Home Assistant Unofficial Reference 2024.12.1
fan.py
Go to the documentation of this file.
1 """Representation of a fan."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.fan import FanEntity, FanEntityFeature
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import DOMAIN, ZWaveMePlatform
14 from .entity import ZWaveMeEntity
15 
16 DEVICE_NAME = ZWaveMePlatform.FAN
17 
18 
20  hass: HomeAssistant,
21  config_entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up the fan platform."""
25 
26  @callback
27  def add_new_device(new_device):
28  controller = hass.data[DOMAIN][config_entry.entry_id]
29  fan = ZWaveMeFan(controller, new_device)
30 
32  [
33  fan,
34  ]
35  )
36 
37  config_entry.async_on_unload(
39  hass, f"ZWAVE_ME_NEW_{DEVICE_NAME.upper()}", add_new_device
40  )
41  )
42 
43 
45  """Representation of a ZWaveMe Fan."""
46 
47  _attr_supported_features = (
48  FanEntityFeature.SET_SPEED
49  | FanEntityFeature.TURN_OFF
50  | FanEntityFeature.TURN_ON
51  )
52  _enable_turn_on_off_backwards_compatibility = False
53 
54  @property
55  def percentage(self) -> int:
56  """Return the current speed as a percentage."""
57  if self.devicedevice.level == 99: # Scale max value
58  return 100
59  return self.devicedevice.level
60 
61  def set_percentage(self, percentage: int) -> None:
62  """Set the speed percentage of the fan."""
63  self.controllercontroller.zwave_api.send_command(
64  self.devicedevice.id, f"exact?level={min(percentage, 99)}"
65  )
66 
67  def turn_off(self, **kwargs: Any) -> None:
68  """Turn the fan off."""
69  self.controllercontroller.zwave_api.send_command(self.devicedevice.id, "exact?level=0")
70 
71  def turn_on(
72  self,
73  percentage: int | None = None,
74  preset_mode: str | None = None,
75  **kwargs: Any,
76  ) -> None:
77  """Turn on the fan."""
78  self.set_percentageset_percentageset_percentage(percentage if percentage is not None else 99)
None set_percentage(self, int percentage)
Definition: __init__.py:336
None set_percentage(self, int percentage)
Definition: fan.py:61
None turn_on(self, int|None percentage=None, str|None preset_mode=None, **Any kwargs)
Definition: fan.py:76
None turn_off(self, **Any kwargs)
Definition: fan.py:67
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: fan.py:23
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103