This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
forked from bdraco/baf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.py
127 lines (112 loc) · 4.01 KB
/
number.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
"""Support for Big Ass Fans number."""
from __future__ import annotations
from typing import cast
from aiobafi6 import Device
from homeassistant import config_entries
from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
NumberMode,
)
from homeassistant.const import TIME_SECONDS
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN, HALF_DAY_SECS, ONE_DAY_SECS, ONE_MIN_SECS, SPEED_RANGE
from .entity import BAFEntity
from .models import BAFData
MODES = {
"return_to_auto_timeout": NumberMode.SLIDER,
"motion_sense_timeout": NumberMode.SLIDER,
"light_return_to_auto_timeout": NumberMode.SLIDER,
"light_auto_motion_timeout": NumberMode.SLIDER,
}
FAN_NUMBER_DESCRIPTIONS = (
NumberEntityDescription(
key="return_to_auto_timeout",
name="Return to Auto Timeout",
min_value=ONE_MIN_SECS,
max_value=HALF_DAY_SECS,
entity_category=EntityCategory.CONFIG,
unit_of_measurement=TIME_SECONDS,
),
NumberEntityDescription(
key="motion_sense_timeout",
name="Motion Sense Timeout",
min_value=ONE_MIN_SECS,
max_value=ONE_DAY_SECS,
entity_category=EntityCategory.CONFIG,
unit_of_measurement=TIME_SECONDS,
),
NumberEntityDescription(
key="comfort_min_speed",
name="Auto Comfort Minimum Speed",
min_value=0,
max_value=SPEED_RANGE[1] - 1,
entity_category=EntityCategory.CONFIG,
),
NumberEntityDescription(
key="comfort_max_speed",
name="Auto Comfort Maximum Speed",
min_value=1,
max_value=SPEED_RANGE[1],
entity_category=EntityCategory.CONFIG,
),
NumberEntityDescription(
key="comfort_heat_assist_speed",
name="Auto Comfort Heat Assist Speed",
min_value=SPEED_RANGE[0],
max_value=SPEED_RANGE[1],
entity_category=EntityCategory.CONFIG,
),
)
LIGHT_NUMBER_DESCRIPTIONS = (
NumberEntityDescription(
key="light_return_to_auto_timeout",
name="Light Return to Auto Timeout",
min_value=ONE_MIN_SECS,
max_value=HALF_DAY_SECS,
entity_category=EntityCategory.CONFIG,
unit_of_measurement=TIME_SECONDS,
),
NumberEntityDescription(
key="light_auto_motion_timeout",
name="Light Motion Sense Timeout",
min_value=ONE_MIN_SECS,
max_value=ONE_DAY_SECS,
entity_category=EntityCategory.CONFIG,
unit_of_measurement=TIME_SECONDS,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: config_entries.ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up BAF numbers."""
data: BAFData = hass.data[DOMAIN][entry.entry_id]
device = data.device
descriptions: list[NumberEntityDescription] = []
if device.has_fan:
descriptions.extend(FAN_NUMBER_DESCRIPTIONS)
if device.has_light:
descriptions.extend(LIGHT_NUMBER_DESCRIPTIONS)
async_add_entities(BAFNumber(device, description) for description in descriptions)
class BAFNumber(BAFEntity, NumberEntity):
"""BAF number."""
entity_description: NumberEntityDescription
def __init__(self, device: Device, description: NumberEntityDescription) -> None:
"""Initialize the entity."""
self.entity_description = description
super().__init__(device, f"{device.name} {description.name}")
self._attr_unique_id = f"{self._device.mac_address}-{description.key}"
self._attr_mode = MODES.get(description.key, NumberMode.BOX)
@callback
def _async_update_attrs(self) -> None:
"""Update attrs from device."""
self._attr_value = cast(
float, getattr(self._device, self.entity_description.key)
)
async def async_set_value(self, value: float) -> None:
"""Set the value."""
setattr(self._device, self.entity_description.key, int(value))