Skip to content

Commit

Permalink
Remove FFmpeg input tests (#18131)
Browse files Browse the repository at this point in the history
* Remove FFmpeg input tests

* Not needed here

* Removing tests for removed functionality

* Minor lint

* Fix tests to reflect removed config option

* Remove async service registration by request

* More lint

* Unused imports

* Make it a non-breaking change

* Update ffmpeg.py
  • Loading branch information
jjlawren authored and balloob committed Nov 3, 2018
1 parent 782a90a commit 9807ba1
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 127 deletions.
4 changes: 0 additions & 4 deletions homeassistant/components/binary_sensor/ffmpeg_motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the FFmpeg binary motion sensor."""
manager = hass.data[DATA_FFMPEG]

if not await manager.async_run_test(config.get(CONF_INPUT)):
return

entity = FFmpegMotion(hass, manager, config)
async_add_entities([entity])

Expand Down
4 changes: 0 additions & 4 deletions homeassistant/components/binary_sensor/ffmpeg_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the FFmpeg noise binary sensor."""
manager = hass.data[DATA_FFMPEG]

if not await manager.async_run_test(config.get(CONF_INPUT)):
return

entity = FFmpegNoise(hass, manager, config)
async_add_entities([entity])

Expand Down
2 changes: 0 additions & 2 deletions homeassistant/components/camera/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up a FFmpeg camera."""
if not await hass.data[DATA_FFMPEG].async_run_test(config.get(CONF_INPUT)):
return
async_add_entities([FFmpegCamera(hass, config)])


Expand Down
7 changes: 2 additions & 5 deletions homeassistant/components/camera/onvif.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@

def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up a ONVIF camera."""
if not hass.data[DATA_FFMPEG].async_run_test(config.get(CONF_HOST)):
return

def handle_ptz(service):
"""Handle PTZ service call."""
pan = service.data.get(ATTR_PAN, None)
Expand All @@ -93,8 +90,8 @@ def handle_ptz(service):
for camera in target_cameras:
camera.perform_ptz(pan, tilt, zoom)

hass.services.async_register(DOMAIN, SERVICE_PTZ, handle_ptz,
schema=SERVICE_PTZ_SCHEMA)
hass.services.register(DOMAIN, SERVICE_PTZ, handle_ptz,
schema=SERVICE_PTZ_SCHEMA)
add_entities([ONVIFHassCamera(hass, config)])


Expand Down
31 changes: 3 additions & 28 deletions homeassistant/components/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@
CONF_RUN_TEST = 'run_test'

DEFAULT_BINARY = 'ffmpeg'
DEFAULT_RUN_TEST = True

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Optional(CONF_FFMPEG_BIN, default=DEFAULT_BINARY): cv.string,
vol.Optional(CONF_RUN_TEST, default=DEFAULT_RUN_TEST): cv.boolean,
vol.Optional(CONF_RUN_TEST): cv.boolean,
}),
}, extra=vol.ALLOW_EXTRA)

Expand All @@ -60,8 +59,7 @@ async def async_setup(hass, config):

manager = FFmpegManager(
hass,
conf.get(CONF_FFMPEG_BIN, DEFAULT_BINARY),
conf.get(CONF_RUN_TEST, DEFAULT_RUN_TEST)
conf.get(CONF_FFMPEG_BIN, DEFAULT_BINARY)
)

# Register service
Expand Down Expand Up @@ -95,40 +93,17 @@ async def async_service_handle(service):
class FFmpegManager:
"""Helper for ha-ffmpeg."""

def __init__(self, hass, ffmpeg_bin, run_test):
def __init__(self, hass, ffmpeg_bin):
"""Initialize helper."""
self.hass = hass
self._cache = {}
self._bin = ffmpeg_bin
self._run_test = run_test

@property
def binary(self):
"""Return ffmpeg binary from config."""
return self._bin

async def async_run_test(self, input_source):
"""Run test on this input. TRUE is deactivate or run correct.
This method must be run in the event loop.
"""
from haffmpeg import Test

if self._run_test:
# if in cache
if input_source in self._cache:
return self._cache[input_source]

# run test
ffmpeg_test = Test(self.binary, loop=self.hass.loop)
success = await ffmpeg_test.run_test(input_source)
if not success:
_LOGGER.error("FFmpeg '%s' test fails!", input_source)
self._cache[input_source] = False
return False
self._cache[input_source] = True
return True


class FFmpegBase(Entity):
"""Interface object for FFmpeg."""
Expand Down
6 changes: 0 additions & 6 deletions tests/components/binary_sensor/test_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ def setup_method(self):
self.hass = get_test_home_assistant()

