-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfan.py
81 lines (62 loc) · 2.53 KB
/
fan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Support for Netatmo/Bubendorff fans."""
from __future__ import annotations
import logging
from typing import Final
from pyatmo import modules as NaModules
from homeassistant.components.fan import FanEntity, FanEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_URL_CONTROL, NETATMO_CREATE_FAN
from .data_handler import HOME, SIGNAL_NAME, NetatmoDevice
from .entity import NetatmoModuleEntity
_LOGGER = logging.getLogger(__name__)
DEFAULT_PERCENTAGE: Final = 50
PRESET_MAPPING = {"slow": 1, "fast": 2}
PRESETS = {v: k for k, v in PRESET_MAPPING.items()}
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Netatmo fan platform."""
@callback
def _create_entity(netatmo_device: NetatmoDevice) -> None:
entity = NetatmoFan(netatmo_device)
_LOGGER.debug("Adding cover %s", entity)
async_add_entities([entity])
entry.async_on_unload(
async_dispatcher_connect(hass, NETATMO_CREATE_FAN, _create_entity)
)
class NetatmoFan(NetatmoModuleEntity, FanEntity):
"""Representation of a Netatmo fan."""
_attr_preset_modes = ["slow", "fast"]
_attr_supported_features = FanEntityFeature.PRESET_MODE
_attr_configuration_url = CONF_URL_CONTROL
_attr_name = None
device: NaModules.Fan
_enable_turn_on_off_backwards_compatibility = False
def __init__(self, netatmo_device: NetatmoDevice) -> None:
"""Initialize of Netatmo fan."""
super().__init__(netatmo_device)
self._publishers.extend(
[
{
"name": HOME,
"home_id": self.home.entity_id,
SIGNAL_NAME: f"{HOME}-{self.home.entity_id}",
},
]
)
self._attr_unique_id = f"{self.device.entity_id}-{self.device_type}"
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode of the fan."""
await self.device.async_set_fan_speed(PRESET_MAPPING[preset_mode])
@callback
def async_update_callback(self) -> None:
"""Update the entity's state."""
if self.device.fan_speed is None:
self._attr_preset_mode = None
return
self._attr_preset_mode = PRESETS.get(self.device.fan_speed)