Minor improvements to automation test suite (#24424)

* Minor improvements to automation test suite

* Removes unused asyncio imports

* Removes some vars that are not needed
This commit is contained in:
Franck Nijhof 2019-06-09 02:10:23 +02:00 committed by Andrew Sayre
parent 848a2a95a8
commit d648eb1e4f
2 changed files with 31 additions and 55 deletions

View File

@ -1,5 +1,4 @@
"""The tests for the Event automation.""" """The tests for the Event automation."""
import asyncio
from unittest.mock import patch, Mock from unittest.mock import patch, Mock
from homeassistant.core import CoreState from homeassistant.core import CoreState
@ -9,8 +8,7 @@ import homeassistant.components.automation as automation
from tests.common import async_mock_service, mock_coro from tests.common import async_mock_service, mock_coro
@asyncio.coroutine async def test_if_fires_on_hass_start(hass):
def test_if_fires_on_hass_start(hass):
"""Test the firing when HASS starts.""" """Test the firing when HASS starts."""
calls = async_mock_service(hass, 'test', 'automation') calls = async_mock_service(hass, 'test', 'automation')
hass.state = CoreState.not_running 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 await async_setup_component(hass, automation.DOMAIN, config)
assert res
assert automation.is_on(hass, 'automation.hello') assert automation.is_on(hass, 'automation.hello')
assert len(calls) == 0 assert len(calls) == 0
yield from hass.async_start() await hass.async_start()
assert automation.is_on(hass, 'automation.hello') assert automation.is_on(hass, 'automation.hello')
assert len(calls) == 1 assert len(calls) == 1
with patch('homeassistant.config.async_hass_config_yaml', with patch('homeassistant.config.async_hass_config_yaml',
Mock(return_value=mock_coro(config))): Mock(return_value=mock_coro(config))):
yield from hass.services.async_call( await hass.services.async_call(
automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True) automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True)
assert automation.is_on(hass, 'automation.hello') assert automation.is_on(hass, 'automation.hello')
assert len(calls) == 1 assert len(calls) == 1
@asyncio.coroutine async def test_if_fires_on_hass_shutdown(hass):
def test_if_fires_on_hass_shutdown(hass):
"""Test the firing when HASS starts.""" """Test the firing when HASS starts."""
calls = async_mock_service(hass, 'test', 'automation') calls = async_mock_service(hass, 'test', 'automation')
hass.state = CoreState.not_running hass.state = CoreState.not_running
res = yield from async_setup_component(hass, automation.DOMAIN, { assert await async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: { automation.DOMAIN: {
'alias': 'hello', 'alias': 'hello',
'trigger': { 'trigger': {
@ -63,22 +59,13 @@ def test_if_fires_on_hass_shutdown(hass):
} }
} }
}) })
assert res
assert automation.is_on(hass, 'automation.hello') assert automation.is_on(hass, 'automation.hello')
assert len(calls) == 0 assert len(calls) == 0
yield from hass.async_start() await hass.async_start()
assert automation.is_on(hass, 'automation.hello') assert automation.is_on(hass, 'automation.hello')
assert len(calls) == 0 assert len(calls) == 0
with patch.object(hass.loop, 'stop'): with patch.object(hass.loop, 'stop'):
yield from hass.async_stop() await hass.async_stop()
assert len(calls) == 1 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

View File

