Repository navigation
Expand file tree
/
Copy pathswitch.py
More file actions
484 lines (405 loc) · 16.1 KB
/
Copy pathswitch.py
File metadata and controls
484 lines (405 loc) · 16.1 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
"""StormAudio switches."""
from __future__ import annotations
from .stormaudio_telnet.constants import PowerCommand
from .stormaudio_telnet.telnet_client import DeviceState, ProcessorState
from homeassistant.components.switch import SwitchEntity
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
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(
[
StormAudioMuteSwitch(
coordinator,
f"{device_unique_id}_mute",
f"{device_name} Mute",
device_info,
),
StormAudioPowerSwitch(
coordinator,
f"{device_unique_id}_power",
f"{device_name} Power",
device_info,
),
StormAudioToggleSwitch(
coordinator,
f"{device_unique_id}_dim",
f"{device_name} Dim",
device_info,
"mdi:brightness-6",
lambda device_state: device_state.dim,
lambda coordinator: coordinator.async_set_dim(True),
lambda coordinator: coordinator.async_set_dim(False),
),
StormAudioToggleSwitch(
coordinator,
f"{device_unique_id}_dolby_virtualizer",
f"{device_name} Dolby Virtualizer",
device_info,
"mdi:surround-sound",
lambda device_state: device_state.dolby_virtualizer,
lambda coordinator: coordinator.async_set_dolby_virtualizer(True),
lambda coordinator: coordinator.async_set_dolby_virtualizer(False),
),
StormAudioToggleSwitch(
coordinator,
f"{device_unique_id}_dialog_norm",
f"{device_name} Dialog Norm",
device_info,
"mdi:message-text",
lambda device_state: device_state.dialog_norm,
lambda coordinator: coordinator.async_set_dialog_norm(True),
lambda coordinator: coordinator.async_set_dialog_norm(False),
),
StormAudioToggleSwitch(
coordinator,
f"{device_unique_id}_center_spread",
f"{device_name} Center Spread",
device_info,
"mdi:arrow-expand-horizontal",
lambda device_state: device_state.center_spread,
lambda coordinator: coordinator.async_set_center_spread(True),
lambda coordinator: coordinator.async_set_center_spread(False),
),
StormAudioToggleSwitch(
coordinator,
f"{device_unique_id}_lfe_dim",
f"{device_name} LFE Dim",
device_info,
"mdi:volume-vibrate",
lambda device_state: device_state.lfe_dim,
lambda coordinator: coordinator.async_set_lfe_dim(True),
lambda coordinator: coordinator.async_set_lfe_dim(False),
),
]
)
# --- Per-zone switches (Mute, EQ, Binaural Mode) ---
device_state = coordinator.data["device_state"]
zones = device_state.zones if device_state is not None else None
if zones:
zone_entities = []
for zone in zones:
zone_entities.extend(
[
StormAudioZoneToggleSwitch(
coordinator,
f"{device_unique_id}_zone{zone.id}_mute",
f"{device_name} {zone.name} Mute",
device_info,
"mdi:volume-mute",
zone.id,
lambda z: z.mute,
lambda coordinator, zone_id: coordinator.async_set_zone_mute(
zone_id, True
),
lambda coordinator, zone_id: coordinator.async_set_zone_mute(
zone_id, False
),
),
StormAudioZoneToggleSwitch(
coordinator,
f"{device_unique_id}_zone{zone.id}_eq",
f"{device_name} {zone.name} EQ",
device_info,
"mdi:equalizer",
zone.id,
lambda z: z.eq,
lambda coordinator, zone_id: coordinator.async_set_zone_eq(
zone_id, True
),
lambda coordinator, zone_id: coordinator.async_set_zone_eq(
zone_id, False
),
),
StormAudioZoneToggleSwitch(
coordinator,
f"{device_unique_id}_zone{zone.id}_binaural_mode",
f"{device_name} {zone.name} Binaural Mode",
device_info,
"mdi:headphones",
zone.id,
lambda z: z.binaural_mode,
lambda coordinator, zone_id: coordinator.async_set_zone_binaural_mode(
zone_id, True
),
lambda coordinator, zone_id: coordinator.async_set_zone_binaural_mode(
zone_id, False
),
),
]
)
add_entities(zone_entities)
# --- Per-trigger switches ---
trigger_names = device_state.trigger_names if device_state is not None else None
if trigger_names:
trigger_entities = []
for idx, trigger_name in enumerate(trigger_names):
trigger_num = idx + 1
trigger_entities.append(
StormAudioTriggerSwitch(
coordinator,
f"{device_unique_id}_trigger{trigger_num}",
f"{device_name} Trigger: {trigger_name}",
device_info,
trigger_num,
)
)
add_entities(trigger_entities)
class StormAudioMuteSwitch(CoordinatorEntity, SwitchEntity):
"""StormAudio mute switch."""
def __init__(
self,
coordinator: StormAudioCoordinator,
unique_id: str,
name: str,
parent_device_info: DeviceInfo,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_native_value = None
self._attr_unique_id = unique_id
self._attr_icon = "mdi:volume-mute"
self._attr_name = name
self._attr_device_info = parent_device_info
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 device_state.volume_db is not None:
self._attr_available = device_state.processor_state in [
ProcessorState.ON,
ProcessorState.INITIALIZING,
]
self._attr_native_value = device_state.mute
@property
def is_on(self) -> bool:
"""Return the state of the switch (muted == True/"on")."""
return self._attr_native_value
async def async_turn_on(self) -> None:
"""Turn the switch on (mute)."""
await self.coordinator.async_set_mute(True)
async def async_turn_off(self) -> None:
"""Turn the switch off (unmute)."""
await self.coordinator.async_set_mute(False)
class StormAudioPowerSwitch(CoordinatorEntity, SwitchEntity):
"""StormAudio power switch."""
def __init__(
self,
coordinator: StormAudioCoordinator,
unique_id: str,
name: str,
parent_device_info: DeviceInfo,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_native_value = None
self._attr_unique_id = unique_id
self._attr_icon = "mdi:power"
self._attr_name = name
self._attr_device_info = parent_device_info
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:
self._attr_native_value = device_state.processor_state in [
ProcessorState.ON,
ProcessorState.INITIALIZING,
]
@property
def is_on(self) -> bool:
"""Return the state of the switch (on == True/"on")."""
return self._attr_native_value
async def async_turn_on(self) -> None:
"""Turn the switch on."""
await self.coordinator.async_set_power_state(PowerCommand.ON)
async def async_turn_off(self) -> None:
"""Turn the switch off."""
await self.coordinator.async_set_power_state(PowerCommand.OFF)
class StormAudioToggleSwitch(CoordinatorEntity, SwitchEntity):
"""Generic StormAudio boolean toggle switch (Dim, Dolby
Virtualizer, Dialog Norm, Center Spread, LFE Dim)."""
def __init__(
self,
coordinator: StormAudioCoordinator,
unique_id: str,
name: str,
parent_device_info: DeviceInfo,
icon: str,
get_value_fn,
async_turn_on_fn,
async_turn_off_fn,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_native_value = None
self._attr_unique_id = unique_id
self._attr_icon = icon
self._attr_name = name
self._attr_device_info = parent_device_info
self._get_value_fn = get_value_fn
self._async_turn_on_fn = async_turn_on_fn
self._async_turn_off_fn = async_turn_off_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
value = self._get_value_fn(device_state) if device_state is not None else None
if value is not None:
self._attr_available = device_state.processor_state in [
ProcessorState.ON,
ProcessorState.INITIALIZING,
]
self._attr_native_value = value
else:
self._attr_available = False
@property
def is_on(self) -> bool:
"""Return the state of the switch."""
return self._attr_native_value
async def async_turn_on(self) -> None:
"""Turn the switch on."""
await self._async_turn_on_fn(self.coordinator)
async def async_turn_off(self) -> None:
"""Turn the switch off."""
await self._async_turn_off_fn(self.coordinator)
def _find_zone(device_state: DeviceState, zone_id: int):
"""Look up a zone by ID in the current zones list."""
if device_state is None or device_state.zones is None:
return None
for zone in device_state.zones:
if zone.id == zone_id:
return zone
return None
class StormAudioZoneToggleSwitch(CoordinatorEntity, SwitchEntity):
"""Per-zone boolean toggle switch (Mute, EQ, Binaural Mode)."""
def __init__(
self,
coordinator: StormAudioCoordinator,
unique_id: str,
name: str,
parent_device_info: DeviceInfo,
icon: str,
zone_id: int,
get_zone_value_fn,
async_turn_on_fn,
async_turn_off_fn,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_native_value = None
self._attr_unique_id = unique_id
self._attr_icon = icon
self._attr_name = name
self._attr_device_info = parent_device_info
self._attr_entity_registry_enabled_default = False
self._zone_id = zone_id
self._get_zone_value_fn = get_zone_value_fn
self._async_turn_on_fn = async_turn_on_fn
self._async_turn_off_fn = async_turn_off_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
zone = _find_zone(device_state, self._zone_id)
value = self._get_zone_value_fn(zone) if zone is not None else None
if value is not None:
self._attr_native_value = value
else:
self._attr_available = False
@property
def is_on(self) -> bool:
"""Return the state of the switch."""
return self._attr_native_value
async def async_turn_on(self) -> None:
"""Turn the switch on."""
await self._async_turn_on_fn(self.coordinator, self._zone_id)
async def async_turn_off(self) -> None:
"""Turn the switch off."""
await self._async_turn_off_fn(self.coordinator, self._zone_id)
class StormAudioTriggerSwitch(CoordinatorEntity, SwitchEntity):
"""12V trigger output switch."""
def __init__(
self,
coordinator: StormAudioCoordinator,
unique_id: str,
name: str,
parent_device_info: DeviceInfo,
trigger_num: int,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_native_value = None
self._attr_unique_id = unique_id
self._attr_icon = "mdi:electric-switch"
self._attr_name = name
self._attr_device_info = parent_device_info
self._attr_entity_registry_enabled_default = False
self._trigger_num = trigger_num
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
value = (
device_state.trigger_states.get(self._trigger_num)
if device_state is not None
else None
)
if value is not None:
self._attr_native_value = value
else:
self._attr_available = False
@property
def is_on(self) -> bool:
"""Return the state of the switch."""
return self._attr_native_value
async def async_turn_on(self) -> None:
"""Turn the switch on."""
await self.coordinator.async_set_trigger(self._trigger_num, True)
async def async_turn_off(self) -> None:
"""Turn the switch off."""
await self.coordinator.async_set_trigger(self._trigger_num, False)