self.config = {
'ffmpeg': {
'run_test': False,
},
'binary_sensor': {
'platform': 'ffmpeg_noise',
'input': 'testinputvideo',
Expand Down Expand Up @@ -80,9 +77,6 @@ def setup_method(self):
self.hass = get_test_home_assistant()

self.config = {
'ffmpeg': {
'run_test': False,
},
'binary_sensor': {
'platform': 'ffmpeg_motion',
'input': 'testinputvideo',
Expand Down
88 changes: 10 additions & 78 deletions tests/components/test_ffmpeg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""The tests for Home Assistant ffmpeg."""
import asyncio
from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock

import homeassistant.components.ffmpeg as ffmpeg
from homeassistant.components.ffmpeg import (
Expand All @@ -10,7 +10,7 @@
from homeassistant.setup import setup_component, async_setup_component

from tests.common import (
get_test_home_assistant, assert_setup_component, mock_coro)
get_test_home_assistant, assert_setup_component)


@callback
Expand Down Expand Up @@ -85,14 +85,14 @@ def teardown_method(self):

def test_setup_component(self):
"""Set up ffmpeg component."""
with assert_setup_component(2):
with assert_setup_component(1):
setup_component(self.hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

assert self.hass.data[ffmpeg.DATA_FFMPEG].binary == 'ffmpeg'

def test_setup_component_test_service(self):
"""Set up ffmpeg component test services."""
with assert_setup_component(2):
with assert_setup_component(1):
setup_component(self.hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

assert self.hass.services.has_service(ffmpeg.DOMAIN, 'start')
Expand All @@ -103,7 +103,7 @@ def test_setup_component_test_service(self):
@asyncio.coroutine
def test_setup_component_test_register(hass):
"""Set up ffmpeg component test register."""
with assert_setup_component(2):
with assert_setup_component(1):
yield from async_setup_component(
hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

Expand All @@ -118,7 +118,7 @@ def test_setup_component_test_register(hass):
@asyncio.coroutine
def test_setup_component_test_register_no_startup(hass):
"""Set up ffmpeg component test register without startup."""
with assert_setup_component(2):
with assert_setup_component(1):
yield from async_setup_component(
hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

Expand All @@ -133,7 +133,7 @@ def test_setup_component_test_register_no_startup(hass):
@asyncio.coroutine
def test_setup_component_test_service_start(hass):
"""Set up ffmpeg component test service start."""
with assert_setup_component(2):
with assert_setup_component(1):
yield from async_setup_component(
hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

Expand All @@ -149,7 +149,7 @@ def test_setup_component_test_service_start(hass):
@asyncio.coroutine
def test_setup_component_test_service_stop(hass):
"""Set up ffmpeg component test service stop."""
with assert_setup_component(2):
with assert_setup_component(1):
yield from async_setup_component(
hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

Expand All @@ -165,7 +165,7 @@ def test_setup_component_test_service_stop(hass):
@asyncio.coroutine
def test_setup_component_test_service_restart(hass):
"""Set up ffmpeg component test service restart."""
with assert_setup_component(2):
with assert_setup_component(1):
yield from async_setup_component(
hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

Expand All @@ -182,7 +182,7 @@ def test_setup_component_test_service_restart(hass):
@asyncio.coroutine
def test_setup_component_test_service_start_with_entity(hass):
"""Set up ffmpeg component test service start."""
with assert_setup_component(2):
with assert_setup_component(1):
yield from async_setup_component(
hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

Expand All @@ -194,71 +194,3 @@ def test_setup_component_test_service_start_with_entity(hass):

assert ffmpeg_dev.called_start
assert ffmpeg_dev.called_entities == ['test.ffmpeg_device']


@asyncio.coroutine
def test_setup_component_test_run_test_false(hass):
"""Set up ffmpeg component test run_test false."""
with assert_setup_component(2):
yield from async_setup_component(
hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {
'run_test': False,
}})

manager = hass.data[ffmpeg.DATA_FFMPEG]
with patch('haffmpeg.Test.run_test', return_value=mock_coro(False)):
yield from manager.async_run_test("blabalblabla")

assert len(manager._cache) == 0


@asyncio.coroutine
def test_setup_component_test_run_test(hass):
"""Set up ffmpeg component test run_test."""
with assert_setup_component(2):
yield from async_setup_component(
hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

manager = hass.data[ffmpeg.DATA_FFMPEG]

with patch('haffmpeg.Test.run_test', return_value=mock_coro(True)) \
as mock_test:
yield from manager.async_run_test("blabalblabla")

assert mock_test.called
assert mock_test.call_count == 1
assert len(manager._cache) == 1
assert manager._cache['blabalblabla']

yield from manager.async_run_test("blabalblabla")

assert mock_test.called
assert mock_test.call_count == 1
assert len(manager._cache) == 1
assert manager._cache['blabalblabla']


@asyncio.coroutine
def test_setup_component_test_run_test_test_fail(hass):
"""Set up ffmpeg component test run_test."""
with assert_setup_component(2):
yield from async_setup_component(
hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

manager = hass.data[ffmpeg.DATA_FFMPEG]

with patch('haffmpeg.Test.run_test', return_value=mock_coro(False)) \
as mock_test:
yield from manager.async_run_test("blabalblabla")

assert mock_test.called
assert mock_test.call_count == 1
assert len(manager._cache) == 1
assert not manager._cache['blabalblabla']

yield from manager.async_run_test("blabalblabla")

assert mock_test.called
assert mock_test.call_count == 1
assert len(manager._cache) == 1
assert not manager._cache['blabalblabla']

0 comments on commit 9807ba1

Please sign in to comment.