Skip to content

Commit

Permalink
new speaker volume
Browse files Browse the repository at this point in the history
  • Loading branch information
llluis committed Feb 14, 2024
1 parent 9e72520 commit 9e9c2dd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
16 changes: 16 additions & 0 deletions devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SatelliteDevice:
noise_suppression_level: int = 0
auto_gain: int = 0
volume_multiplier: float = 1.0
speaker_volume: float = 1.0

_is_active_listener: Callable[[], None] | None = None
_is_muted_listener: Callable[[], None] | None = None
Expand Down Expand Up @@ -92,6 +93,14 @@ def set_volume_multiplier(self, volume_multiplier: float) -> None:
if self._audio_settings_listener is not None:
self._audio_settings_listener()

@callback
def set_speaker_volume(self, speaker_volume: float) -> None:
"""Set speaker volume."""
if speaker_volume != self.speaker_volume:
self.speaker_volume = speaker_volume
if self._audio_settings_listener is not None:
self._audio_settings_listener()

@callback
def set_play_media_listener(self, play_media_listener: Callable[[str], None]) -> None:
"""Listen for updates to play media."""
Expand Down Expand Up @@ -172,3 +181,10 @@ def get_volume_multiplier_entity_id(self, hass: HomeAssistant) -> str | None:
return ent_reg.async_get_entity_id(
"number", DOMAIN, f"{self.satellite_id}-volume_multiplier"
)

def get_speaker_volume_entity_id(self, hass: HomeAssistant) -> str | None:
"""Return entity id for speaker volume."""
ent_reg = er.async_get(hass)
return ent_reg.async_get_entity_id(
"number", DOMAIN, f"{self.satellite_id}-speaker_volume"
)
35 changes: 35 additions & 0 deletions number.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
_MAX_AUTO_GAIN: Final = 31
_MIN_VOLUME_MULTIPLIER: Final = 0.1
_MAX_VOLUME_MULTIPLIER: Final = 10.0
_MIN_SPEAKER_VOLUME: Final = 0.1
_MAX_SPEAKER_VOLUME: Final = 2.0


async def async_setup_entry(
Expand All @@ -37,6 +39,7 @@ async def async_setup_entry(
[
WyomingSatelliteAutoGainNumber(device),
WyomingSatelliteVolumeMultiplierNumber(device),
WyomingSatelliteSpeakerVolume(device),
]
)

Expand Down Expand Up @@ -100,3 +103,35 @@ async def async_set_native_value(self, value: float) -> None:
)
self.async_write_ha_state()
self._device.set_volume_multiplier(self._attr_native_value)


class WyomingSatelliteSpeakerVolume(WyomingSatelliteEntity, RestoreNumber):
"""Entity to represent speaker volume."""

entity_description = NumberEntityDescription(
key="speaker_volume",
translation_key="speaker_volume",
entity_category=EntityCategory.CONFIG,
)
_attr_should_poll = False
_attr_native_min_value = _MIN_SPEAKER_VOLUME
_attr_native_max_value = _MAX_SPEAKER_VOLUME
_attr_native_step = 0.1
_attr_native_value = 1.0

async def async_added_to_hass(self) -> None:
"""When entity is added to Home Assistant."""
await super().async_added_to_hass()
last_number_data = await self.async_get_last_number_data()
if (last_number_data is not None) and (
last_number_data.native_value is not None
):
await self.async_set_native_value(last_number_data.native_value)

async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
self._attr_native_value = float(
max(_MIN_SPEAKER_VOLUME, min(_MAX_SPEAKER_VOLUME, value))
)
self.async_write_ha_state()
self._device.set_speaker_volume(self._attr_native_value)
30 changes: 17 additions & 13 deletions satellite.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,18 @@ def _play_media(self, media_id: str) -> None:
else:
_LOGGER.warning("media_id not supported: %s", media_id)


def _remote_trigger(self, question_id: Optional[str] = None) -> None:
if self.is_running and (not self.device.is_muted) and (not self._is_pipeline_running):
if (
self.is_running
and (not self.device.is_muted)
and (not self._is_pipeline_running)
):
# Ask client of answer to a question
ask = Detection(
name = "remote",
name="remote",
).event()
if question_id is not None:
ask.data['question_id'] = question_id
ask.data["question_id"] = question_id
self.hass.add_job(self._client.write_event(ask))

def _send_pause(self) -> None:
Expand Down Expand Up @@ -508,10 +511,10 @@ async def _stream_tts(self, media_id: str) -> None:
extension, data = await tts.async_get_media_source_audio(self.hass, media_id)

if extension == "mp3":
_LOGGER.debug("Converting mp3 to wav")
data = await tts.async_convert_audio(self.hass, "mp3", data, "wav")
extension = "wav"


if extension != "wav":
raise ValueError(f"Cannot stream audio format to satellite: {extension}")

Expand All @@ -522,14 +525,15 @@ async def _stream_tts(self, media_id: str) -> None:
_LOGGER.debug("Streaming %s TTS sample(s)", wav_file.getnframes())

timestamp = 0
await self._client.write_event(
AudioStart(
rate=sample_rate,
width=sample_width,
channels=sample_channels,
timestamp=timestamp,
).event()
)
audiostart = AudioStart(
rate=sample_rate,
width=sample_width,
channels=sample_channels,
timestamp=timestamp,
).event()
audiostart.data["speaker_vol"] = self.device.speaker_volume

await self._client.write_event(audiostart)

# Stream audio chunks
while audio_bytes := wav_file.readframes(_SAMPLES_PER_CHUNK):
Expand Down
3 changes: 3 additions & 0 deletions strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
},
"volume_multiplier": {
"name": "Mic volume"
},
"speaker_volume": {
"name": "Speaker volume"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
},
"volume_multiplier": {
"name": "Mic volume"
},
"speaker_volume": {
"name": "Speaker volume"
}
}
},
Expand Down

0 comments on commit 9e9c2dd

Please sign in to comment.