hass-core/tests/components/voip/test_sip.py
Joost Lekkerkerker 6bb4e7d62c
Bump ruff to 0.3.4 (#112690)
Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com>
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
2024-03-26 00:02:16 +01:00

58 lines
1.7 KiB
Python

"""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))