From d648eb1e4f4fe9222570fc1247bbb85227a712a9 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sun, 9 Jun 2019 02:10:23 +0200 Subject: [PATCH] Minor improvements to automation test suite (#24424) * Minor improvements to automation test suite * Removes unused asyncio imports * Removes some vars that are not needed --- .../automation/test_homeassistant.py | 29 +++------- tests/components/automation/test_init.py | 57 ++++++++----------- 2 files changed, 31 insertions(+), 55 deletions(-) diff --git a/tests/components/automation/test_homeassistant.py b/tests/components/automation/test_homeassistant.py index 742a2aa857c..b8802501d5d 100644 --- a/tests/components/automation/test_homeassistant.py +++ b/tests/components/automation/test_homeassistant.py @@ -1,5 +1,4 @@ """The tests for the Event automation.""" -import asyncio from unittest.mock import patch, Mock from homeassistant.core import CoreState @@ -9,8 +8,7 @@ import homeassistant.components.automation as automation from tests.common import async_mock_service, mock_coro -@asyncio.coroutine -def test_if_fires_on_hass_start(hass): +async def test_if_fires_on_hass_start(hass): """Test the firing when HASS starts.""" calls = async_mock_service(hass, 'test', 'automation') hass.state = CoreState.not_running @@ -27,31 +25,29 @@ def test_if_fires_on_hass_start(hass): } } - res = yield from async_setup_component(hass, automation.DOMAIN, config) - assert res + assert await async_setup_component(hass, automation.DOMAIN, config) assert automation.is_on(hass, 'automation.hello') assert len(calls) == 0 - yield from hass.async_start() + await hass.async_start() assert automation.is_on(hass, 'automation.hello') assert len(calls) == 1 with patch('homeassistant.config.async_hass_config_yaml', Mock(return_value=mock_coro(config))): - yield from hass.services.async_call( + await hass.services.async_call( automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True) assert automation.is_on(hass, 'automation.hello') assert len(calls) == 1 -@asyncio.coroutine -def test_if_fires_on_hass_shutdown(hass): +async def test_if_fires_on_hass_shutdown(hass): """Test the firing when HASS starts.""" calls = async_mock_service(hass, 'test', 'automation') hass.state = CoreState.not_running - res = yield from async_setup_component(hass, automation.DOMAIN, { + assert await async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { @@ -63,22 +59,13 @@ def test_if_fires_on_hass_shutdown(hass): } } }) - assert res assert automation.is_on(hass, 'automation.hello') assert len(calls) == 0 - yield from hass.async_start() + await hass.async_start() assert automation.is_on(hass, 'automation.hello') assert len(calls) == 0 with patch.object(hass.loop, 'stop'): - yield from hass.async_stop() + await hass.async_stop() assert len(calls) == 1 - - # with patch('homeassistant.config.async_hass_config_yaml', - # Mock(return_value=mock_coro(config))): - # yield from hass.services.async_call( - # automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True) - - # assert automation.is_on(hass, 'automation.hello') - # assert len(calls) == 1 diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index f8748b20efb..7fa658b0064 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -1,5 +1,4 @@ """The tests for the automation component.""" -import asyncio from datetime import timedelta from unittest.mock import patch, Mock @@ -616,8 +615,7 @@ async def test_reload_config_handles_load_fails(hass, calls): assert len(calls) == 2 -@asyncio.coroutine -def test_automation_restore_state(hass): +async def test_automation_restore_state(hass): """Ensure states are restored on startup.""" time = dt_util.utcnow() @@ -642,39 +640,39 @@ def test_automation_restore_state(hass): 'action': {'service': 'test.automation'} }]} - assert (yield from async_setup_component(hass, automation.DOMAIN, config)) + assert await async_setup_component(hass, automation.DOMAIN, config) state = hass.states.get('automation.hello') assert state assert state.state == STATE_ON + assert state.attributes['last_triggered'] is None state = hass.states.get('automation.bye') assert state assert state.state == STATE_OFF - assert state.attributes.get('last_triggered') == time + assert state.attributes['last_triggered'] == time calls = async_mock_service(hass, 'test', 'automation') assert automation.is_on(hass, 'automation.bye') is False hass.bus.async_fire('test_event_bye') - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert len(calls) == 0 assert automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event_hello') - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert len(calls) == 1 -@asyncio.coroutine -def test_initial_value_off(hass): +async def test_initial_value_off(hass): """Test initial value off.""" calls = async_mock_service(hass, 'test', 'automation') - res = yield from async_setup_component(hass, automation.DOMAIN, { + assert await async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'initial_state': 'off', @@ -688,11 +686,10 @@ def test_initial_value_off(hass): } } }) - assert res assert not automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert len(calls) == 0 @@ -753,15 +750,14 @@ async def test_initial_value_off_but_restore_on(hass): assert len(calls) == 0 -@asyncio.coroutine -def test_initial_value_on_but_restore_off(hass): +async def test_initial_value_on_but_restore_off(hass): """Test initial value on and restored state is turned off.""" calls = async_mock_service(hass, 'test', 'automation') mock_restore_cache(hass, ( State('automation.hello', STATE_OFF), )) - res = yield from async_setup_component(hass, automation.DOMAIN, { + assert await async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'initial_state': 'on', @@ -775,23 +771,21 @@ def test_initial_value_on_but_restore_off(hass): } } }) - assert res assert automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert len(calls) == 1 -@asyncio.coroutine -def test_no_initial_value_and_restore_off(hass): +async def test_no_initial_value_and_restore_off(hass): """Test initial value off and restored state is turned on.""" calls = async_mock_service(hass, 'test', 'automation') mock_restore_cache(hass, ( State('automation.hello', STATE_OFF), )) - res = yield from async_setup_component(hass, automation.DOMAIN, { + assert await async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { @@ -804,20 +798,18 @@ def test_no_initial_value_and_restore_off(hass): } } }) - assert res assert not automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert len(calls) == 0 -@asyncio.coroutine -def test_automation_is_on_if_no_initial_state_or_restore(hass): +async def test_automation_is_on_if_no_initial_state_or_restore(hass): """Test initial value is on when no initial state or restored state.""" calls = async_mock_service(hass, 'test', 'automation') - res = yield from async_setup_component(hass, automation.DOMAIN, { + assert await async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { @@ -830,21 +822,19 @@ def test_automation_is_on_if_no_initial_state_or_restore(hass): } } }) - assert res assert automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert len(calls) == 1 -@asyncio.coroutine -def test_automation_not_trigger_on_bootstrap(hass): +async def test_automation_not_trigger_on_bootstrap(hass): """Test if automation is not trigger on bootstrap.""" hass.state = CoreState.not_running calls = async_mock_service(hass, 'test', 'automation') - res = yield from async_setup_component(hass, automation.DOMAIN, { + assert await async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { @@ -857,19 +847,18 @@ def test_automation_not_trigger_on_bootstrap(hass): } } }) - assert res assert automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert len(calls) == 0 hass.bus.async_fire(EVENT_HOMEASSISTANT_START) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert len(calls) == 1 assert ['hello.world'] == calls[0].data.get(ATTR_ENTITY_ID)