Skip to content

Commit

Permalink
Fix audio format for VoIP (home-assistant#125785)
Browse files Browse the repository at this point in the history
Fix audio format
  • Loading branch information
synesthesiam authored Sep 12, 2024
1 parent 2475e8c commit 9651072
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 11 additions & 1 deletion homeassistant/components/voip/assist_satellite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io
import logging
from pathlib import Path
from typing import TYPE_CHECKING, Final
from typing import TYPE_CHECKING, Any, Final
import wave

from voip_utils import RtpDatagramProtocol
Expand Down Expand Up @@ -120,6 +120,16 @@ def vad_sensitivity_entity_id(self) -> str | None:
"""Return the entity ID of the VAD sensitivity to use for the next conversation."""
return self.voip_device.get_vad_sensitivity_entity_id(self.hass)

@property
def tts_options(self) -> dict[str, Any] | None:
"""Options passed for text-to-speech."""
return {
tts.ATTR_PREFERRED_FORMAT: "wav",
tts.ATTR_PREFERRED_SAMPLE_RATE: 16000,
tts.ATTR_PREFERRED_SAMPLE_CHANNELS: 1,
tts.ATTR_PREFERRED_SAMPLE_BYTES: 2,
}

async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
await super().async_added_to_hass()
Expand Down
18 changes: 16 additions & 2 deletions tests/components/voip/test_voip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import asyncio
import io
from pathlib import Path
from typing import Any
from unittest.mock import AsyncMock, Mock, patch
import wave

import pytest
from syrupy.assertion import SnapshotAssertion
from voip_utils import CallInfo

from homeassistant.components import assist_pipeline, assist_satellite, voip
from homeassistant.components import assist_pipeline, assist_satellite, tts, voip
from homeassistant.components.assist_satellite.entity import (
AssistSatelliteEntity,
AssistSatelliteState,
Expand Down Expand Up @@ -205,11 +206,24 @@ async def test_pipeline(
bad_chunk = bytes([1, 2, 3, 4])

async def async_pipeline_from_audio_stream(
hass: HomeAssistant, context: Context, *args, device_id: str | None, **kwargs
hass: HomeAssistant,
context: Context,
*args,
device_id: str | None,
tts_audio_output: str | dict[str, Any] | None,
**kwargs,
):
assert context.user_id == voip_user_id
assert device_id == voip_device.device_id

# voip can only stream WAV
assert tts_audio_output == {
tts.ATTR_PREFERRED_FORMAT: "wav",
tts.ATTR_PREFERRED_SAMPLE_RATE: 16000,
tts.ATTR_PREFERRED_SAMPLE_CHANNELS: 1,
tts.ATTR_PREFERRED_SAMPLE_BYTES: 2,
}

stt_stream = kwargs["stt_stream"]
event_callback = kwargs["event_callback"]
in_command = False
Expand Down

0 comments on commit 9651072

Please sign in to comment.