Fix automations listening to HOMEASSISTANT_START (#6936)
* Fire EVENT_HOMEASSISTANT_START automations off right away while starting * Actually have core state be set to 'starting' during boot * Fix correct start implementation * Test and deprecate event automation platform on start * Fix doc strings * Remove shutting down exception * More strict when to mark an instance as finished * Add automation platform to listen for start/shutdown * When we stop we should wait till it's all done * Fix testing * Fix async bugs in tests * Only set UVLOOP when hass starts from CLI * This hangs normal asyncio event loop * Clean up Z-Wave node entity test
This commit is contained in:
1 parent
289d6b6605
commit
29f385ea76
23 files changed
+258
-97
No files matched your search
+10
-10
@@ -23,12 +23,13 @@ import homeassistant.util.yaml as yaml
|
||||
from homeassistant.const import (
|
||||
STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, EVENT_TIME_CHANGED,
|
||||
EVENT_STATE_CHANGED, EVENT_PLATFORM_DISCOVERED, ATTR_SERVICE,
|
||||
ATTR_DISCOVERED, SERVER_PORT, EVENT_HOMEASSISTANT_STOP)
|
||||
ATTR_DISCOVERED, SERVER_PORT, EVENT_HOMEASSISTANT_CLOSE)
|
||||
from homeassistant.components import sun, mqtt, recorder
|
||||
from homeassistant.components.http.auth import auth_middleware
|
||||
from homeassistant.components.http.const import (
|
||||
KEY_USE_X_FORWARDED_FOR, KEY_BANS_ENABLED, KEY_TRUSTED_NETWORKS)
|
||||
from homeassistant.util.async import run_callback_threadsafe
|
||||
from homeassistant.util.async import (
|
||||
run_callback_threadsafe, run_coroutine_threadsafe)
|
||||
|
||||
_TEST_INSTANCE_PORT = SERVER_PORT
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -58,15 +59,11 @@ def get_test_home_assistant():
|
||||
loop.run_forever()
|
||||
stop_event.set()
|
||||
|
||||
orig_start = hass.start
|
||||
orig_stop = hass.stop
|
||||
|
||||
@patch.object(hass.loop, 'run_forever')
|
||||
@patch.object(hass.loop, 'close')
|
||||
def start_hass(*mocks):
|
||||
"""Helper to start hass."""
|
||||
orig_start()
|
||||
hass.block_till_done()
|
||||
run_coroutine_threadsafe(hass.async_start(), loop=hass.loop).result()
|
||||
|
||||
def stop_hass():
|
||||
"""Stop hass."""
|
||||
@@ -101,7 +98,6 @@ def async_test_home_assistant(loop):
|
||||
return orig_async_add_job(target, *args)
|
||||
|
||||
hass.async_add_job = async_add_job
|
||||
hass.async_track_tasks()
|
||||
|
||||
hass.config.location_name = 'test home'
|
||||
hass.config.config_dir = get_test_config_dir()
|
||||
@@ -123,7 +119,11 @@ def async_test_home_assistant(loop):
|
||||
@asyncio.coroutine
|
||||
def mock_async_start():
|
||||
"""Start the mocking."""
|
||||
with patch('homeassistant.core._async_create_timer'):
|
||||
# 1. We only mock time during tests
|
||||
# 2. We want block_till_done that is called inside stop_track_tasks
|
||||
with patch('homeassistant.core._async_create_timer'), \
|
||||
patch.object(hass, 'async_stop_track_tasks',
|
||||
hass.async_block_till_done):
|
||||
yield from orig_start()
|
||||
|
||||
hass.async_start = mock_async_start
|
||||
@@ -134,7 +134,7 @@ def async_test_home_assistant(loop):
|
||||
global INST_COUNT
|
||||
INST_COUNT -= 1
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, clear_instance)
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, clear_instance)
|
||||
|
||||
return hass
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
"""The tests for the Event automation."""
|
||||
import asyncio
|
||||
import unittest
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
||||
from homeassistant.core import callback, CoreState
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
|
||||
from tests.common import get_test_home_assistant, mock_component
|
||||
from tests.common import get_test_home_assistant, mock_component, mock_service
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
@@ -92,3 +94,30 @@ class TestAutomationEvent(unittest.TestCase):
|
||||
self.hass.bus.fire('test_event', {'some_attr': 'some_other_value'})
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_if_fires_on_event_with_data(hass):
|
||||
"""Test the firing of events with data."""
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
hass.state = CoreState.not_running
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': EVENT_HOMEASSISTANT_START,
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
}
|
||||
})
|
||||
assert res
|
||||
assert not automation.is_on(hass, 'automation.hello')
|
||||
assert len(calls) == 0
|
||||
|
||||
yield from hass.async_start()
|
||||
assert automation.is_on(hass, 'automation.hello')
|
||||
assert len(calls) == 1
|
||||
@@ -0,0 +1,84 @@
|
||||
"""The tests for the Event automation."""
|
||||
import asyncio
|
||||
from unittest.mock import patch, Mock
|
||||
|
||||
from homeassistant.core import CoreState
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
|
||||
from tests.common import mock_service, mock_coro
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_if_fires_on_hass_start(hass):
|
||||
"""Test the firing when HASS starts."""
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
hass.state = CoreState.not_running
|
||||
config = {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'homeassistant',
|
||||
'event': 'start',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, config)
|
||||
assert res
|
||||
assert not automation.is_on(hass, 'automation.hello')
|
||||
assert len(calls) == 0
|
||||
|
||||
yield from 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(
|
||||
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):
|
||||
"""Test the firing when HASS starts."""
|
||||
calls = mock_service(hass, 'test', 'automation')
|
||||
hass.state = CoreState.not_running
|
||||
|
||||
res = yield from async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
'platform': 'homeassistant',
|
||||
'event': 'shutdown',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
}
|
||||
})
|
||||
assert res
|
||||
assert not automation.is_on(hass, 'automation.hello')
|
||||
assert len(calls) == 0
|
||||
|
||||
yield from 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()
|
||||
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
|
||||
@@ -65,7 +65,7 @@ class TestFFmpegNoiseSetup(object):
|
||||
entity = self.hass.states.get('binary_sensor.ffmpeg_noise')
|
||||
assert entity.state == 'off'
|
||||
|
||||
mock_ffmpeg.call_args[0][2](True)
|
||||
self.hass.add_job(mock_ffmpeg.call_args[0][2], True)
|
||||
self.hass.block_till_done()
|
||||
|
||||
entity = self.hass.states.get('binary_sensor.ffmpeg_noise')
|
||||
@@ -130,7 +130,7 @@ class TestFFmpegMotionSetup(object):
|
||||
entity = self.hass.states.get('binary_sensor.ffmpeg_motion')
|
||||
assert entity.state == 'off'
|
||||
|
||||
mock_ffmpeg.call_args[0][2](True)
|
||||
self.hass.add_job(mock_ffmpeg.call_args[0][2], True)
|
||||
self.hass.block_till_done()
|
||||
|
||||
entity = self.hass.states.get('binary_sensor.ffmpeg_motion')
|
||||
|
||||
@@ -166,7 +166,7 @@ class TestAlert(unittest.TestCase):
|
||||
def test_noack(self):
|
||||
"""Test no ack feature."""
|
||||
entity = alert.Alert(self.hass, *TEST_NOACK)
|
||||
self.hass.async_add_job(entity.begin_alerting)
|
||||
self.hass.add_job(entity.begin_alerting)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(True, entity.hidden)
|
||||
@@ -1,49 +1,33 @@
|
||||
"""Test Z-Wave node entity."""
|
||||
import asyncio
|
||||
import unittest
|
||||
from unittest.mock import patch, Mock
|
||||
from tests.common import get_test_home_assistant
|
||||
from unittest.mock import patch
|
||||
import tests.mock.zwave as mock_zwave
|
||||
import pytest
|
||||
from homeassistant.components.zwave import node_entity
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('mock_openzwave')
|
||||
class TestZWaveBaseEntity(unittest.TestCase):
|
||||
"""Class to test ZWaveBaseEntity."""
|
||||
@asyncio.coroutine
|
||||
def test_maybe_schedule_update(hass, mock_openzwave):
|
||||
"""Test maybe schedule update."""
|
||||
base_entity = node_entity.ZWaveBaseEntity()
|
||||
base_entity.hass = hass
|
||||
|
||||
def setUp(self):
|
||||
"""Initialize values for this testcase class."""
|
||||
self.hass = get_test_home_assistant()
|
||||
with patch.object(hass.loop, 'call_later') as mock_call_later:
|
||||
base_entity._schedule_update()
|
||||
assert mock_call_later.called
|
||||
|
||||
def call_soon(time, func, *args):
|
||||
"""Replace call_later by call_soon."""
|
||||
return self.hass.loop.call_soon(func, *args)
|
||||
base_entity._schedule_update()
|
||||
assert len(mock_call_later.mock_calls) == 1
|
||||
|
||||
self.hass.loop.call_later = call_soon
|
||||
self.base_entity = node_entity.ZWaveBaseEntity()
|
||||
self.base_entity.hass = self.hass
|
||||
self.hass.start()
|
||||
do_update = mock_call_later.mock_calls[0][1][1]
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
with patch.object(hass, 'async_add_job') as mock_add_job:
|
||||
do_update()
|
||||
assert mock_add_job.called
|
||||
|
||||
def test_maybe_schedule_update(self):
|
||||
"""Test maybe_schedule_update."""
|
||||
with patch.object(self.base_entity, 'async_update_ha_state',
|
||||
Mock()) as mock_update:
|
||||
self.base_entity.maybe_schedule_update()
|
||||
self.hass.block_till_done()
|
||||
mock_update.assert_called_once_with()
|
||||
|
||||
def test_maybe_schedule_update_called_twice(self):
|
||||
"""Test maybe_schedule_update called twice."""
|
||||
with patch.object(self.base_entity, 'async_update_ha_state',
|
||||
Mock()) as mock_update:
|
||||
self.base_entity.maybe_schedule_update()
|
||||
self.base_entity.maybe_schedule_update()
|
||||
self.hass.block_till_done()
|
||||
mock_update.assert_called_once_with()
|
||||
base_entity._schedule_update()
|
||||
assert len(mock_call_later.mock_calls) == 2
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('mock_openzwave')
|
||||
|
||||
Reference in new issue
Block a user