Move ffmpeg to dispatcher from hass.data entity store. (#6211)

* Move ffmpeg to dispatcher from hass.data entity store.

* fix lint

* address paulus comments

* add more unittest for better coverage
This commit is contained in:
Pascal Vizeli authored and Paulus Schoutsen committed 2017-02-26 14:31:46 -08:00
1 parent 9490cb4c8f
commit 48cf7a4af9
5 files changed
+252 -206

No files matched your search

+76 -68
View File
@@ -14,6 +14,8 @@ from homeassistant.core import callback
from homeassistant.const import (
ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
from homeassistant.config import load_yaml_config_file
from homeassistant.helpers.dispatcher import (
async_dispatcher_send, async_dispatcher_connect)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
@@ -26,6 +28,10 @@ SERVICE_START = 'start'
SERVICE_STOP = 'stop'
SERVICE_RESTART = 'restart'
SIGNAL_FFMPEG_START = 'ffmpeg.start'
SIGNAL_FFMPEG_STOP = 'ffmpeg.stop'
SIGNAL_FFMPEG_RESTART = 'ffmpeg.restart'
DATA_FFMPEG = 'ffmpeg'
CONF_INITIAL_STATE = 'initial_state'
@@ -50,22 +56,25 @@ SERVICE_FFMPEG_SCHEMA = vol.Schema({
})
def start(hass, entity_id=None):
@callback
def async_start(hass, entity_id=None):
"""Start a ffmpeg process on entity."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_START, data)
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_START, data))
def stop(hass, entity_id=None):
@callback
def async_stop(hass, entity_id=None):
"""Stop a ffmpeg process on entity."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_STOP, data)
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_STOP, data))
def restart(hass, entity_id=None):
@callback
def async_restart(hass, entity_id=None):
"""Restart a ffmpeg process on entity."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_RESTART, data)
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_RESTART, data))
@asyncio.coroutine
@@ -89,30 +98,12 @@ def async_setup(hass, config):
"""Handle service ffmpeg process."""
entity_ids = service.data.get(ATTR_ENTITY_ID)
if entity_ids:
devices = [device for device in manager.entities
if device.entity_id in entity_ids]
if service.service == SERVICE_START:
async_dispatcher_send(hass, SIGNAL_FFMPEG_START, entity_ids)
elif service.service == SERVICE_STOP:
async_dispatcher_send(hass, SIGNAL_FFMPEG_STOP, entity_ids)
else:
devices = manager.entities
tasks = []
for device in devices:
if service.service == SERVICE_START:
tasks.append(device.async_start_ffmpeg())
elif service.service == SERVICE_STOP:
tasks.append(device.async_stop_ffmpeg())
else:
tasks.append(device.async_restart_ffmpeg())
if tasks:
yield from asyncio.wait(tasks, loop=hass.loop)
tasks.clear()
for device in devices:
tasks.append(device.async_update_ha_state())
if tasks:
yield from asyncio.wait(tasks, loop=hass.loop)
async_dispatcher_send(hass, SIGNAL_FFMPEG_RESTART, entity_ids)
hass.services.async_register(
DOMAIN, SERVICE_START, async_service_handle,
@@ -140,42 +131,12 @@ class FFmpegManager(object):
self._cache = {}
self._bin = ffmpeg_bin
self._run_test = run_test
self._entities = []
@property
def binary(self):
"""Return ffmpeg binary from config."""
return self._bin
@property
def entities(self):
"""Return ffmpeg entities for services."""
return self._entities
@callback
def async_register_device(self, device):
"""Register a ffmpeg process/device."""
self._entities.append(device)
@asyncio.coroutine
def async_shutdown(event):
"""Stop ffmpeg process."""
yield from device.async_stop_ffmpeg()
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, async_shutdown)
# start on startup
if device.initial_state:
@asyncio.coroutine
def async_start(event):
"""Start ffmpeg process."""
yield from device.async_start_ffmpeg()
yield from device.async_update_ha_state()
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_START, async_start)
@asyncio.coroutine
def async_run_test(self, input_source):
"""Run test on this input. TRUE is deactivate or run correct.
@@ -208,6 +169,22 @@ class FFmpegBase(Entity):
self.ffmpeg = None
self.initial_state = initial_state
@asyncio.coroutine
def async_added_to_hass(self):
"""Register dispatcher & events.
This method is a coroutine.
"""
async_dispatcher_connect(
self.hass, SIGNAL_FFMPEG_START, self._async_start_ffmpeg)
async_dispatcher_connect(
self.hass, SIGNAL_FFMPEG_STOP, self._async_stop_ffmpeg)
async_dispatcher_connect(
self.hass, SIGNAL_FFMPEG_RESTART, self._async_restart_ffmpeg)
# register start/stop
self._async_register_events()
@property
def available(self):
"""Return True if entity is available."""
@@ -218,22 +195,53 @@ class FFmpegBase(Entity):
"""Return True if entity has to be polled for state."""
return False
def async_start_ffmpeg(self):
@asyncio.coroutine
def _async_start_ffmpeg(self, entity_ids):
"""Start a ffmpeg process.
This method must be run in the event loop and returns a coroutine.
This method is a coroutine.
"""
raise NotImplementedError()
def async_stop_ffmpeg(self):
@asyncio.coroutine
def _async_stop_ffmpeg(self, entity_ids):
"""Stop a ffmpeg process.
This method must be run in the event loop and returns a coroutine.
This method is a coroutine.
"""
return self.ffmpeg.close()
if entity_ids is None or self.entity_id in entity_ids:
yield from self.ffmpeg.close()
@asyncio.coroutine
def async_restart_ffmpeg(self):
"""Stop a ffmpeg process."""
yield from self.async_stop_ffmpeg()
yield from self.async_start_ffmpeg()
def _async_restart_ffmpeg(self, entity_ids):
"""Stop a ffmpeg process.
This method is a coroutine.
"""
if entity_ids is None or self.entity_id in entity_ids:
yield from self._async_stop_ffmpeg(None)
yield from self._async_start_ffmpeg(None)
@callback
def _async_register_events(self):
"""Register a ffmpeg process/device."""
@asyncio.coroutine
def async_shutdown_handle(event):
"""Stop ffmpeg process."""
yield from self._async_stop_ffmpeg(None)
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, async_shutdown_handle)
# start on startup
if not self.initial_state:
return
@asyncio.coroutine
def async_start_handle(event):
"""Start ffmpeg process."""
yield from self._async_start_ffmpeg(None)
self.hass.async_add_job(self.async_update_ha_state())
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_START, async_start_handle)