Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -12,8 +12,14 @@ from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.loader import bind_hass
|
||||
|
||||
from .const import (
|
||||
DOMAIN, ATTR_STREAMS, ATTR_ENDPOINTS, CONF_STREAM_SOURCE,
|
||||
CONF_DURATION, CONF_LOOKBACK, SERVICE_RECORD)
|
||||
DOMAIN,
|
||||
ATTR_STREAMS,
|
||||
ATTR_ENDPOINTS,
|
||||
CONF_STREAM_SOURCE,
|
||||
CONF_DURATION,
|
||||
CONF_LOOKBACK,
|
||||
SERVICE_RECORD,
|
||||
)
|
||||
from .core import PROVIDERS
|
||||
from .worker import stream_worker
|
||||
from .hls import async_setup_hls
|
||||
@@ -21,27 +27,24 @@ from .recorder import async_setup_recorder
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
STREAM_SERVICE_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_STREAM_SOURCE): cv.string,
|
||||
})
|
||||
STREAM_SERVICE_SCHEMA = vol.Schema({vol.Required(CONF_STREAM_SOURCE): cv.string})
|
||||
|
||||
SERVICE_RECORD_SCHEMA = STREAM_SERVICE_SCHEMA.extend({
|
||||
vol.Required(CONF_FILENAME): cv.string,
|
||||
vol.Optional(CONF_DURATION, default=30): int,
|
||||
vol.Optional(CONF_LOOKBACK, default=0): int,
|
||||
})
|
||||
SERVICE_RECORD_SCHEMA = STREAM_SERVICE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_FILENAME): cv.string,
|
||||
vol.Optional(CONF_DURATION, default=30): int,
|
||||
vol.Optional(CONF_LOOKBACK, default=0): int,
|
||||
}
|
||||
)
|
||||
|
||||
# Set log level to error for libav
|
||||
logging.getLogger('libav').setLevel(logging.ERROR)
|
||||
logging.getLogger("libav").setLevel(logging.ERROR)
|
||||
|
||||
|
||||
@bind_hass
|
||||
def request_stream(hass, stream_source, *, fmt='hls',
|
||||
keepalive=False, options=None):
|
||||
def request_stream(hass, stream_source, *, fmt="hls", keepalive=False, options=None):
|
||||
"""Set up stream with token."""
|
||||
if DOMAIN not in hass.config.components:
|
||||
raise HomeAssistantError("Stream integration is not set up.")
|
||||
@@ -50,17 +53,19 @@ def request_stream(hass, stream_source, *, fmt='hls',
|
||||
options = {}
|
||||
|
||||
# For RTSP streams, prefer TCP
|
||||
if isinstance(stream_source, str) \
|
||||
and stream_source[:7] == 'rtsp://' and not options:
|
||||
options['rtsp_flags'] = 'prefer_tcp'
|
||||
options['stimeout'] = '5000000'
|
||||
if (
|
||||
isinstance(stream_source, str)
|
||||
and stream_source[:7] == "rtsp://"
|
||||
and not options
|
||||
):
|
||||
options["rtsp_flags"] = "prefer_tcp"
|
||||
options["stimeout"] = "5000000"
|
||||
|
||||
try:
|
||||
streams = hass.data[DOMAIN][ATTR_STREAMS]
|
||||
stream = streams.get(stream_source)
|
||||
if not stream:
|
||||
stream = Stream(hass, stream_source,
|
||||
options=options, keepalive=keepalive)
|
||||
stream = Stream(hass, stream_source, options=options, keepalive=keepalive)
|
||||
streams[stream_source] = stream
|
||||
else:
|
||||
# Update keepalive option on existing stream
|
||||
@@ -72,10 +77,9 @@ def request_stream(hass, stream_source, *, fmt='hls',
|
||||
if not stream.access_token:
|
||||
stream.access_token = generate_secret()
|
||||
stream.start()
|
||||
return hass.data[DOMAIN][ATTR_ENDPOINTS][fmt].format(
|
||||
stream.access_token)
|
||||
return hass.data[DOMAIN][ATTR_ENDPOINTS][fmt].format(stream.access_token)
|
||||
except Exception:
|
||||
raise HomeAssistantError('Unable to get stream')
|
||||
raise HomeAssistantError("Unable to get stream")
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
@@ -86,7 +90,7 @@ async def async_setup(hass, config):
|
||||
|
||||
# Setup HLS
|
||||
hls_endpoint = async_setup_hls(hass)
|
||||
hass.data[DOMAIN][ATTR_ENDPOINTS]['hls'] = hls_endpoint
|
||||
hass.data[DOMAIN][ATTR_ENDPOINTS]["hls"] = hls_endpoint
|
||||
|
||||
# Setup Recorder
|
||||
async_setup_recorder(hass)
|
||||
@@ -105,8 +109,9 @@ async def async_setup(hass, config):
|
||||
"""Call record stream service handler."""
|
||||
await async_handle_record_service(hass, call)
|
||||
|
||||
hass.services.async_register(DOMAIN, SERVICE_RECORD,
|
||||
async_record, schema=SERVICE_RECORD_SCHEMA)
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_RECORD, async_record, schema=SERVICE_RECORD_SCHEMA
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
@@ -159,10 +164,10 @@ class Stream:
|
||||
if self._thread is None or not self._thread.isAlive():
|
||||
self._thread_quit = threading.Event()
|
||||
self._thread = threading.Thread(
|
||||
name='stream_worker',
|
||||
name="stream_worker",
|
||||
target=stream_worker,
|
||||
args=(
|
||||
self.hass, self, self._thread_quit))
|
||||
args=(self.hass, self, self._thread_quit),
|
||||
)
|
||||
self._thread.start()
|
||||
_LOGGER.info("Started stream: %s", self.source)
|
||||
|
||||
@@ -192,8 +197,9 @@ async def async_handle_record_service(hass, call):
|
||||
|
||||
# Check for file access
|
||||
if not hass.config.is_allowed_path(video_path):
|
||||
raise HomeAssistantError("Can't write {}, no access to path!"
|
||||
.format(video_path))
|
||||
raise HomeAssistantError(
|
||||
"Can't write {}, no access to path!".format(video_path)
|
||||
)
|
||||
|
||||
# Check for active stream
|
||||
streams = hass.data[DOMAIN][ATTR_STREAMS]
|
||||
@@ -203,22 +209,22 @@ async def async_handle_record_service(hass, call):
|
||||
streams[stream_source] = stream
|
||||
|
||||
# Add recorder
|
||||
recorder = stream.outputs.get('recorder')
|
||||
recorder = stream.outputs.get("recorder")
|
||||
if recorder:
|
||||
raise HomeAssistantError("Stream already recording to {}!"
|
||||
.format(recorder.video_path))
|
||||
raise HomeAssistantError(
|
||||
"Stream already recording to {}!".format(recorder.video_path)
|
||||
)
|
||||
|
||||
recorder = stream.add_provider('recorder')
|
||||
recorder = stream.add_provider("recorder")
|
||||
recorder.video_path = video_path
|
||||
recorder.timeout = duration
|
||||
|
||||
stream.start()
|
||||
|
||||
# Take advantage of lookback
|
||||
hls = stream.outputs.get('hls')
|
||||
hls = stream.outputs.get("hls")
|
||||
if lookback > 0 and hls:
|
||||
num_segments = min(int(lookback // hls.target_duration),
|
||||
hls.num_segments)
|
||||
num_segments = min(int(lookback // hls.target_duration), hls.num_segments)
|
||||
# Wait for latest segment, then add the lookback
|
||||
await hls.recv()
|
||||
recorder.prepend(list(hls.get_segment())[-num_segments:])
|
||||
Reference in new issue
Block a user