mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Implement async_get_stream_source in the camera integration (#35704)
This commit is contained in:
parent
6fbc3c4a51
commit
3dfeec5033
@ -161,6 +161,14 @@ async def async_get_image(hass, entity_id, timeout=10):
|
|||||||
raise HomeAssistantError("Unable to get image")
|
raise HomeAssistantError("Unable to get image")
|
||||||
|
|
||||||
|
|
||||||
|
@bind_hass
|
||||||
|
async def async_get_stream_source(hass, entity_id):
|
||||||
|
"""Fetch the stream source for a camera entity."""
|
||||||
|
camera = _get_camera_from_entity_id(hass, entity_id)
|
||||||
|
|
||||||
|
return await camera.stream_source()
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
async def async_get_mjpeg_stream(hass, request, entity_id):
|
async def async_get_mjpeg_stream(hass, request, entity_id):
|
||||||
"""Fetch an mjpeg stream from a camera entity."""
|
"""Fetch an mjpeg stream from a camera entity."""
|
||||||
|
@ -12,7 +12,6 @@ from pyhap.camera import (
|
|||||||
)
|
)
|
||||||
from pyhap.const import CATEGORY_CAMERA
|
from pyhap.const import CATEGORY_CAMERA
|
||||||
|
|
||||||
from homeassistant.components.camera.const import DOMAIN as DOMAIN_CAMERA
|
|
||||||
from homeassistant.components.ffmpeg import DATA_FFMPEG
|
from homeassistant.components.ffmpeg import DATA_FFMPEG
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
@ -126,7 +125,6 @@ class Camera(HomeAccessory, PyhapCamera):
|
|||||||
"""Initialize a Camera accessory object."""
|
"""Initialize a Camera accessory object."""
|
||||||
self._ffmpeg = hass.data[DATA_FFMPEG]
|
self._ffmpeg = hass.data[DATA_FFMPEG]
|
||||||
self._cur_session = None
|
self._cur_session = None
|
||||||
self._camera = hass.data[DOMAIN_CAMERA]
|
|
||||||
for config_key in CONFIG_DEFAULTS:
|
for config_key in CONFIG_DEFAULTS:
|
||||||
if config_key not in config:
|
if config_key not in config:
|
||||||
config[config_key] = CONFIG_DEFAULTS[config_key]
|
config[config_key] = CONFIG_DEFAULTS[config_key]
|
||||||
@ -188,14 +186,13 @@ class Camera(HomeAccessory, PyhapCamera):
|
|||||||
|
|
||||||
async def _async_get_stream_source(self):
|
async def _async_get_stream_source(self):
|
||||||
"""Find the camera stream source url."""
|
"""Find the camera stream source url."""
|
||||||
camera = self._camera.get_entity(self.entity_id)
|
|
||||||
if not camera or not camera.is_on:
|
|
||||||
return None
|
|
||||||
stream_source = self.config.get(CONF_STREAM_SOURCE)
|
stream_source = self.config.get(CONF_STREAM_SOURCE)
|
||||||
if stream_source:
|
if stream_source:
|
||||||
return stream_source
|
return stream_source
|
||||||
try:
|
try:
|
||||||
stream_source = await camera.stream_source()
|
stream_source = await self.hass.components.camera.async_get_stream_source(
|
||||||
|
self.entity_id
|
||||||
|
)
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
_LOGGER.exception(
|
_LOGGER.exception(
|
||||||
"Failed to get stream source - this could be a transient error or your camera might not be compatible with HomeKit yet"
|
"Failed to get stream source - this could be a transient error or your camera might not be compatible with HomeKit yet"
|
||||||
|
@ -67,6 +67,19 @@ async def test_get_image_from_camera(hass, image_mock_url):
|
|||||||
assert image.content == b"Test"
|
assert image.content == b"Test"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_stream_source_from_camera(hass, mock_camera):
|
||||||
|
"""Fetch stream source from camera entity."""
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.camera.Camera.stream_source",
|
||||||
|
return_value="rtsp://127.0.0.1/stream",
|
||||||
|
) as mock_camera_stream_source:
|
||||||
|
stream_source = await camera.async_get_stream_source(hass, "camera.demo_camera")
|
||||||
|
|
||||||
|
assert mock_camera_stream_source.called
|
||||||
|
assert stream_source == "rtsp://127.0.0.1/stream"
|
||||||
|
|
||||||
|
|
||||||
async def test_get_image_without_exists_camera(hass, image_mock_url):
|
async def test_get_image_without_exists_camera(hass, image_mock_url):
|
||||||
"""Try to get image without exists camera."""
|
"""Try to get image without exists camera."""
|
||||||
with patch(
|
with patch(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user