Fire event when core config is updated (#23922)

* Fire event when core config is updated
This commit is contained in:
Erik Montnemery authored and GitHub committed 2019-05-20 20:02:36 +02:00
1 parent eb912be47a
commit afe9fc221e
6 files changed
+180 -97

No files matched your search

-1
View File
@@ -122,7 +122,6 @@ def get_test_home_assistant():
async def async_test_home_assistant(loop):
"""Return a Home Assistant object pointing at test config dir."""
hass = ha.HomeAssistant(loop)
hass.config.async_load = Mock()
store = auth_store.AuthStore(hass)
hass.auth = auth.AuthManager(hass, store, {}, {})
ensure_auth_manager_loaded(hass.auth)
+31 -10
View File
@@ -1,6 +1,7 @@
"""Test config utils."""
# pylint: disable=protected-access
import asyncio
import copy
import os
import unittest.mock as mock
from collections import OrderedDict
@@ -11,7 +12,8 @@ import pytest
from voluptuous import MultipleInvalid, Invalid
import yaml
from homeassistant.core import DOMAIN, HomeAssistantError, Config
from homeassistant.core import (
DOMAIN, SOURCE_STORAGE, Config, HomeAssistantError)
import homeassistant.config as config_util
from homeassistant.loader import async_get_integration
from homeassistant.const import (
@@ -439,7 +441,32 @@ async def test_loading_configuration_from_storage(hass, hass_storage):
assert hass.config.time_zone.zone == 'Europe/Copenhagen'
assert len(hass.config.whitelist_external_dirs) == 2
assert '/tmp' in hass.config.whitelist_external_dirs
assert hass.config.config_source == config_util.SOURCE_STORAGE
assert hass.config.config_source == SOURCE_STORAGE
async def test_updating_configuration(hass, hass_storage):
"""Test updating configuration stores the new configuration."""
core_data = {
'data': {
'elevation': 10,
'latitude': 55,
'location_name': 'Home',
'longitude': 13,
'time_zone': 'Europe/Copenhagen',
'unit_system': 'metric'
},
'key': 'homeassistant.core_config',
'version': 1
}
hass_storage["homeassistant.core_config"] = dict(core_data)
await config_util.async_process_ha_core_config(
hass, {'whitelist_external_dirs': '/tmp'})
await hass.config.update(latitude=50)
new_core_data = copy.deepcopy(core_data)
new_core_data['data']['latitude'] = 50
assert hass_storage["homeassistant.core_config"] == new_core_data
assert hass.config.latitude == 50
async def test_override_stored_configuration(hass, hass_storage):
@@ -474,8 +501,6 @@ async def test_override_stored_configuration(hass, hass_storage):
async def test_loading_configuration(hass):
"""Test loading core config onto hass object."""
hass.config = mock.Mock()
await config_util.async_process_ha_core_config(hass, {
'latitude': 60,
'longitude': 50,
@@ -499,8 +524,6 @@ async def test_loading_configuration(hass):
async def test_loading_configuration_temperature_unit(hass):
"""Test backward compatibility when loading core config."""
hass.config = mock.Mock()
await config_util.async_process_ha_core_config(hass, {
'latitude': 60,
'longitude': 50,
@@ -521,8 +544,6 @@ async def test_loading_configuration_temperature_unit(hass):
async def test_loading_configuration_from_packages(hass):
"""Test loading packages config onto hass object config."""
hass.config = mock.Mock()
await config_util.async_process_ha_core_config(hass, {
'latitude': 39,
'longitude': -1,
@@ -586,12 +607,12 @@ async def test_discovering_configuration_auto_detect_fails(mock_detect,
mock_elevation,
hass):
"""Test config remains unchanged if discovery fails."""
hass.config = Config()
hass.config = Config(hass)
hass.config.config_dir = "/test/config"
await config_util.async_process_ha_core_config(hass, {})
blankConfig = Config()
blankConfig = Config(hass)
assert hass.config.latitude == blankConfig.latitude
assert hass.config.longitude == blankConfig.longitude
assert hass.config.elevation == blankConfig.elevation
+29 -2
View File
@@ -23,7 +23,8 @@ from homeassistant.const import (
__version__, EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM,
ATTR_NOW, EVENT_TIME_CHANGED, EVENT_TIMER_OUT_OF_SYNC, ATTR_SECONDS,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_CLOSE,
EVENT_SERVICE_REGISTERED, EVENT_SERVICE_REMOVED, EVENT_CALL_SERVICE)
EVENT_SERVICE_REGISTERED, EVENT_SERVICE_REMOVED, EVENT_CALL_SERVICE,
EVENT_CORE_CONFIG_UPDATE)
from tests.common import get_test_home_assistant, async_mock_service
@@ -871,7 +872,7 @@ class TestConfig(unittest.TestCase):
# pylint: disable=invalid-name
def setUp(self):
"""Set up things to be run when tests are started."""
self.config = ha.Config()
self.config = ha.Config(None)
assert self.config.config_dir is None
def test_path_with_file(self):
@@ -942,6 +943,32 @@ class TestConfig(unittest.TestCase):
self.config.is_allowed_path(None)
async def test_event_on_update(hass, hass_storage):
"""Test that event is fired on update."""
events = []
@ha.callback
def callback(event):
events.append(event)
hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, callback)
assert hass.config.latitude != 12
await hass.config.update(latitude=12)
await hass.async_block_till_done()
assert hass.config.latitude == 12
assert len(events) == 1
assert events[0].data == {'latitude': 12}
def test_bad_timezone_raises_value_error(hass):
"""Test bad timezone raises ValueError."""
with pytest.raises(ValueError):
hass.config.set_time_zone('not_a_timezone')
@patch('homeassistant.core.monotonic')
def test_create_timer(mock_monotonic, loop):
"""Test create timer."""