hass-core/tests/components/camera/conftest.py
Robert Resch 675ee8e813
Add async webrtc offer support (#127981)
* Add async webrtc offer support

* Create dataclass for messages

* Send session ID over websocket

* Fixes

* Rename

* Implement some review findings

* Add WebRTCError and small renames

* Use dedicated function instead of inspec

* Update go2rtc-client to 0.0.1b1

* Improve checking for sync offer

* Revert change as not needed anymore

* Typo

* Fix tests

* Add missing go2rtc tests

* Move webrtc offer tests to test_webrtc file

* Add ws camera/webrtc/candidate tests

* Add missing tests

* Implement suggestions

* Implement review changes

* rename

* Revert test to use ws endpoints

* Change doc string

* Don't import from submodule

* Get type form class name

* Update homeassistant/components/camera/__init__.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Adopt tests

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Fix tests

---------

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Erik <erik@montnemery.com>
2024-10-28 15:46:15 +01:00

144 lines
4.5 KiB
Python

"""Test helpers for camera."""
from collections.abc import AsyncGenerator, Generator
from unittest.mock import AsyncMock, PropertyMock, patch
import pytest
from homeassistant.components import camera
from homeassistant.components.camera.const import StreamType
from homeassistant.components.camera.webrtc import WebRTCAnswer, WebRTCSendMessage
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.setup import async_setup_component
from .common import STREAM_SOURCE, WEBRTC_ANSWER
@pytest.fixture(autouse=True)
async def setup_homeassistant(hass: HomeAssistant) -> None:
"""Set up the homeassistant integration."""
await async_setup_component(hass, "homeassistant", {})
@pytest.fixture(autouse=True)
def camera_only() -> Generator[None]:
"""Enable only the camera platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.CAMERA],
):
yield
@pytest.fixture(name="mock_camera")
async def mock_camera_fixture(hass: HomeAssistant) -> AsyncGenerator[None]:
"""Initialize a demo camera platform."""
assert await async_setup_component(
hass, "camera", {camera.DOMAIN: {"platform": "demo"}}
)
await hass.async_block_till_done()
with patch(
"homeassistant.components.demo.camera.Path.read_bytes",
return_value=b"Test",
):
yield
@pytest.fixture(name="mock_camera_hls")
def mock_camera_hls_fixture(mock_camera: None) -> Generator[None]:
"""Initialize a demo camera platform with HLS."""
with patch(
"homeassistant.components.camera.Camera.frontend_stream_type",
new_callable=PropertyMock(return_value=StreamType.HLS),
):
yield
@pytest.fixture
async def mock_camera_webrtc_frontendtype_only(
hass: HomeAssistant,
) -> AsyncGenerator[None]:
"""Initialize a demo camera platform with WebRTC."""
assert await async_setup_component(
hass, "camera", {camera.DOMAIN: {"platform": "demo"}}
)
await hass.async_block_till_done()
with patch(
"homeassistant.components.camera.Camera.frontend_stream_type",
new_callable=PropertyMock(return_value=StreamType.WEB_RTC),
):
yield
@pytest.fixture
async def mock_camera_webrtc(
mock_camera_webrtc_frontendtype_only: None,
) -> AsyncGenerator[None]:
"""Initialize a demo camera platform with WebRTC."""
async def async_handle_async_webrtc_offer(
offer_sdp: str, session_id: str, send_message: WebRTCSendMessage
) -> None:
send_message(WebRTCAnswer(WEBRTC_ANSWER))
with patch(
"homeassistant.components.camera.Camera.async_handle_async_webrtc_offer",
side_effect=async_handle_async_webrtc_offer,
):
yield
@pytest.fixture(name="mock_camera_with_device")
def mock_camera_with_device_fixture() -> Generator[None]:
"""Initialize a demo camera platform with a device."""
dev_info = DeviceInfo(
identifiers={("camera", "test_unique_id")},
name="Test Camera Device",
)
class UniqueIdMock(PropertyMock):
def __get__(self, obj, obj_type=None):
return obj.name
with (
patch(
"homeassistant.components.camera.Camera.has_entity_name",
new_callable=PropertyMock(return_value=True),
),
patch("homeassistant.components.camera.Camera.unique_id", new=UniqueIdMock()),
patch(
"homeassistant.components.camera.Camera.device_info",
new_callable=PropertyMock(return_value=dev_info),
),
):
yield
@pytest.fixture(name="mock_camera_with_no_name")
def mock_camera_with_no_name_fixture(mock_camera_with_device: None) -> Generator[None]:
"""Initialize a demo camera platform with a device and no name."""
with patch(
"homeassistant.components.camera.Camera._attr_name",
new_callable=PropertyMock(return_value=None),
):
yield
@pytest.fixture(name="mock_stream")
async def mock_stream_fixture(hass: HomeAssistant) -> None:
"""Initialize a demo camera platform with streaming."""
assert await async_setup_component(hass, "stream", {"stream": {}})
@pytest.fixture(name="mock_stream_source")
def mock_stream_source_fixture() -> Generator[AsyncMock]:
"""Fixture to create an RTSP stream source."""
with patch(
"homeassistant.components.camera.Camera.stream_source",
return_value=STREAM_SOURCE,
) as mock_stream_source:
yield mock_stream_source