Repository navigation
Expand file tree
/
Copy pathselect.py
More file actions
386 lines (343 loc) · 13.6 KB
/
Copy pathselect.py
File metadata and controls
386 lines (343 loc) · 13.6 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""StormAudio selects."""
from __future__ import annotations
import itertools
from .stormaudio_telnet.constants import (
AuroPreset,
DolbyMode,
FRONT_PANEL_COLORS,
FRONT_PANEL_STANDBY_DELAYS,
LoudnessLevel,
SphereAudioEffect,
)
from .stormaudio_telnet.telnet_client import DeviceState, ProcessorState
from homeassistant.components.select import SelectEntity
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import helpers
from .const import DOMAIN
from .coordinator import StormAudioCoordinator
DOLBY_MODE_NAMES = {
DolbyMode.OFF.value: "Off",
DolbyMode.MOVIE.value: "Movie",
DolbyMode.MUSIC.value: "Music",
DolbyMode.NIGHT.value: "Night",
}
AURO_PRESET_NAMES = {
AuroPreset.SMALL.value: "Small",
AuroPreset.MEDIUM.value: "Medium",
AuroPreset.LARGE.value: "Large",
AuroPreset.SPEECH.value: "Speech",
}
SPHEREAUDIO_EFFECT_NAMES = {
SphereAudioEffect.BYPASS.value: "Bypass",
SphereAudioEffect.LOUNGE.value: "Lounge",
SphereAudioEffect.HOME_CINEMA.value: "Home Cinema",
SphereAudioEffect.CONCERT.value: "Concert",
SphereAudioEffect.CINEMA.value: "Cinema",
}
LOUDNESS_NAMES = {
LoudnessLevel.OFF.value: "Off",
LoudnessLevel.LOW.value: "Low",
LoudnessLevel.MEDIUM.value: "Medium",
LoudnessLevel.FULL.value: "Full",
}
FRONTPANEL_STANDBY_DELAY_NAMES = {
seconds: f"{seconds}s" for seconds in FRONT_PANEL_STANDBY_DELAYS
}
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
) -> None:
"""Setup config entry."""
coordinator: StormAudioCoordinator = hass.data[DOMAIN][config.entry_id][
"coordinator"
]
await helpers.async_wait_2_seconds_for_initial_device_state_or_raise_platform_not_ready(
coordinator
)
device_info = coordinator.data["device_info"]
device_unique_id = coordinator.data["device_unique_id"]
device_name = coordinator.data["device_name"]
add_entities(
[
StormAudioSelect(
coordinator,
f"{device_unique_id}_source",
f"{device_name} Source",
device_info,
lambda device_state: (
device_state.inputs is not None
and device_state.input_id is not None
),
lambda device_state: ((i.id, i.name) for i in device_state.inputs),
lambda device_state: device_state.input_id,
lambda coordinator, selected_id: coordinator.async_set_input_id(
selected_id
),
),
StormAudioSelect(
coordinator,
f"{device_unique_id}_source_zone2",
f"{device_name} Source (Zone 2)",
device_info,
lambda device_state: (
device_state.inputs is not None
and device_state.input_zone2_id is not None
),
lambda device_state: itertools.chain(
((i.id, i.name) for i in device_state.inputs),
# add an entry to allow for selection of no-value; maps to ID 0
[(0, "")],
),
lambda device_state: device_state.input_zone2_id,
lambda coordinator, selected_id: coordinator.async_set_input_zone2_id(
selected_id
),
enabled_default=False,
),
StormAudioSelect(
coordinator,
f"{device_unique_id}_preset",
f"{device_name} Preset",
device_info,
lambda device_state: (
device_state.presets is not None
and device_state.preset_id is not None
),
lambda device_state: ((i.id, i.name) for i in device_state.presets),
lambda device_state: device_state.preset_id,
lambda coordinator, selected_id: coordinator.async_set_preset_id(
selected_id
),
),
StormAudioSelect(
coordinator,
f"{device_unique_id}_dolby_mode",
f"{device_name} Dolby Mode",
device_info,
lambda device_state: device_state.dolby_mode is not None,
lambda device_state: DOLBY_MODE_NAMES.items(),
lambda device_state: device_state.dolby_mode,
lambda coordinator, selected_id: coordinator.async_set_dolby_mode(
selected_id
),
),
StormAudioSelect(
coordinator,
f"{device_unique_id}_auro_preset",
f"{device_name} Auro Preset",
device_info,
lambda device_state: device_state.auro_preset is not None,
lambda device_state: AURO_PRESET_NAMES.items(),
lambda device_state: device_state.auro_preset,
lambda coordinator, selected_id: coordinator.async_set_auro_preset(
selected_id
),
enabled_default=False,
),
StormAudioSelect(
coordinator,
f"{device_unique_id}_sphereaudio_effect",
f"{device_name} SphereAudio Effect",
device_info,
lambda device_state: isinstance(
device_state.sphereaudio_effect, int
),
lambda device_state: SPHEREAUDIO_EFFECT_NAMES.items(),
lambda device_state: device_state.sphereaudio_effect,
lambda coordinator, selected_id: coordinator.async_set_sphereaudio_effect(
selected_id
),
),
StormAudioSelect(
coordinator,
f"{device_unique_id}_loudness",
f"{device_name} Loudness",
device_info,
lambda device_state: device_state.loudness is not None,
lambda device_state: LOUDNESS_NAMES.items(),
lambda device_state: device_state.loudness,
lambda coordinator, selected_id: coordinator.async_set_loudness(
selected_id
),
),
StormAudioSelect(
coordinator,
f"{device_unique_id}_frontpanel_standby_delay",
f"{device_name} Front Panel Standby Delay",
device_info,
lambda device_state: device_state.frontpanel_stbytime is not None,
lambda device_state: FRONTPANEL_STANDBY_DELAY_NAMES.items(),
lambda device_state: device_state.frontpanel_stbytime,
lambda coordinator, selected_id: coordinator.async_set_frontpanel_stbytime(
selected_id
),
enabled_default=False,
),
StormAudioStringSelect(
coordinator,
f"{device_unique_id}_drc",
f"{device_name} DRC",
device_info,
["on", "off", "auto"],
lambda device_state: device_state.drc,
lambda coordinator, selected: coordinator.async_set_drc(selected),
enabled_default=False,
),
StormAudioStringSelect(
coordinator,
f"{device_unique_id}_imax_mode",
f"{device_name} IMAX Mode",
device_info,
["on", "off", "auto"],
lambda device_state: device_state.imax_mode,
lambda coordinator, selected: coordinator.async_set_imax_mode(
selected
),
enabled_default=False,
),
StormAudioStringSelect(
coordinator,
f"{device_unique_id}_frontpanel_color",
f"{device_name} Front Panel Color",
device_info,
FRONT_PANEL_COLORS,
lambda device_state: device_state.frontpanel_color,
lambda coordinator, selected: coordinator.async_set_frontpanel_color(
selected
),
enabled_default=False,
),
]
)
class StormAudioSelect(CoordinatorEntity, SelectEntity):
"""StormAudio select."""
def __init__(
self,
coordinator: StormAudioCoordinator,
unique_id: str,
name: str,
parent_device_info: DeviceInfo,
is_data_available_fn,
get_id_name_map_fn,
get_current_id_fn,
async_set_current_id_fn,
enabled_default: bool = True,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_options = None
self._attr_current_option = None
self._attr_unique_id = unique_id
self._attr_icon = "mdi:audio-video"
self._attr_name = name
self._attr_device_info = parent_device_info
self._attr_entity_registry_enabled_default = enabled_default
self._is_data_available_fn = is_data_available_fn
self._get_id_name_map_fn = get_id_name_map_fn
self._get_current_id_fn = get_current_id_fn
self._async_set_current_id_fn = async_set_current_id_fn
self._set_state_from_device()
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._set_state_from_device()
self.async_write_ha_state()
def _set_state_from_device(self):
device_state: DeviceState = self.coordinator.data["device_state"]
self._attr_available = self.coordinator.connected
if device_state is not None and self._is_data_available_fn(device_state):
self._attr_available = device_state.processor_state in [
ProcessorState.ON,
ProcessorState.INITIALIZING,
]
id_name_map = self._get_id_name_map_fn(device_state)
self._id_to_name = dict(id_name_map)
self._name_to_id = {v: k for k, v in self._id_to_name.items()}
self._attr_options = list(self._name_to_id.keys())
self._attr_current_option = None
current_id = self._get_current_id_fn(device_state)
if self._id_to_name is not None and current_id in self._id_to_name:
self._attr_current_option = self._id_to_name[current_id]
@property
def available(self) -> bool:
"""Return True if entity is available."""
available: bool = self._attr_available
if available:
available = super().available
return available
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
if self._name_to_id is None:
return
option_id: int
if option not in self._name_to_id:
option_id = 0
else:
option_id = self._name_to_id[option]
await self._async_set_current_id_fn(self.coordinator, option_id)
self._attr_current_option = option
self.async_write_ha_state()
class StormAudioStringSelect(CoordinatorEntity, SelectEntity):
"""StormAudio select backed by a plain string value rather than an
ID/name map (DRC, IMAX Mode, Front Panel Color)."""
def __init__(
self,
coordinator: StormAudioCoordinator,
unique_id: str,
name: str,
parent_device_info: DeviceInfo,
options: list[str],
get_current_value_fn,
async_set_value_fn,
enabled_default: bool = True,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_options = options
self._attr_current_option = None
self._attr_unique_id = unique_id
self._attr_icon = "mdi:tune"
self._attr_name = name
self._attr_device_info = parent_device_info
self._attr_entity_registry_enabled_default = enabled_default
self._get_current_value_fn = get_current_value_fn
self._async_set_value_fn = async_set_value_fn
self._set_state_from_device()
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._set_state_from_device()
self.async_write_ha_state()
def _set_state_from_device(self):
device_state: DeviceState = self.coordinator.data["device_state"]
self._attr_available = self.coordinator.connected
current = (
self._get_current_value_fn(device_state)
if device_state is not None
else None
)
if current is not None:
self._attr_available = device_state.processor_state in [
ProcessorState.ON,
ProcessorState.INITIALIZING,
]
self._attr_current_option = current
else:
self._attr_available = False
@property
def available(self) -> bool:
"""Return True if entity is available."""
available: bool = self._attr_available
if available:
available = super().available
return available
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self._async_set_value_fn(self.coordinator, option)
self._attr_current_option = option
self.async_write_ha_state()