-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Expand file tree
/
Copy pathselect.py
More file actions
224 lines (175 loc) · 7.43 KB
/
select.py
File metadata and controls
224 lines (175 loc) · 7.43 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""Support for LED selects."""
from functools import partial
from wled import LiveDataOverride
from homeassistant.components.select import SelectEntity
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .coordinator import WLEDConfigEntry, WLEDDataUpdateCoordinator
from .entity import WLEDEntity
from .helpers import wled_exception_handler
PARALLEL_UPDATES = 1
async def async_setup_entry(
hass: HomeAssistant,
entry: WLEDConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up WLED select based on a config entry."""
coordinator = entry.runtime_data
async_add_entities(
[
WLEDLiveOverrideSelect(coordinator),
WLEDPlaylistSelect(coordinator),
WLEDPresetSelect(coordinator),
]
)
update_segments = partial(
async_update_segments,
coordinator,
set(),
async_add_entities,
)
coordinator.async_add_listener(update_segments)
update_segments()
class WLEDLiveOverrideSelect(WLEDEntity, SelectEntity):
"""Defined a WLED Live Override select."""
_attr_entity_category = EntityCategory.CONFIG
_attr_translation_key = "live_override"
def __init__(self, coordinator: WLEDDataUpdateCoordinator) -> None:
"""Initialize WLED ."""
super().__init__(coordinator=coordinator)
self._attr_unique_id = f"{coordinator.data.info.mac_address}_live_override"
self._attr_options = [str(live.value) for live in LiveDataOverride]
@property
def current_option(self) -> str:
"""Return the current selected live override."""
return str(self.coordinator.data.state.live_data_override.value)
@wled_exception_handler
async def async_select_option(self, option: str) -> None:
"""Set WLED state to the selected live override state."""
await self.coordinator.wled.live(live=LiveDataOverride(int(option)))
class WLEDPresetSelect(WLEDEntity, SelectEntity):
"""Defined a WLED Preset select."""
_attr_translation_key = "preset"
def __init__(self, coordinator: WLEDDataUpdateCoordinator) -> None:
"""Initialize WLED ."""
super().__init__(coordinator=coordinator)
self._attr_unique_id = f"{coordinator.data.info.mac_address}_preset"
@property
def available(self) -> bool:
"""Return True if entity is available."""
return len(self.coordinator.data.presets) > 0 and super().available
@property
def current_option(self) -> str | None:
"""Return the current selected preset."""
if not self.coordinator.data.state.preset_id:
return None
if preset := self.coordinator.data.presets.get(
self.coordinator.data.state.preset_id
):
return preset.name
return None
@property
def options(self) -> list[str]:
"""Return a list of selectable options."""
sorted_values = sorted(
self.coordinator.data.presets.values(), key=lambda preset: preset.name
)
return [preset.name for preset in sorted_values]
@wled_exception_handler
async def async_select_option(self, option: str) -> None:
"""Set WLED segment to the selected preset."""
await self.coordinator.wled.preset(preset=option)
class WLEDPlaylistSelect(WLEDEntity, SelectEntity):
"""Define a WLED Playlist select."""
_attr_translation_key = "playlist"
def __init__(self, coordinator: WLEDDataUpdateCoordinator) -> None:
"""Initialize WLED playlist."""
super().__init__(coordinator=coordinator)
self._attr_unique_id = f"{coordinator.data.info.mac_address}_playlist"
@property
def available(self) -> bool:
"""Return True if entity is available."""
return len(self.coordinator.data.playlists) > 0 and super().available
@property
def current_option(self) -> str | None:
"""Return the currently selected playlist."""
if not self.coordinator.data.state.playlist_id:
return None
if playlist := self.coordinator.data.playlists.get(
self.coordinator.data.state.playlist_id
):
return playlist.name
return None
@property
def options(self) -> list[str]:
"""Return a list of selectable options."""
sorted_values = sorted(
self.coordinator.data.playlists.values(), key=lambda playlist: playlist.name
)
return [playlist.name for playlist in sorted_values]
@wled_exception_handler
async def async_select_option(self, option: str) -> None:
"""Set WLED segment to the selected playlist."""
await self.coordinator.wled.playlist(playlist=option)
class WLEDPaletteSelect(WLEDEntity, SelectEntity):
"""Defines a WLED Palette select."""
_attr_entity_category = EntityCategory.CONFIG
_attr_translation_key = "color_palette"
_segment: int
def __init__(self, coordinator: WLEDDataUpdateCoordinator, segment: int) -> None:
"""Initialize WLED ."""
super().__init__(coordinator=coordinator)
# With keep_main_light disabled, a single-segment setup uses segment 0
# as the primary entity — it drops the "Segment N" qualifier.
if segment != 0 or coordinator.keep_main_light:
self._attr_translation_key = "segment_color_palette"
self._attr_translation_placeholders = {"segment": str(segment)}
self._attr_unique_id = f"{coordinator.data.info.mac_address}_palette_{segment}"
self._segment = segment
@property
def available(self) -> bool:
"""Return True if entity is available."""
return (
super().available and self._segment in self.coordinator.data.state.segments
)
@property
def current_option(self) -> str | None:
"""Return the current selected color palette."""
if not self.coordinator.data.palettes:
return None
if (segment := self.coordinator.data.state.segments.get(self._segment)) is None:
return None
palette_id = int(segment.palette_id)
if (palette := self.coordinator.data.palettes.get(palette_id)) is None:
return None
return palette.name
@property
def options(self) -> list[str]:
"""Return a list of selectable options."""
sorted_values = sorted(
self.coordinator.data.palettes.values(), key=lambda palette: palette.name
)
return [palette.name for palette in sorted_values]
@wled_exception_handler
async def async_select_option(self, option: str) -> None:
"""Set WLED segment to the selected color palette."""
await self.coordinator.wled.segment(segment_id=self._segment, palette=option)
@callback
def async_update_segments(
coordinator: WLEDDataUpdateCoordinator,
current_ids: set[int],
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Update segments."""
segment_ids = {
segment.segment_id
for segment in coordinator.data.state.segments.values()
if segment.segment_id is not None
}
new_entities: list[WLEDPaletteSelect] = []
# Process new segments, add them to Home Assistant
for segment_id in segment_ids - current_ids:
current_ids.add(segment_id)
new_entities.append(WLEDPaletteSelect(coordinator, segment_id))
async_add_entities(new_entities)