@ -1,5 +1,4 @@
"""The tests for the automation component.""" """The tests for the automation component."""
import asyncio
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch, Mock from unittest.mock import patch, Mock
@ -616,8 +615,7 @@ async def test_reload_config_handles_load_fails(hass, calls):
assert len(calls) == 2 assert len(calls) == 2
@asyncio.coroutine async def test_automation_restore_state(hass):
def test_automation_restore_state(hass):
"""Ensure states are restored on startup.""" """Ensure states are restored on startup."""
time = dt_util.utcnow() time = dt_util.utcnow()
@ -642,39 +640,39 @@ def test_automation_restore_state(hass):
'action': {'service': 'test.automation'} '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') state = hass.states.get('automation.hello')
assert state assert state
assert state.state == STATE_ON assert state.state == STATE_ON
assert state.attributes['last_triggered'] is None
state = hass.states.get('automation.bye') state = hass.states.get('automation.bye')
assert state assert state
assert state.state == STATE_OFF 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') calls = async_mock_service(hass, 'test', 'automation')
assert automation.is_on(hass, 'automation.bye') is False assert automation.is_on(hass, 'automation.bye') is False
hass.bus.async_fire('test_event_bye') 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 len(calls) == 0
assert automation.is_on(hass, 'automation.hello') assert automation.is_on(hass, 'automation.hello')
hass.bus.async_fire('test_event_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 assert len(calls) == 1
@asyncio.coroutine async def test_initial_value_off(hass):
def test_initial_value_off(hass):
"""Test initial value off.""" """Test initial value off."""
calls = async_mock_service(hass, 'test', 'automation') 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: { automation.DOMAIN: {
'alias': 'hello', 'alias': 'hello',
'initial_state': 'off', 'initial_state': 'off',
@ -688,11 +686,10 @@ def test_initial_value_off(hass):
} }
} }
}) })
assert res
assert not automation.is_on(hass, 'automation.hello') assert not automation.is_on(hass, 'automation.hello')
hass.bus.async_fire('test_event') hass.bus.async_fire('test_event')
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0
@ -753,15 +750,14 @@ async def test_initial_value_off_but_restore_on(hass):
assert len(calls) == 0 assert len(calls) == 0
@asyncio.coroutine async def test_initial_value_on_but_restore_off(hass):
def test_initial_value_on_but_restore_off(hass):
"""Test initial value on and restored state is turned off.""" """Test initial value on and restored state is turned off."""
calls = async_mock_service(hass, 'test', 'automation') calls = async_mock_service(hass, 'test', 'automation')
mock_restore_cache(hass, ( mock_restore_cache(hass, (
State('automation.hello', STATE_OFF), State('automation.hello', STATE_OFF),
)) ))
res = yield from async_setup_component(hass, automation.DOMAIN, { assert await async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: { automation.DOMAIN: {
'alias': 'hello', 'alias': 'hello',
'initial_state': 'on', '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') assert automation.is_on(hass, 'automation.hello')
hass.bus.async_fire('test_event') hass.bus.async_fire('test_event')
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@asyncio.coroutine async def test_no_initial_value_and_restore_off(hass):
def test_no_initial_value_and_restore_off(hass):
"""Test initial value off and restored state is turned on.""" """Test initial value off and restored state is turned on."""
calls = async_mock_service(hass, 'test', 'automation') calls = async_mock_service(hass, 'test', 'automation')
mock_restore_cache(hass, ( mock_restore_cache(hass, (
State('automation.hello', STATE_OFF), State('automation.hello', STATE_OFF),
)) ))
res = yield from async_setup_component(hass, automation.DOMAIN, { assert await async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: { automation.DOMAIN: {
'alias': 'hello', 'alias': 'hello',
'trigger': { 'trigger': {
@ -804,20 +798,18 @@ def test_no_initial_value_and_restore_off(hass):
} }
} }
}) })
assert res
assert not automation.is_on(hass, 'automation.hello') assert not automation.is_on(hass, 'automation.hello')
hass.bus.async_fire('test_event') hass.bus.async_fire('test_event')
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0
@asyncio.coroutine async def test_automation_is_on_if_no_initial_state_or_restore(hass):
def test_automation_is_on_if_no_initial_state_or_restore(hass):
"""Test initial value is on when no initial state or restored state.""" """Test initial value is on when no initial state or restored state."""
calls = async_mock_service(hass, 'test', 'automation') 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: { automation.DOMAIN: {
'alias': 'hello', 'alias': 'hello',
'trigger': { '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') assert automation.is_on(hass, 'automation.hello')
hass.bus.async_fire('test_event') hass.bus.async_fire('test_event')
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
@asyncio.coroutine async def test_automation_not_trigger_on_bootstrap(hass):
def test_automation_not_trigger_on_bootstrap(hass):
"""Test if automation is not trigger on bootstrap.""" """Test if automation is not trigger on bootstrap."""
hass.state = CoreState.not_running hass.state = CoreState.not_running
calls = async_mock_service(hass, 'test', 'automation') 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: { automation.DOMAIN: {
'alias': 'hello', 'alias': 'hello',
'trigger': { 'trigger': {
@ -857,19 +847,18 @@ def test_automation_not_trigger_on_bootstrap(hass):
} }
} }
}) })
assert res
assert automation.is_on(hass, 'automation.hello') assert automation.is_on(hass, 'automation.hello')
hass.bus.async_fire('test_event') hass.bus.async_fire('test_event')
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(calls) == 0
hass.bus.async_fire(EVENT_HOMEASSISTANT_START) 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') assert automation.is_on(hass, 'automation.hello')
hass.bus.async_fire('test_event') hass.bus.async_fire('test_event')
yield from hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1
assert ['hello.world'] == calls[0].data.get(ATTR_ENTITY_ID) assert ['hello.world'] == calls[0].data.get(ATTR_ENTITY_ID)