Skip to content

Commit 6c309a1

Browse files
author
Ted Roberts
committed
Convert audio output mode to select with documented modes
1 parent ed48705 commit 6c309a1

5 files changed

Lines changed: 74 additions & 63 deletions

File tree

custom_components/novastar_h/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
}
2222
],
2323
"zeroconf": ["_novastar._tcp.local."],
24-
"version": "0.2.58"
24+
"version": "0.2.59"
2525
}

custom_components/novastar_h/number.py

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ async def async_setup_entry(
2525
[
2626
NovastarBrightnessNumber(entry, coordinator, device_info),
2727
NovastarAudioVolumeNumber(entry, coordinator, device_info),
28-
NovastarAudioOutputModeNumber(entry, coordinator, device_info),
2928
]
3029
)
3130

@@ -152,58 +151,3 @@ def native_value(self) -> float | None:
152151
async def async_set_native_value(self, value: float) -> None:
153152
"""Set audio volume value."""
154153
await self.coordinator.async_set_audio_volume(int(value))
155-
156-
157-
class NovastarAudioOutputModeNumber(CoordinatorEntity[NovastarCoordinator], NumberEntity):
158-
"""Number entity for audio output mode (audioOutputMode/outputChannelMode)."""
159-
160-
_attr_has_entity_name = True
161-
_attr_name = "Audio Output Mode"
162-
_attr_translation_key = "audio_output_mode"
163-
_attr_native_min_value = 0
164-
_attr_native_max_value = 255
165-
_attr_native_step = 1
166-
_attr_mode = NumberMode.BOX
167-
168-
def __init__(
169-
self,
170-
entry: ConfigEntry,
171-
coordinator: NovastarCoordinator,
172-
device_info: NovastarDeviceInfo,
173-
) -> None:
174-
"""Initialize the number entity."""
175-
super().__init__(coordinator)
176-
self._entry = entry
177-
self._device_info = device_info
178-
self._attr_unique_id = f"{entry.entry_id}_audio_output_mode"
179-
180-
@property
181-
def device_info(self):
182-
"""Return device info."""
183-
model = "H Series"
184-
if self._device_info.model_id:
185-
model = f"H Series (Model {self._device_info.model_id})"
186-
return {
187-
"identifiers": {(DOMAIN, self._entry.entry_id)},
188-
"manufacturer": "Novastar",
189-
"model": model,
190-
"name": self._entry.data.get(CONF_NAME, DEFAULT_NAME),
191-
"sw_version": self._device_info.firmware,
192-
"serial_number": self._device_info.serial,
193-
}
194-
195-
@property
196-
def available(self) -> bool:
197-
"""Return True if entity is available."""
198-
return self.coordinator.last_update_success
199-
200-
@property
201-
def native_value(self) -> float | None:
202-
"""Return current audio output mode."""
203-
if self.coordinator.data and self.coordinator.data.audio_output_id is not None:
204-
return float(self.coordinator.data.audio_output_id)
205-
return None
206-
207-
async def async_set_native_value(self, value: float) -> None:
208-
"""Set audio output mode value."""
209-
await self.coordinator.async_set_audio_output(int(value))

custom_components/novastar_h/select.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async def async_setup_entry(
9595
entities.append(NovastarBackgroundSelect(entry, coordinator, device_info))
9696
entities.append(NovastarAudioInputSelect(entry, coordinator, device_info))
9797
entities.append(NovastarAudioOutputSelect(entry, coordinator, device_info))
98+
entities.append(NovastarAudioOutputModeSelect(entry, coordinator, device_info))
9899
entities.extend(
99100
[
100101
NovastarLayerSourceSelect(entry, coordinator, device_info, layer_id)
@@ -600,3 +601,69 @@ async def async_select_option(self, option: str) -> None:
600601
if output_id is None:
601602
return
602603
await self.coordinator.async_set_audio_output(output_id)
604+
605+
606+
class NovastarAudioOutputModeSelect(_NovastarBaseAudioSelect):
607+
"""Select entity for audio output mode (audioOutputMode/outputChannelMode)."""
608+
609+
_attr_name = "Audio Output Mode"
610+
_attr_translation_key = "audio_output_mode"
611+
612+
def __init__(
613+
self,
614+
entry: ConfigEntry,
615+
coordinator: NovastarCoordinator,
616+
device_info: NovastarDeviceInfo,
617+
) -> None:
618+
"""Initialize audio output mode select."""
619+
super().__init__(entry, coordinator, device_info)
620+
self._attr_unique_id = f"{entry.entry_id}_audio_output_mode"
621+
622+
@staticmethod
623+
def _audio_output_mode_map() -> dict[str, int]:
624+
"""Map documented audio output mode labels to ids."""
625+
return {
626+
"Embedded Mode": 0,
627+
"Fixed Mode": 1,
628+
}
629+
630+
@staticmethod
631+
def _audio_output_mode_label(mode_id: int) -> str:
632+
"""Return display label for one audio output mode id."""
633+
for label, option_id in NovastarAudioOutputModeSelect._audio_output_mode_map().items():
634+
if option_id == mode_id:
635+
return label
636+
return f"Audio Output Mode {mode_id}"
637+
638+
@property
639+
def options(self) -> list[str]:
640+
"""Return available audio output mode options."""
641+
options = list(self._audio_output_mode_map().keys())
642+
current = self.current_option
643+
if current and current not in options:
644+
options.append(current)
645+
return options
646+
647+
@property
648+
def current_option(self) -> str | None:
649+
"""Return selected audio output mode option."""
650+
if not self.coordinator.data:
651+
return None
652+
current_id = _coerce_int(self.coordinator.data.audio_output_id)
653+
if current_id is None:
654+
return None
655+
return self._audio_output_mode_label(current_id)
656+
657+
async def async_select_option(self, option: str) -> None:
658+
"""Set active audio output mode."""
659+
output_id = self._audio_output_mode_map().get(option)
660+
if output_id is None:
661+
text = option.strip()
662+
if text.startswith("Audio Output Mode "):
663+
try:
664+
output_id = int(text.replace("Audio Output Mode ", "").strip())
665+
except ValueError:
666+
return
667+
if output_id is None:
668+
return
669+
await self.coordinator.async_set_audio_output(output_id)

custom_components/novastar_h/strings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
"audio_output": {
130130
"name": "Audio Output"
131131
},
132+
"audio_output_mode": {
133+
"name": "Audio Output Mode"
134+
},
132135
"layer_source": {
133136
"name": "Layer Source"
134137
}
@@ -139,9 +142,6 @@
139142
},
140143
"audio_volume": {
141144
"name": "Audio Volume"
142-
},
143-
"audio_output_mode": {
144-
"name": "Audio Output Mode"
145145
}
146146
},
147147
"sensor": {

custom_components/novastar_h/translations/en.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
"audio_output": {
130130
"name": "Audio Output"
131131
},
132+
"audio_output_mode": {
133+
"name": "Audio Output Mode"
134+
},
132135
"layer_source": {
133136
"name": "Layer Source"
134137
}
@@ -139,9 +142,6 @@
139142
},
140143
"audio_volume": {
141144
"name": "Audio Volume"
142-
},
143-
"audio_output_mode": {
144-
"name": "Audio Output Mode"
145145
}
146146
},
147147
"sensor": {

0 commit comments

Comments
 (0)