1
0
mirror of https://github.com/elisspace/core.git synced 2026-09-15 15:33:01 +00:00

Allow configuring SIP port in VoIP (#92210)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Michael Hansen
2023-05-01 15:42:27 -05:00
committed by Franck Nijhof
parent 05530d656a
commit 0ba662e7bc
9 changed files with 210 additions and 22 deletions

View File

@@ -2,7 +2,7 @@
from __future__ import annotations
from unittest.mock import Mock, patch
from unittest.mock import AsyncMock, Mock, patch
import pytest
from voip_utils import CallInfo
@@ -27,7 +27,10 @@ def config_entry(hass: HomeAssistant) -> MockConfigEntry:
@pytest.fixture
async def setup_voip(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
"""Set up VoIP integration."""
with patch("homeassistant.components.voip._create_sip_server", return_value=Mock()):
with patch(
"homeassistant.components.voip._create_sip_server",
return_value=(Mock(), AsyncMock()),
):
assert await async_setup_component(hass, DOMAIN, {})
assert config_entry.state == ConfigEntryState.LOADED
yield

View File

@@ -1,11 +1,13 @@
"""Test VoIP config flow."""
from unittest.mock import patch
from homeassistant import config_entries
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import voip
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
async def test_form_user(hass: HomeAssistant) -> None:
"""Test user form config flow."""
@@ -40,3 +42,40 @@ async def test_single_instance(
)
assert result["type"] == "abort"
assert result["reason"] == "single_instance_allowed"
async def test_options_flow(hass: HomeAssistant) -> None:
"""Test config flow options."""
config_entry = MockConfigEntry(
domain=voip.DOMAIN,
data={},
unique_id="1234",
)
config_entry.add_to_hass(hass)
assert config_entry.options == {}
result = await hass.config_entries.options.async_init(
config_entry.entry_id,
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init"
# Default
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={},
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert config_entry.options == {"sip_port": 5060}
# Manual
result = await hass.config_entries.options.async_init(
config_entry.entry_id,
)
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"sip_port": 5061},
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert config_entry.options == {"sip_port": 5061}

View File

@@ -0,0 +1,55 @@
"""Test SIP server."""
import socket
import pytest
from homeassistant import config_entries
from homeassistant.components import voip
from homeassistant.core import HomeAssistant
async def test_create_sip_server(hass: HomeAssistant, socket_enabled) -> None:
"""Tests starting/stopping SIP server."""
result = await hass.config_entries.flow.async_init(
voip.DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{},
)
entry = result["result"]
await hass.async_block_till_done()
with pytest.raises(OSError), socket.socket(
socket.AF_INET, socket.SOCK_DGRAM
) as sock:
# Server should have the port
sock.bind(("127.0.0.1", 5060))
# Configure different port
result = await hass.config_entries.options.async_init(
entry.entry_id,
)
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"sip_port": 5061},
)
await hass.async_block_till_done()
# Server should be stopped now on 5060
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.bind(("127.0.0.1", 5060))
with pytest.raises(OSError), socket.socket(
socket.AF_INET, socket.SOCK_DGRAM
) as sock:
# Server should now have the new port
sock.bind(("127.0.0.1", 5061))
# Shut down
await hass.config_entries.async_remove(entry.entry_id)
await hass.async_block_till_done()
# Server should be stopped
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.bind(("127.0.0.1", 5061))

View File

@@ -237,9 +237,15 @@ async def test_tts_timeout(
)
)
def send_audio(*args, **kwargs):
tone_bytes = bytes([1, 2, 3, 4])
def send_audio(audio_bytes, **kwargs):
if audio_bytes == tone_bytes:
# Not TTS
return
# Block here to force a timeout in _send_tts
time.sleep(1)
time.sleep(2)
async def async_get_media_source_audio(
hass: HomeAssistant,
@@ -263,10 +269,13 @@ async def test_tts_timeout(
hass.config.language,
voip_device,
Context(),
listening_tone_enabled=False,
processing_tone_enabled=False,
error_tone_enabled=False,
listening_tone_enabled=True,
processing_tone_enabled=True,
error_tone_enabled=True,
)
rtp_protocol._tone_bytes = tone_bytes
rtp_protocol._processing_bytes = tone_bytes
rtp_protocol._error_bytes = tone_bytes
rtp_protocol.transport = Mock()
rtp_protocol.send_audio = Mock(side_effect=send_audio)