Migrate ffmpeg tests from coroutine to async/await (#30375)

This commit is contained in:
Franck Nijhof 2020-01-02 00:22:08 +01:00 committed by Andrew Sayre
parent c5298dc4dc
commit b43b50b6d2

View file

@ -1,5 +1,4 @@
"""The tests for Home Assistant ffmpeg.""" """The tests for Home Assistant ffmpeg."""
import asyncio
from unittest.mock import MagicMock from unittest.mock import MagicMock
import homeassistant.components.ffmpeg as ffmpeg import homeassistant.components.ffmpeg as ffmpeg
@ -61,14 +60,12 @@ class MockFFmpegDev(ffmpeg.FFmpegBase):
self.called_restart = False self.called_restart = False
self.called_entities = None self.called_entities = None
@asyncio.coroutine async def _async_start_ffmpeg(self, entity_ids):
def _async_start_ffmpeg(self, entity_ids):
"""Mock start.""" """Mock start."""
self.called_start = True self.called_start = True
self.called_entities = entity_ids self.called_entities = entity_ids
@asyncio.coroutine async def _async_stop_ffmpeg(self, entity_ids):
def _async_stop_ffmpeg(self, entity_ids):
"""Mock stop.""" """Mock stop."""
self.called_stop = True self.called_stop = True
self.called_entities = entity_ids self.called_entities = entity_ids
@ -102,91 +99,85 @@ class TestFFmpegSetup:
assert self.hass.services.has_service(ffmpeg.DOMAIN, "restart") assert self.hass.services.has_service(ffmpeg.DOMAIN, "restart")
@asyncio.coroutine async def test_setup_component_test_register(hass):
def test_setup_component_test_register(hass):
"""Set up ffmpeg component test register.""" """Set up ffmpeg component test register."""
with assert_setup_component(1): with assert_setup_component(1):
yield from async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}}) await async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})
hass.bus.async_listen_once = MagicMock() hass.bus.async_listen_once = MagicMock()
ffmpeg_dev = MockFFmpegDev(hass) ffmpeg_dev = MockFFmpegDev(hass)
yield from ffmpeg_dev.async_added_to_hass() await ffmpeg_dev.async_added_to_hass()
assert hass.bus.async_listen_once.called assert hass.bus.async_listen_once.called
assert hass.bus.async_listen_once.call_count == 2 assert hass.bus.async_listen_once.call_count == 2
@asyncio.coroutine async def test_setup_component_test_register_no_startup(hass):
def test_setup_component_test_register_no_startup(hass):
"""Set up ffmpeg component test register without startup.""" """Set up ffmpeg component test register without startup."""
with assert_setup_component(1): with assert_setup_component(1):
yield from async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}}) await async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})
hass.bus.async_listen_once = MagicMock() hass.bus.async_listen_once = MagicMock()
ffmpeg_dev = MockFFmpegDev(hass, False) ffmpeg_dev = MockFFmpegDev(hass, False)
yield from ffmpeg_dev.async_added_to_hass() await ffmpeg_dev.async_added_to_hass()
assert hass.bus.async_listen_once.called assert hass.bus.async_listen_once.called
assert hass.bus.async_listen_once.call_count == 1 assert hass.bus.async_listen_once.call_count == 1
@asyncio.coroutine async def test_setup_component_test_service_start(hass):
def test_setup_component_test_service_start(hass):
"""Set up ffmpeg component test service start.""" """Set up ffmpeg component test service start."""
with assert_setup_component(1): with assert_setup_component(1):
yield from async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}}) await async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})
ffmpeg_dev = MockFFmpegDev(hass, False) ffmpeg_dev = MockFFmpegDev(hass, False)
yield from ffmpeg_dev.async_added_to_hass() await ffmpeg_dev.async_added_to_hass()
async_start(hass) async_start(hass)
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert ffmpeg_dev.called_start assert ffmpeg_dev.called_start
@asyncio.coroutine async def test_setup_component_test_service_stop(hass):
def test_setup_component_test_service_stop(hass):
"""Set up ffmpeg component test service stop.""" """Set up ffmpeg component test service stop."""
with assert_setup_component(1): with assert_setup_component(1):
yield from async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}}) await async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})
ffmpeg_dev = MockFFmpegDev(hass, False) ffmpeg_dev = MockFFmpegDev(hass, False)
yield from ffmpeg_dev.async_added_to_hass() await ffmpeg_dev.async_added_to_hass()
async_stop(hass) async_stop(hass)
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert ffmpeg_dev.called_stop assert ffmpeg_dev.called_stop
@asyncio.coroutine async def test_setup_component_test_service_restart(hass):
def test_setup_component_test_service_restart(hass):
"""Set up ffmpeg component test service restart.""" """Set up ffmpeg component test service restart."""
with assert_setup_component(1): with assert_setup_component(1):
yield from async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}}) await async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})
ffmpeg_dev = MockFFmpegDev(hass, False) ffmpeg_dev = MockFFmpegDev(hass, False)
yield from ffmpeg_dev.async_added_to_hass() await ffmpeg_dev.async_added_to_hass()
async_restart(hass) async_restart(hass)
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert ffmpeg_dev.called_stop assert ffmpeg_dev.called_stop
assert ffmpeg_dev.called_start assert ffmpeg_dev.called_start
@asyncio.coroutine async def test_setup_component_test_service_start_with_entity(hass):
def test_setup_component_test_service_start_with_entity(hass):
"""Set up ffmpeg component test service start.""" """Set up ffmpeg component test service start."""
with assert_setup_component(1): with assert_setup_component(1):
yield from async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}}) await async_setup_component(hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})
ffmpeg_dev = MockFFmpegDev(hass, False) ffmpeg_dev = MockFFmpegDev(hass, False)
yield from ffmpeg_dev.async_added_to_hass() await ffmpeg_dev.async_added_to_hass()
async_start(hass, "test.ffmpeg_device") async_start(hass, "test.ffmpeg_device")
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert ffmpeg_dev.called_start assert ffmpeg_dev.called_start
assert ffmpeg_dev.called_entities == ["test.ffmpeg_device"] assert ffmpeg_dev.called_entities == ["test.ffmpeg_device"]