Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -14,22 +14,37 @@ import async_timeout
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, \
|
||||
SERVICE_TURN_ON, CONF_FILENAME
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
CONF_FILENAME,
|
||||
)
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.config_validation import ( # noqa
|
||||
PLATFORM_SCHEMA, PLATFORM_SCHEMA_BASE)
|
||||
PLATFORM_SCHEMA,
|
||||
PLATFORM_SCHEMA_BASE,
|
||||
)
|
||||
from homeassistant.components.http import HomeAssistantView, KEY_AUTHENTICATED
|
||||
from homeassistant.components.media_player.const import (
|
||||
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_TYPE,
|
||||
SERVICE_PLAY_MEDIA, DOMAIN as DOMAIN_MP)
|
||||
ATTR_MEDIA_CONTENT_ID,
|
||||
ATTR_MEDIA_CONTENT_TYPE,
|
||||
SERVICE_PLAY_MEDIA,
|
||||
DOMAIN as DOMAIN_MP,
|
||||
)
|
||||
from homeassistant.components.stream import request_stream
|
||||
from homeassistant.components.stream.const import (
|
||||
OUTPUT_FORMATS, FORMAT_CONTENT_TYPE, CONF_STREAM_SOURCE, CONF_LOOKBACK,
|
||||
CONF_DURATION, SERVICE_RECORD, DOMAIN as DOMAIN_STREAM)
|
||||
OUTPUT_FORMATS,
|
||||
FORMAT_CONTENT_TYPE,
|
||||
CONF_STREAM_SOURCE,
|
||||
CONF_LOOKBACK,
|
||||
CONF_DURATION,
|
||||
SERVICE_RECORD,
|
||||
DOMAIN as DOMAIN_STREAM,
|
||||
)
|
||||
from homeassistant.components import websocket_api
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.setup import async_when_setup
|
||||
@@ -39,58 +54,62 @@ from .prefs import CameraPreferences
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SERVICE_ENABLE_MOTION = 'enable_motion_detection'
|
||||
SERVICE_DISABLE_MOTION = 'disable_motion_detection'
|
||||
SERVICE_SNAPSHOT = 'snapshot'
|
||||
SERVICE_PLAY_STREAM = 'play_stream'
|
||||
SERVICE_ENABLE_MOTION = "enable_motion_detection"
|
||||
SERVICE_DISABLE_MOTION = "disable_motion_detection"
|
||||
SERVICE_SNAPSHOT = "snapshot"
|
||||
SERVICE_PLAY_STREAM = "play_stream"
|
||||
|
||||
SCAN_INTERVAL = timedelta(seconds=30)
|
||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
||||
|
||||
ATTR_FILENAME = 'filename'
|
||||
ATTR_MEDIA_PLAYER = 'media_player'
|
||||
ATTR_FORMAT = 'format'
|
||||
ATTR_FILENAME = "filename"
|
||||
ATTR_MEDIA_PLAYER = "media_player"
|
||||
ATTR_FORMAT = "format"
|
||||
|
||||
STATE_RECORDING = 'recording'
|
||||
STATE_STREAMING = 'streaming'
|
||||
STATE_IDLE = 'idle'
|
||||
STATE_RECORDING = "recording"
|
||||
STATE_STREAMING = "streaming"
|
||||
STATE_IDLE = "idle"
|
||||
|
||||
# Bitfield of features supported by the camera entity
|
||||
SUPPORT_ON_OFF = 1
|
||||
SUPPORT_STREAM = 2
|
||||
|
||||
DEFAULT_CONTENT_TYPE = 'image/jpeg'
|
||||
ENTITY_IMAGE_URL = '/api/camera_proxy/{0}?token={1}'
|
||||
DEFAULT_CONTENT_TYPE = "image/jpeg"
|
||||
ENTITY_IMAGE_URL = "/api/camera_proxy/{0}?token={1}"
|
||||
|
||||
TOKEN_CHANGE_INTERVAL = timedelta(minutes=5)
|
||||
_RND = SystemRandom()
|
||||
|
||||
MIN_STREAM_INTERVAL = 0.5 # seconds
|
||||
|
||||
CAMERA_SERVICE_SCHEMA = vol.Schema({
|
||||
vol.Optional(ATTR_ENTITY_ID): cv.comp_entity_ids,
|
||||
})
|
||||
CAMERA_SERVICE_SCHEMA = vol.Schema({vol.Optional(ATTR_ENTITY_ID): cv.comp_entity_ids})
|
||||
|
||||
CAMERA_SERVICE_SNAPSHOT = CAMERA_SERVICE_SCHEMA.extend({
|
||||
vol.Required(ATTR_FILENAME): cv.template
|
||||
})
|
||||
CAMERA_SERVICE_SNAPSHOT = CAMERA_SERVICE_SCHEMA.extend(
|
||||
{vol.Required(ATTR_FILENAME): cv.template}
|
||||
)
|
||||
|
||||
CAMERA_SERVICE_PLAY_STREAM = CAMERA_SERVICE_SCHEMA.extend({
|
||||
vol.Required(ATTR_MEDIA_PLAYER): cv.entities_domain(DOMAIN_MP),
|
||||
vol.Optional(ATTR_FORMAT, default='hls'): vol.In(OUTPUT_FORMATS),
|
||||
})
|
||||
CAMERA_SERVICE_PLAY_STREAM = CAMERA_SERVICE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(ATTR_MEDIA_PLAYER): cv.entities_domain(DOMAIN_MP),
|
||||
vol.Optional(ATTR_FORMAT, default="hls"): vol.In(OUTPUT_FORMATS),
|
||||
}
|
||||
)
|
||||
|
||||
CAMERA_SERVICE_RECORD = CAMERA_SERVICE_SCHEMA.extend({
|
||||
vol.Required(CONF_FILENAME): cv.template,
|
||||
vol.Optional(CONF_DURATION, default=30): vol.Coerce(int),
|
||||
vol.Optional(CONF_LOOKBACK, default=0): vol.Coerce(int),
|
||||
})
|
||||
CAMERA_SERVICE_RECORD = CAMERA_SERVICE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_FILENAME): cv.template,
|
||||
vol.Optional(CONF_DURATION, default=30): vol.Coerce(int),
|
||||
vol.Optional(CONF_LOOKBACK, default=0): vol.Coerce(int),
|
||||
}
|
||||
)
|
||||
|
||||
WS_TYPE_CAMERA_THUMBNAIL = 'camera_thumbnail'
|
||||
SCHEMA_WS_CAMERA_THUMBNAIL = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
|
||||
vol.Required('type'): WS_TYPE_CAMERA_THUMBNAIL,
|
||||
vol.Required('entity_id'): cv.entity_id
|
||||
})
|
||||
WS_TYPE_CAMERA_THUMBNAIL = "camera_thumbnail"
|
||||
SCHEMA_WS_CAMERA_THUMBNAIL = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required("type"): WS_TYPE_CAMERA_THUMBNAIL,
|
||||
vol.Required("entity_id"): cv.entity_id,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@attr.s
|
||||
@@ -111,11 +130,11 @@ async def async_request_stream(hass, entity_id, fmt):
|
||||
source = await camera.stream_source()
|
||||
|
||||
if not source:
|
||||
raise HomeAssistantError("{} does not support play stream service"
|
||||
.format(camera.entity_id))
|
||||
raise HomeAssistantError(
|
||||
"{} does not support play stream service".format(camera.entity_id)
|
||||
)
|
||||
|
||||
return request_stream(hass, source, fmt=fmt,
|
||||
keepalive=camera_prefs.preload_stream)
|
||||
return request_stream(hass, source, fmt=fmt, keepalive=camera_prefs.preload_stream)
|
||||
|
||||
|
||||
@bind_hass
|
||||
@@ -130,7 +149,7 @@ async def async_get_image(hass, entity_id, timeout=10):
|
||||
if image:
|
||||
return Image(camera.content_type, image)
|
||||
|
||||
raise HomeAssistantError('Unable to get image')
|
||||
raise HomeAssistantError("Unable to get image")
|
||||
|
||||
|
||||
@bind_hass
|
||||
@@ -147,18 +166,21 @@ async def async_get_still_stream(request, image_cb, content_type, interval):
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
response = web.StreamResponse()
|
||||
response.content_type = ('multipart/x-mixed-replace; '
|
||||
'boundary=--frameboundary')
|
||||
response.content_type = "multipart/x-mixed-replace; " "boundary=--frameboundary"
|
||||
await response.prepare(request)
|
||||
|
||||
async def write_to_mjpeg_stream(img_bytes):
|
||||
"""Write image to stream."""
|
||||
await response.write(bytes(
|
||||
'--frameboundary\r\n'
|
||||
'Content-Type: {}\r\n'
|
||||
'Content-Length: {}\r\n\r\n'.format(
|
||||
content_type, len(img_bytes)),
|
||||
'utf-8') + img_bytes + b'\r\n')
|
||||
await response.write(
|
||||
bytes(
|
||||
"--frameboundary\r\n"
|
||||
"Content-Type: {}\r\n"
|
||||
"Content-Length: {}\r\n\r\n".format(content_type, len(img_bytes)),
|
||||
"utf-8",
|
||||
)
|
||||
+ img_bytes
|
||||
+ b"\r\n"
|
||||
)
|
||||
|
||||
last_image = None
|
||||
|
||||
@@ -186,23 +208,24 @@ def _get_camera_from_entity_id(hass, entity_id):
|
||||
component = hass.data.get(DOMAIN)
|
||||
|
||||
if component is None:
|
||||
raise HomeAssistantError('Camera integration not set up')
|
||||
raise HomeAssistantError("Camera integration not set up")
|
||||
|
||||
camera = component.get_entity(entity_id)
|
||||
|
||||
if camera is None:
|
||||
raise HomeAssistantError('Camera not found')
|
||||
raise HomeAssistantError("Camera not found")
|
||||
|
||||
if not camera.is_on:
|
||||
raise HomeAssistantError('Camera is off')
|
||||
raise HomeAssistantError("Camera is off")
|
||||
|
||||
return camera
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the camera component."""
|
||||
component = hass.data[DOMAIN] = \
|
||||
EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)
|
||||
component = hass.data[DOMAIN] = EntityComponent(
|
||||
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
|
||||
)
|
||||
|
||||
prefs = CameraPreferences(hass)
|
||||
await prefs.async_initialize()
|
||||
@@ -211,13 +234,11 @@ async def async_setup(hass, config):
|
||||
hass.http.register_view(CameraImageView(component))
|
||||
hass.http.register_view(CameraMjpegStream(component))
|
||||
hass.components.websocket_api.async_register_command(
|
||||
WS_TYPE_CAMERA_THUMBNAIL, websocket_camera_thumbnail,
|
||||
SCHEMA_WS_CAMERA_THUMBNAIL
|
||||
WS_TYPE_CAMERA_THUMBNAIL, websocket_camera_thumbnail, 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)
|
||||
hass.components.websocket_api.async_register_command(websocket_update_prefs)
|
||||
|
||||
await component.async_setup(config)
|
||||
|
||||
@@ -244,36 +265,30 @@ async def async_setup(hass, config):
|
||||
entity.async_update_token()
|
||||
hass.async_create_task(entity.async_update_ha_state())
|
||||
|
||||
hass.helpers.event.async_track_time_interval(
|
||||
update_tokens, TOKEN_CHANGE_INTERVAL)
|
||||
hass.helpers.event.async_track_time_interval(update_tokens, TOKEN_CHANGE_INTERVAL)
|
||||
|
||||
component.async_register_entity_service(
|
||||
SERVICE_ENABLE_MOTION, CAMERA_SERVICE_SCHEMA,
|
||||
'async_enable_motion_detection'
|
||||
SERVICE_ENABLE_MOTION, CAMERA_SERVICE_SCHEMA, "async_enable_motion_detection"
|
||||
)
|
||||
component.async_register_entity_service(
|
||||
SERVICE_DISABLE_MOTION, CAMERA_SERVICE_SCHEMA,
|
||||
'async_disable_motion_detection'
|
||||
SERVICE_DISABLE_MOTION, CAMERA_SERVICE_SCHEMA, "async_disable_motion_detection"
|
||||
)
|
||||
component.async_register_entity_service(
|
||||
SERVICE_TURN_OFF, CAMERA_SERVICE_SCHEMA,
|
||||
'async_turn_off'
|
||||
SERVICE_TURN_OFF, CAMERA_SERVICE_SCHEMA, "async_turn_off"
|
||||
)
|
||||
component.async_register_entity_service(
|
||||
SERVICE_TURN_ON, CAMERA_SERVICE_SCHEMA,
|
||||
'async_turn_on'
|
||||
SERVICE_TURN_ON, CAMERA_SERVICE_SCHEMA, "async_turn_on"
|
||||
)
|
||||
component.async_register_entity_service(
|
||||
SERVICE_SNAPSHOT, CAMERA_SERVICE_SNAPSHOT,
|
||||
async_handle_snapshot_service
|
||||
SERVICE_SNAPSHOT, CAMERA_SERVICE_SNAPSHOT, async_handle_snapshot_service
|
||||
)
|
||||
component.async_register_entity_service(
|
||||
SERVICE_PLAY_STREAM, CAMERA_SERVICE_PLAY_STREAM,
|
||||
async_handle_play_stream_service
|
||||
SERVICE_PLAY_STREAM,
|
||||
CAMERA_SERVICE_PLAY_STREAM,
|
||||
async_handle_play_stream_service,
|
||||
)
|
||||
component.async_register_entity_service(
|
||||
SERVICE_RECORD, CAMERA_SERVICE_RECORD,
|
||||
async_handle_record_service
|
||||
SERVICE_RECORD, CAMERA_SERVICE_RECORD, async_handle_record_service
|
||||
)
|
||||
|
||||
return True
|
||||
@@ -360,8 +375,9 @@ class Camera(Entity):
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
return await async_get_still_stream(request, self.async_camera_image,
|
||||
self.content_type, interval)
|
||||
return await async_get_still_stream(
|
||||
request, self.async_camera_image, self.content_type, interval
|
||||
)
|
||||
|
||||
async def handle_async_mjpeg_stream(self, request):
|
||||
"""Serve an HTTP MJPEG stream from the camera.
|
||||
@@ -370,8 +386,7 @@ class Camera(Entity):
|
||||
a direct stream from the camera.
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
return await self.handle_async_still_stream(
|
||||
request, self.frame_interval)
|
||||
return await self.handle_async_still_stream(request, self.frame_interval)
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
@@ -426,18 +441,16 @@ class Camera(Entity):
|
||||
@property
|
||||
def state_attributes(self):
|
||||
"""Return the camera state attributes."""
|
||||
attrs = {
|
||||
'access_token': self.access_tokens[-1],
|
||||
}
|
||||
attrs = {"access_token": self.access_tokens[-1]}
|
||||
|
||||
if self.model:
|
||||
attrs['model_name'] = self.model
|
||||
attrs["model_name"] = self.model
|
||||
|
||||
if self.brand:
|
||||
attrs['brand'] = self.brand
|
||||
attrs["brand"] = self.brand
|
||||
|
||||
if self.motion_detection_enabled:
|
||||
attrs['motion_detection'] = self.motion_detection_enabled
|
||||
attrs["motion_detection"] = self.motion_detection_enabled
|
||||
|
||||
return attrs
|
||||
|
||||
@@ -445,8 +458,8 @@ class Camera(Entity):
|
||||
def async_update_token(self):
|
||||
"""Update the used token."""
|
||||
self.access_tokens.append(
|
||||
hashlib.sha256(
|
||||
_RND.getrandbits(256).to_bytes(32, 'little')).hexdigest())
|
||||
hashlib.sha256(_RND.getrandbits(256).to_bytes(32, "little")).hexdigest()
|
||||
)
|
||||
|
||||
|
||||
class CameraView(HomeAssistantView):
|
||||
@@ -465,14 +478,16 @@ class CameraView(HomeAssistantView):
|
||||
if camera is None:
|
||||
raise web.HTTPNotFound()
|
||||
|
||||
authenticated = (request[KEY_AUTHENTICATED] or
|
||||
request.query.get('token') in camera.access_tokens)
|
||||
authenticated = (
|
||||
request[KEY_AUTHENTICATED]
|
||||
or request.query.get("token") in camera.access_tokens
|
||||
)
|
||||
|
||||
if not authenticated:
|
||||
raise web.HTTPUnauthorized()
|
||||
|
||||
if not camera.is_on:
|
||||
_LOGGER.debug('Camera is off.')
|
||||
_LOGGER.debug("Camera is off.")
|
||||
raise web.HTTPServiceUnavailable()
|
||||
|
||||
return await self.handle(request, camera)
|
||||
@@ -485,8 +500,8 @@ class CameraView(HomeAssistantView):
|
||||
class CameraImageView(CameraView):
|
||||
"""Camera view to serve an image."""
|
||||
|
||||
url = '/api/camera_proxy/{entity_id}'
|
||||
name = 'api:camera:image'
|
||||
url = "/api/camera_proxy/{entity_id}"
|
||||
name = "api:camera:image"
|
||||
|
||||
async def handle(self, request, camera):
|
||||
"""Serve camera image."""
|
||||
@@ -495,8 +510,7 @@ class CameraImageView(CameraView):
|
||||
image = await camera.async_camera_image()
|
||||
|
||||
if image:
|
||||
return web.Response(body=image,
|
||||
content_type=camera.content_type)
|
||||
return web.Response(body=image, content_type=camera.content_type)
|
||||
|
||||
raise web.HTTPInternalServerError()
|
||||
|
||||
@@ -504,21 +518,22 @@ class CameraImageView(CameraView):
|
||||
class CameraMjpegStream(CameraView):
|
||||
"""Camera View to serve an MJPEG stream."""
|
||||
|
||||
url = '/api/camera_proxy_stream/{entity_id}'
|
||||
name = 'api:camera:stream'
|
||||
url = "/api/camera_proxy_stream/{entity_id}"
|
||||
name = "api:camera:stream"
|
||||
|
||||
async def handle(self, request, camera):
|
||||
"""Serve camera stream, possibly with interval."""
|
||||
interval = request.query.get('interval')
|
||||
interval = request.query.get("interval")
|
||||
if interval is None:
|
||||
return await camera.handle_async_mjpeg_stream(request)
|
||||
|
||||
try:
|
||||
# Compose camera stream from stills
|
||||
interval = float(request.query.get('interval'))
|
||||
interval = float(request.query.get("interval"))
|
||||
if interval < MIN_STREAM_INTERVAL:
|
||||
raise ValueError("Stream interval must be be > {}"
|
||||
.format(MIN_STREAM_INTERVAL))
|
||||
raise ValueError(
|
||||
"Stream interval must be be > {}".format(MIN_STREAM_INTERVAL)
|
||||
)
|
||||
return await camera.handle_async_still_stream(request, interval)
|
||||
except ValueError:
|
||||
raise web.HTTPBadRequest()
|
||||
@@ -531,29 +546,37 @@ async def websocket_camera_thumbnail(hass, connection, msg):
|
||||
Async friendly.
|
||||
"""
|
||||
try:
|
||||
image = await async_get_image(hass, msg['entity_id'])
|
||||
await connection.send_big_result(msg['id'], {
|
||||
'content_type': image.content_type,
|
||||
'content': base64.b64encode(image.content).decode('utf-8')
|
||||
})
|
||||
image = await async_get_image(hass, msg["entity_id"])
|
||||
await connection.send_big_result(
|
||||
msg["id"],
|
||||
{
|
||||
"content_type": image.content_type,
|
||||
"content": base64.b64encode(image.content).decode("utf-8"),
|
||||
},
|
||||
)
|
||||
except HomeAssistantError:
|
||||
connection.send_message(websocket_api.error_message(
|
||||
msg['id'], 'image_fetch_failed', 'Unable to fetch image'))
|
||||
connection.send_message(
|
||||
websocket_api.error_message(
|
||||
msg["id"], "image_fetch_failed", "Unable to fetch image"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@websocket_api.async_response
|
||||
@websocket_api.websocket_command({
|
||||
vol.Required('type'): 'camera/stream',
|
||||
vol.Required('entity_id'): cv.entity_id,
|
||||
vol.Optional('format', default='hls'): vol.In(OUTPUT_FORMATS),
|
||||
})
|
||||
@websocket_api.websocket_command(
|
||||
{
|
||||
vol.Required("type"): "camera/stream",
|
||||
vol.Required("entity_id"): cv.entity_id,
|
||||
vol.Optional("format", default="hls"): vol.In(OUTPUT_FORMATS),
|
||||
}
|
||||
)
|
||||
async def ws_camera_stream(hass, connection, msg):
|
||||
"""Handle get camera stream websocket command.
|
||||
|
||||
Async friendly.
|
||||
"""
|
||||
try:
|
||||
entity_id = 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)
|
||||
|
||||
@@ -561,51 +584,54 @@ async def ws_camera_stream(hass, connection, msg):
|
||||
source = await camera.stream_source()
|
||||
|
||||
if not source:
|
||||
raise HomeAssistantError("{} does not support play stream service"
|
||||
.format(camera.entity_id))
|
||||
raise HomeAssistantError(
|
||||
"{} does not support play stream service".format(camera.entity_id)
|
||||
)
|
||||
|
||||
fmt = msg['format']
|
||||
url = request_stream(hass, source, fmt=fmt,
|
||||
keepalive=camera_prefs.preload_stream)
|
||||
connection.send_result(msg['id'], {'url': url})
|
||||
fmt = msg["format"]
|
||||
url = request_stream(
|
||||
hass, source, fmt=fmt, keepalive=camera_prefs.preload_stream
|
||||
)
|
||||
connection.send_result(msg["id"], {"url": url})
|
||||
except HomeAssistantError as ex:
|
||||
_LOGGER.error("Error requesting stream: %s", ex)
|
||||
connection.send_error(
|
||||
msg['id'], 'start_stream_failed', str(ex))
|
||||
connection.send_error(msg["id"], "start_stream_failed", str(ex))
|
||||
except asyncio.TimeoutError:
|
||||
_LOGGER.error("Timeout getting stream source")
|
||||
connection.send_error(
|
||||
msg['id'], 'start_stream_failed', "Timeout getting stream source")
|
||||
msg["id"], "start_stream_failed", "Timeout getting stream source"
|
||||
)
|
||||
|
||||
|
||||
@websocket_api.async_response
|
||||
@websocket_api.websocket_command({
|
||||
vol.Required('type'): 'camera/get_prefs',
|
||||
vol.Required('entity_id'): cv.entity_id,
|
||||
})
|
||||
@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())
|
||||
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,
|
||||
})
|
||||
@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')
|
||||
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())
|
||||
connection.send_result(msg["id"], prefs.get(entity_id).as_dict())
|
||||
|
||||
|
||||
async def async_handle_snapshot_service(camera, service):
|
||||
@@ -614,25 +640,22 @@ async def async_handle_snapshot_service(camera, service):
|
||||
filename = service.data[ATTR_FILENAME]
|
||||
filename.hass = hass
|
||||
|
||||
snapshot_file = filename.async_render(
|
||||
variables={ATTR_ENTITY_ID: camera})
|
||||
snapshot_file = filename.async_render(variables={ATTR_ENTITY_ID: camera})
|
||||
|
||||
# check if we allow to access to that file
|
||||
if not hass.config.is_allowed_path(snapshot_file):
|
||||
_LOGGER.error(
|
||||
"Can't write %s, no access to path!", snapshot_file)
|
||||
_LOGGER.error("Can't write %s, no access to path!", snapshot_file)
|
||||
return
|
||||
|
||||
image = await camera.async_camera_image()
|
||||
|
||||
def _write_image(to_file, image_data):
|
||||
"""Executor helper to write image."""
|
||||
with open(to_file, 'wb') as img_file:
|
||||
with open(to_file, "wb") as img_file:
|
||||
img_file.write(image_data)
|
||||
|
||||
try:
|
||||
await hass.async_add_executor_job(
|
||||
_write_image, snapshot_file, image)
|
||||
await hass.async_add_executor_job(_write_image, snapshot_file, image)
|
||||
except OSError as err:
|
||||
_LOGGER.error("Can't write image to file: %s", err)
|
||||
|
||||
@@ -643,25 +666,25 @@ async def async_handle_play_stream_service(camera, service_call):
|
||||
source = await camera.stream_source()
|
||||
|
||||
if not source:
|
||||
raise HomeAssistantError("{} does not support play stream service"
|
||||
.format(camera.entity_id))
|
||||
raise HomeAssistantError(
|
||||
"{} does not support play stream service".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, source, fmt=fmt,
|
||||
keepalive=camera_prefs.preload_stream)
|
||||
url = request_stream(hass, 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),
|
||||
ATTR_MEDIA_CONTENT_TYPE: FORMAT_CONTENT_TYPE[fmt]
|
||||
ATTR_MEDIA_CONTENT_TYPE: FORMAT_CONTENT_TYPE[fmt],
|
||||
}
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN_MP, SERVICE_PLAY_MEDIA, data,
|
||||
blocking=True, context=service_call.context)
|
||||
DOMAIN_MP, SERVICE_PLAY_MEDIA, data, blocking=True, context=service_call.context
|
||||
)
|
||||
|
||||
|
||||
async def async_handle_record_service(camera, call):
|
||||
@@ -670,14 +693,14 @@ async def async_handle_record_service(camera, call):
|
||||
source = await camera.stream_source()
|
||||
|
||||
if not source:
|
||||
raise HomeAssistantError("{} does not support record service"
|
||||
.format(camera.entity_id))
|
||||
raise HomeAssistantError(
|
||||
"{} does not support record service".format(camera.entity_id)
|
||||
)
|
||||
|
||||
hass = camera.hass
|
||||
filename = call.data[CONF_FILENAME]
|
||||
filename.hass = hass
|
||||
video_path = filename.async_render(
|
||||
variables={ATTR_ENTITY_ID: camera})
|
||||
video_path = filename.async_render(variables={ATTR_ENTITY_ID: camera})
|
||||
|
||||
data = {
|
||||
CONF_STREAM_SOURCE: source,
|
||||
@@ -687,5 +710,5 @@ async def async_handle_record_service(camera, call):
|
||||
}
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN_STREAM, SERVICE_RECORD, data,
|
||||
blocking=True, context=call.context)
|
||||
DOMAIN_STREAM, SERVICE_RECORD, data, blocking=True, context=call.context
|
||||
)
|
||||
Reference in new issue
Block a user