-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathfan.py
135 lines (110 loc) · 4.13 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""Fan definition for Intellifire."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
import math
from typing import Any
from intellifire4py.control import IntelliFireController
from intellifire4py.model import IntelliFirePollData
from homeassistant.components.fan import (
FanEntity,
FanEntityDescription,
FanEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.percentage import (
percentage_to_ranged_value,
ranged_value_to_percentage,
)
from .const import DOMAIN, LOGGER
from .coordinator import IntellifireDataUpdateCoordinator
from .entity import IntellifireEntity
@dataclass(frozen=True)
class IntellifireFanRequiredKeysMixin:
"""Required keys for fan entity."""
set_fn: Callable[[IntelliFireController, int], Awaitable]
value_fn: Callable[[IntelliFirePollData], int]
speed_range: tuple[int, int]
@dataclass(frozen=True)
class IntellifireFanEntityDescription(
FanEntityDescription, IntellifireFanRequiredKeysMixin
):
"""Describes a fan entity."""
INTELLIFIRE_FANS: tuple[IntellifireFanEntityDescription, ...] = (
IntellifireFanEntityDescription(
key="fan",
translation_key="fan",
set_fn=lambda control_api, speed: control_api.set_fan_speed(speed=speed),
value_fn=lambda data: data.fanspeed,
speed_range=(1, 4),
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the fans."""
coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
if coordinator.data.has_fan:
async_add_entities(
IntellifireFan(coordinator=coordinator, description=description)
for description in INTELLIFIRE_FANS
)
return
LOGGER.debug("Disabling Fan - IntelliFire device does not appear to have one")
class IntellifireFan(IntellifireEntity, FanEntity):
"""Fan entity for the fireplace."""
entity_description: IntellifireFanEntityDescription
_attr_supported_features = (
FanEntityFeature.SET_SPEED
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
@property
def is_on(self) -> bool:
"""Return on or off."""
return self.entity_description.value_fn(self.coordinator.read_api.data) >= 1
@property
def percentage(self) -> int | None:
"""Return fan percentage."""
return ranged_value_to_percentage(
self.entity_description.speed_range,
self.coordinator.read_api.data.fanspeed,
)
@property
def speed_count(self) -> int:
"""Count of supported speeds."""
return self.entity_description.speed_range[1]
async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan."""
# Calculate percentage steps
LOGGER.debug("Setting Fan Speed %s", percentage)
int_value = math.ceil(
percentage_to_ranged_value(self.entity_description.speed_range, percentage)
)
await self.entity_description.set_fn(self.coordinator.control_api, int_value)
await self.coordinator.async_request_refresh()
async def async_turn_on(
self,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs: Any,
) -> None:
"""Turn on the fan."""
if percentage:
int_value = math.ceil(
percentage_to_ranged_value(
self.entity_description.speed_range, percentage
)
)
else:
int_value = 1
await self.entity_description.set_fn(self.coordinator.control_api, int_value)
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the fan."""
await self.entity_description.set_fn(self.coordinator.control_api, 0)
await self.coordinator.async_request_refresh()