Various camera test improvements (#34022)
This commit is contained in:
1 parent
ea9f1376a6
commit
123ae941a9
3 files changed
+90
-101
No files matched your search
@@ -3,63 +3,13 @@
|
||||
All containing methods are legacy helpers that should not be used by new
|
||||
components. Instead call the service directly.
|
||||
"""
|
||||
from homeassistant.components.camera import (
|
||||
ATTR_FILENAME,
|
||||
SERVICE_ENABLE_MOTION,
|
||||
SERVICE_SNAPSHOT,
|
||||
)
|
||||
from homeassistant.components.camera.const import (
|
||||
DATA_CAMERA_PREFS,
|
||||
DOMAIN,
|
||||
PREF_PRELOAD_STREAM,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ENTITY_MATCH_ALL,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.components.camera.const import DATA_CAMERA_PREFS, PREF_PRELOAD_STREAM
|
||||
|
||||
|
||||
@bind_hass
|
||||
async def async_turn_off(hass, entity_id=ENTITY_MATCH_ALL):
|
||||
"""Turn off camera."""
|
||||
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
|
||||
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data)
|
||||
|
||||
|
||||
@bind_hass
|
||||
async def async_turn_on(hass, entity_id=ENTITY_MATCH_ALL):
|
||||
"""Turn on camera, and set operation mode."""
|
||||
data = {}
|
||||
if entity_id is not None:
|
||||
data[ATTR_ENTITY_ID] = entity_id
|
||||
|
||||
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data)
|
||||
|
||||
|
||||
@bind_hass
|
||||
def enable_motion_detection(hass, entity_id=ENTITY_MATCH_ALL):
|
||||
"""Enable Motion Detection."""
|
||||
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
||||
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_ENABLE_MOTION, data))
|
||||
|
||||
|
||||
@bind_hass
|
||||
@callback
|
||||
def async_snapshot(hass, filename, entity_id=ENTITY_MATCH_ALL):
|
||||
"""Make a snapshot from a camera."""
|
||||
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
|
||||
data[ATTR_FILENAME] = filename
|
||||
|
||||
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_SNAPSHOT, data))
|
||||
|
||||
|
||||
def mock_camera_prefs(hass, entity_id, prefs={}):
|
||||
def mock_camera_prefs(hass, entity_id, prefs=None):
|
||||
"""Fixture for cloud component."""
|
||||
prefs_to_set = {PREF_PRELOAD_STREAM: True}
|
||||
prefs_to_set.update(prefs)
|
||||
if prefs is not None:
|
||||
prefs_to_set.update(prefs)
|
||||
hass.data[DATA_CAMERA_PREFS]._prefs[entity_id] = prefs_to_set
|
||||
return prefs_to_set
|
||||
@@ -102,8 +102,15 @@ async def test_snapshot_service(hass, mock_camera):
|
||||
with patch(
|
||||
"homeassistant.components.camera.open", mopen, create=True
|
||||
), patch.object(hass.config, "is_allowed_path", return_value=True):
|
||||
common.async_snapshot(hass, "/test/snapshot.jpg")
|
||||
await hass.async_block_till_done()
|
||||
await hass.services.async_call(
|
||||
camera.DOMAIN,
|
||||
camera.SERVICE_SNAPSHOT,
|
||||
{
|
||||
ATTR_ENTITY_ID: "camera.demo_camera",
|
||||
camera.ATTR_FILENAME: "/test/snapshot.jpg",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_write = mopen().write
|
||||
|
||||
@@ -237,10 +244,6 @@ async def test_play_stream_service_no_source(hass, mock_camera, mock_stream):
|
||||
async def test_handle_play_stream_service(hass, mock_camera, mock_stream):
|
||||
"""Test camera play_stream service."""
|
||||
await async_setup_component(hass, "media_player", {})
|
||||
data = {
|
||||
ATTR_ENTITY_ID: "camera.demo_camera",
|
||||
camera.ATTR_MEDIA_PLAYER: "media_player.test",
|
||||
}
|
||||
with patch(
|
||||
"homeassistant.components.camera.request_stream"
|
||||
) as mock_request_stream, patch(
|
||||
@@ -249,7 +252,13 @@ async def test_handle_play_stream_service(hass, mock_camera, mock_stream):
|
||||
):
|
||||
# Call service
|
||||
await hass.services.async_call(
|
||||
camera.DOMAIN, camera.SERVICE_PLAY_STREAM, data, blocking=True
|
||||
camera.DOMAIN,
|
||||
camera.SERVICE_PLAY_STREAM,
|
||||
{
|
||||
ATTR_ENTITY_ID: "camera.demo_camera",
|
||||
camera.ATTR_MEDIA_PLAYER: "media_player.test",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
# So long as we request the stream, the rest should be covered
|
||||
# by the play_media service tests.
|
||||
@@ -295,23 +304,23 @@ async def test_preload_stream(hass, mock_stream):
|
||||
|
||||
async def test_record_service_invalid_path(hass, mock_camera):
|
||||
"""Test record service with invalid path."""
|
||||
data = {
|
||||
ATTR_ENTITY_ID: "camera.demo_camera",
|
||||
camera.CONF_FILENAME: "/my/invalid/path",
|
||||
}
|
||||
with patch.object(
|
||||
hass.config, "is_allowed_path", return_value=False
|
||||
), pytest.raises(HomeAssistantError):
|
||||
# Call service
|
||||
await hass.services.async_call(
|
||||
camera.DOMAIN, camera.SERVICE_RECORD, data, blocking=True
|
||||
camera.DOMAIN,
|
||||
camera.SERVICE_RECORD,
|
||||
{
|
||||
ATTR_ENTITY_ID: "camera.demo_camera",
|
||||
camera.CONF_FILENAME: "/my/invalid/path",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_record_service(hass, mock_camera, mock_stream):
|
||||
"""Test record service."""
|
||||
data = {ATTR_ENTITY_ID: "camera.demo_camera", camera.CONF_FILENAME: "/my/path"}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.demo.camera.DemoCamera.stream_source",
|
||||
return_value=mock_coro("http://example.com"),
|
||||
@@ -323,7 +332,10 @@ async def test_record_service(hass, mock_camera, mock_stream):
|
||||
):
|
||||
# Call service
|
||||
await hass.services.async_call(
|
||||
camera.DOMAIN, camera.SERVICE_RECORD, data, blocking=True
|
||||
camera.DOMAIN,
|
||||
camera.SERVICE_RECORD,
|
||||
{ATTR_ENTITY_ID: "camera.demo_camera", camera.CONF_FILENAME: "/my/path"},
|
||||
blocking=True,
|
||||
)
|
||||
# So long as we call stream.record, the rest should be covered
|
||||
# by those tests.
|
||||
|
||||
Reference in new issue
Block a user