Move core service from core to components (#5787)

* Move core servcie from core to components

* add new handler for signals/exception

* Static persistent id

* Move unittest

* fix coro/callback

* Add more unittest for new services

* Address comments

* Update __init__.py
This commit is contained in:
Pascal Vizeli
2017-02-08 09:17:52 -08:00
committed by Paulus Schoutsen
parent 08efe2bf6d
commit 3f82ef64a1
6 changed files with 165 additions and 102 deletions
+43 -1
View File
@@ -11,11 +11,12 @@ from homeassistant import config
from homeassistant.const import (
STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE)
import homeassistant.components as comps
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity
from homeassistant.util.async import run_coroutine_threadsafe
from tests.common import (
get_test_home_assistant, mock_service, patch_yaml_files)
get_test_home_assistant, mock_service, patch_yaml_files, mock_coro)
class TestComponentsCore(unittest.TestCase):
@@ -150,3 +151,44 @@ class TestComponentsCore(unittest.TestCase):
assert mock_error.called
assert mock_process.called is False
@patch('homeassistant.core.HomeAssistant.async_stop',
return_value=mock_coro()())
def test_stop_homeassistant(self, mock_stop):
"""Test stop service."""
comps.stop(self.hass)
self.hass.block_till_done()
assert mock_stop.called
@patch('homeassistant.core.HomeAssistant.async_stop',
return_value=mock_coro()())
@patch('homeassistant.config.async_check_ha_config_file',
return_value=mock_coro()())
def test_restart_homeassistant(self, mock_check, mock_restart):
"""Test stop service."""
comps.restart(self.hass)
self.hass.block_till_done()
assert mock_restart.called
assert mock_check.called
@patch('homeassistant.core.HomeAssistant.async_stop',
return_value=mock_coro()())
@patch('homeassistant.config.async_check_ha_config_file',
side_effect=HomeAssistantError("Test error"))
def test_restart_homeassistant_wrong_conf(self, mock_check, mock_restart):
"""Test stop service."""
comps.restart(self.hass)
self.hass.block_till_done()
assert mock_check.called
assert not mock_restart.called
@patch('homeassistant.core.HomeAssistant.async_stop',
return_value=mock_coro()())
@patch('homeassistant.config.async_check_ha_config_file',
return_value=mock_coro()())
def test_check_config(self, mock_check, mock_stop):
"""Test stop service."""
comps.check_config(self.hass)
self.hass.block_till_done()
assert mock_check.called
assert not mock_stop.called
+32 -1
View File
@@ -18,7 +18,7 @@ from homeassistant.util.async import run_coroutine_threadsafe
from homeassistant.helpers.entity import Entity
from tests.common import (
get_test_config_dir, get_test_home_assistant)
get_test_config_dir, get_test_home_assistant, mock_generator)
CONFIG_DIR = get_test_config_dir()
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
@@ -376,6 +376,37 @@ class TestConfig(unittest.TestCase):
assert self.hass.config.units == blankConfig.units
assert self.hass.config.time_zone == blankConfig.time_zone
@mock.patch('asyncio.create_subprocess_exec')
def test_check_ha_config_file_correct(self, mock_create):
"""Check that restart propagates to stop."""
process_mock = mock.MagicMock()
attrs = {
'communicate.return_value': mock_generator(('output', 'error')),
'wait.return_value': mock_generator(0)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_generator(process_mock)
assert run_coroutine_threadsafe(
config_util.async_check_ha_config_file(self.hass), self.hass.loop
).result() is None
@mock.patch('asyncio.create_subprocess_exec')
def test_check_ha_config_file_wrong(self, mock_create):
"""Check that restart with a bad config doesn't propagate to stop."""
process_mock = mock.MagicMock()
attrs = {
'communicate.return_value':
mock_generator((r'\033[hellom'.encode('utf-8'), 'error')),
'wait.return_value': mock_generator(1)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_generator(process_mock)
with self.assertRaises(HomeAssistantError):
run_coroutine_threadsafe(
config_util.async_check_ha_config_file(self.hass),
self.hass.loop
).result()
# pylint: disable=redefined-outer-name
@pytest.fixture
+2 -36
View File
@@ -14,10 +14,9 @@ from homeassistant.util.async import run_coroutine_threadsafe
import homeassistant.util.dt as dt_util
from homeassistant.util.unit_system import (METRIC_SYSTEM)
from homeassistant.const import (
__version__, EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM,
SERVICE_HOMEASSISTANT_RESTART, RESTART_EXIT_CODE)
__version__, EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM)
from tests.common import get_test_home_assistant, mock_generator
from tests.common import get_test_home_assistant
PST = pytz.timezone('America/Los_Angeles')
@@ -221,39 +220,6 @@ class TestHomeAssistant(unittest.TestCase):
with pytest.raises(ValueError):
self.hass.add_job(None, 'test_arg')
@patch('asyncio.create_subprocess_exec')
def test_restart(self, mock_create):
"""Check that restart propagates to stop."""
process_mock = MagicMock()
attrs = {
'communicate.return_value': mock_generator(('output', 'error')),
'wait.return_value': mock_generator(0)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_generator(process_mock)
self.hass.start()
with patch.object(self.hass, 'async_stop') as mock_stop:
self.hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART)
mock_stop.assert_called_once_with()
self.assertEqual(RESTART_EXIT_CODE, self.hass.exit_code)
@patch('asyncio.create_subprocess_exec')
def test_restart_bad_config(self, mock_create):
"""Check that restart with a bad config doesn't propagate to stop."""
process_mock = MagicMock()
attrs = {
'communicate.return_value':
mock_generator((r'\033[hellom'.encode('utf-8'), 'error')),
'wait.return_value': mock_generator(1)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_generator(process_mock)
self.hass.start()
with patch.object(self.hass, 'async_stop') as mock_stop:
self.hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART)
mock_stop.assert_not_called()
self.assertEqual(None, self.hass.exit_code)
class TestEvent(unittest.TestCase):
"""A Test Event class."""