Camera Preferences + Preload Stream (#22339)

* initial commit for camera preferences and preload stream

* cleanup and add tests

* respect camera preferences on each request stream call

* return the new prefs after update
This commit is contained in:
Jason Hunter authored and GitHub committed 2019-03-26 08:31:29 -04:00
1 parent baa4945944
commit bad0a8b342
11 files changed
+252 -22

No files matched your search

+61 -7
View File
@@ -20,7 +20,7 @@ import voluptuous as vol
from homeassistant.core import callback
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, \
SERVICE_TURN_ON
SERVICE_TURN_ON, EVENT_HOMEASSISTANT_START
from homeassistant.exceptions import HomeAssistantError
from homeassistant.loader import bind_hass
from homeassistant.helpers.entity import Entity
@@ -37,7 +37,9 @@ from homeassistant.components.stream.const import (
from homeassistant.components import websocket_api
import homeassistant.helpers.config_validation as cv
DOMAIN = 'camera'
from .const import DOMAIN, DATA_CAMERA_PREFS
from .prefs import CameraPreferences
DEPENDENCIES = ['http']
_LOGGER = logging.getLogger(__name__)
@@ -68,7 +70,6 @@ ENTITY_IMAGE_URL = '/api/camera_proxy/{0}?token={1}'
TOKEN_CHANGE_INTERVAL = timedelta(minutes=5)
_RND = SystemRandom()
FALLBACK_STREAM_INTERVAL = 1 # seconds
MIN_STREAM_INTERVAL = 0.5 # seconds
CAMERA_SERVICE_SCHEMA = vol.Schema({
@@ -103,12 +104,14 @@ class Image:
async def async_request_stream(hass, entity_id, fmt):
"""Request a stream for a camera entity."""
camera = _get_camera_from_entity_id(hass, entity_id)
camera_prefs = hass.data[DATA_CAMERA_PREFS].get(entity_id)
if not camera.stream_source:
raise HomeAssistantError("{} does not support play stream service"
.format(camera.entity_id))
return request_stream(hass, camera.stream_source, fmt=fmt)
return request_stream(hass, camera.stream_source, fmt=fmt,
keepalive=camera_prefs.preload_stream)
@bind_hass
@@ -197,6 +200,10 @@ async def async_setup(hass, config):
component = hass.data[DOMAIN] = \
EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)
prefs = CameraPreferences(hass)
await prefs.async_initialize()
hass.data[DATA_CAMERA_PREFS] = prefs
hass.http.register_view(CameraImageView(component))
hass.http.register_view(CameraMjpegStream(component))
hass.components.websocket_api.async_register_command(
@@ -204,9 +211,21 @@ async def async_setup(hass, config):
SCHEMA_WS_CAMERA_THUMBNAIL
)
hass.components.websocket_api.async_register_command(ws_camera_stream)
hass.components.websocket_api.async_register_command(websocket_get_prefs)
hass.components.websocket_api.async_register_command(
websocket_update_prefs)
await component.async_setup(config)
@callback
def preload_stream(event):
for camera in component.entities:
camera_prefs = prefs.get(camera.entity_id)
if camera.stream_source and camera_prefs.preload_stream:
request_stream(hass, camera.stream_source, keepalive=True)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, preload_stream)
@callback
def update_tokens(time):
"""Update tokens of the entities."""
@@ -522,14 +541,17 @@ async def ws_camera_stream(hass, connection, msg):
Async friendly.
"""
try:
camera = _get_camera_from_entity_id(hass, msg['entity_id'])
entity_id = msg['entity_id']
camera = _get_camera_from_entity_id(hass, entity_id)
camera_prefs = hass.data[DATA_CAMERA_PREFS].get(entity_id)
if not camera.stream_source:
raise HomeAssistantError("{} does not support play stream service"
.format(camera.entity_id))
fmt = msg['format']
url = request_stream(hass, camera.stream_source, fmt=fmt)
url = request_stream(hass, camera.stream_source, fmt=fmt,
keepalive=camera_prefs.preload_stream)
connection.send_result(msg['id'], {'url': url})
except HomeAssistantError as ex:
_LOGGER.error(ex)
@@ -537,6 +559,36 @@ async def ws_camera_stream(hass, connection, msg):
msg['id'], 'start_stream_failed', str(ex))
@websocket_api.async_response
@websocket_api.websocket_command({
vol.Required('type'): 'camera/get_prefs',
vol.Required('entity_id'): cv.entity_id,
})
async def websocket_get_prefs(hass, connection, msg):
"""Handle request for account info."""
prefs = hass.data[DATA_CAMERA_PREFS].get(msg['entity_id'])
connection.send_result(msg['id'], prefs.as_dict())
@websocket_api.async_response
@websocket_api.websocket_command({
vol.Required('type'): 'camera/update_prefs',
vol.Required('entity_id'): cv.entity_id,
vol.Optional('preload_stream'): bool,
})
async def websocket_update_prefs(hass, connection, msg):
"""Handle request for account info."""
prefs = hass.data[DATA_CAMERA_PREFS]
changes = dict(msg)
changes.pop('id')
changes.pop('type')
entity_id = changes.pop('entity_id')
await prefs.async_update(entity_id, **changes)
connection.send_result(msg['id'], prefs.get(entity_id).as_dict())
async def async_handle_snapshot_service(camera, service):
"""Handle snapshot services calls."""
hass = camera.hass
@@ -573,10 +625,12 @@ async def async_handle_play_stream_service(camera, service_call):
.format(camera.entity_id))
hass = camera.hass
camera_prefs = hass.data[DATA_CAMERA_PREFS].get(camera.entity_id)
fmt = service_call.data[ATTR_FORMAT]
entity_ids = service_call.data[ATTR_MEDIA_PLAYER]
url = request_stream(hass, camera.stream_source, fmt=fmt)
url = request_stream(hass, camera.stream_source, fmt=fmt,
keepalive=camera_prefs.preload_stream)
data = {
ATTR_ENTITY_ID: entity_ids,
ATTR_MEDIA_CONTENT_ID: "{}{}".format(hass.config.api.base_url, url),