Require core config detection to be triggerd manually (#24019)
* Detect core config * Remove elevation * Lint * Lint * Fix type
This commit is contained in:
1 parent
f207e01510
commit
9e96397e6a
12 files changed
+188
-323
No files matched your search
@@ -1,24 +1,31 @@
|
||||
"""Test hassbian config."""
|
||||
import asyncio
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components import config
|
||||
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||
from homeassistant.const import CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util import dt as dt_util, location
|
||||
from tests.common import mock_coro
|
||||
|
||||
ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client(hass, hass_ws_client):
|
||||
"""Fixture that can interact with the config manager API."""
|
||||
with patch.object(config, 'SECTIONS', ['core']):
|
||||
assert await async_setup_component(hass, 'config', {})
|
||||
return await hass_ws_client(hass)
|
||||
|
||||
|
||||
async def test_validate_config_ok(hass, hass_client):
|
||||
"""Test checking config."""
|
||||
with patch.object(config, 'SECTIONS', ['core']):
|
||||
await async_setup_component(hass, 'config', {})
|
||||
|
||||
await asyncio.sleep(0.1, loop=hass.loop)
|
||||
|
||||
client = await hass_client()
|
||||
|
||||
with patch(
|
||||
@@ -42,11 +49,8 @@ async def test_validate_config_ok(hass, hass_client):
|
||||
assert result['errors'] == 'beer'
|
||||
|
||||
|
||||
async def test_websocket_core_update(hass, hass_ws_client):
|
||||
async def test_websocket_core_update(hass, client):
|
||||
"""Test core config update websocket command."""
|
||||
with patch.object(config, 'SECTIONS', ['core']):
|
||||
await async_setup_component(hass, 'config', {})
|
||||
|
||||
assert hass.config.latitude != 60
|
||||
assert hass.config.longitude != 50
|
||||
assert hass.config.elevation != 25
|
||||
@@ -54,7 +58,6 @@ async def test_websocket_core_update(hass, hass_ws_client):
|
||||
assert hass.config.units.name != CONF_UNIT_SYSTEM_IMPERIAL
|
||||
assert hass.config.time_zone.zone != 'America/New_York'
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json({
|
||||
'id': 5,
|
||||
'type': 'config/core/update',
|
||||
@@ -92,7 +95,7 @@ async def test_websocket_core_update_not_admin(
|
||||
await client.send_json({
|
||||
'id': 6,
|
||||
'type': 'config/core/update',
|
||||
'latitude': 123,
|
||||
'latitude': 23,
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
@@ -103,16 +106,12 @@ async def test_websocket_core_update_not_admin(
|
||||
assert msg['error']['code'] == 'unauthorized'
|
||||
|
||||
|
||||
async def test_websocket_bad_core_update(hass, hass_ws_client):
|
||||
async def test_websocket_bad_core_update(hass, client):
|
||||
"""Test core config update fails with bad parameters."""
|
||||
with patch.object(config, 'SECTIONS', ['core']):
|
||||
await async_setup_component(hass, 'config', {})
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json({
|
||||
'id': 7,
|
||||
'type': 'config/core/update',
|
||||
'latituude': 123,
|
||||
'latituude': 23,
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
@@ -121,3 +120,48 @@ async def test_websocket_bad_core_update(hass, hass_ws_client):
|
||||
assert msg['type'] == TYPE_RESULT
|
||||
assert not msg['success']
|
||||
assert msg['error']['code'] == 'invalid_format'
|
||||
|
||||
|
||||
async def test_detect_config(hass, client):
|
||||
"""Test detect config."""
|
||||
with patch('homeassistant.util.location.async_detect_location_info',
|
||||
return_value=mock_coro(None)):
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'type': 'config/core/detect',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg['success'] is True
|
||||
assert msg['result'] == {}
|
||||
|
||||
|
||||
async def test_detect_config_fail(hass, client):
|
||||
"""Test detect config."""
|
||||
with patch('homeassistant.util.location.async_detect_location_info',
|
||||
return_value=mock_coro(location.LocationInfo(
|
||||
ip=None,
|
||||
country_code=None,
|
||||
country_name=None,
|
||||
region_code=None,
|
||||
region_name=None,
|
||||
city=None,
|
||||
zip_code=None,
|
||||
latitude=None,
|
||||
longitude=None,
|
||||
use_metric=True,
|
||||
time_zone='Europe/Amsterdam',
|
||||
))):
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'type': 'config/core/detect',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg['success'] is True
|
||||
assert msg['result'] == {
|
||||
'unit_system': 'metric',
|
||||
'time_zone': 'Europe/Amsterdam',
|
||||
}
|
||||
@@ -44,8 +44,6 @@ def check_real(func):
|
||||
# Guard a few functions that would make network connections
|
||||
location.async_detect_location_info = \
|
||||
check_real(location.async_detect_location_info)
|
||||
location.async_get_elevation = \
|
||||
check_real(location.async_get_elevation)
|
||||
util.get_local_ip = lambda: '127.0.0.1'
|
||||
|
||||
|
||||
|
||||
+15
-108
@@ -12,17 +12,16 @@ import pytest
|
||||
from voluptuous import MultipleInvalid, Invalid
|
||||
import yaml
|
||||
|
||||
from homeassistant.core import (
|
||||
DOMAIN, SOURCE_STORAGE, Config, HomeAssistantError)
|
||||
from homeassistant.core import SOURCE_STORAGE, HomeAssistantError
|
||||
import homeassistant.config as config_util
|
||||
from homeassistant.loader import async_get_integration
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_ASSUMED_STATE,
|
||||
CONF_LATITUDE, CONF_LONGITUDE, CONF_UNIT_SYSTEM, CONF_NAME,
|
||||
CONF_TIME_ZONE, CONF_ELEVATION, CONF_CUSTOMIZE, __version__,
|
||||
CONF_CUSTOMIZE, __version__,
|
||||
CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL, CONF_TEMPERATURE_UNIT,
|
||||
CONF_AUTH_PROVIDERS, CONF_AUTH_MFA_MODULES)
|
||||
from homeassistant.util import location as location_util, dt as dt_util
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.yaml import SECRET_YAML
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.components.config.group import (
|
||||
@@ -31,12 +30,10 @@ from homeassistant.components.config.automation import (
|
||||
CONFIG_PATH as AUTOMATIONS_CONFIG_PATH)
|
||||
from homeassistant.components.config.script import (
|
||||
CONFIG_PATH as SCRIPTS_CONFIG_PATH)
|
||||
from homeassistant.components.config.customize import (
|
||||
CONFIG_PATH as CUSTOMIZE_CONFIG_PATH)
|
||||
import homeassistant.scripts.check_config as check_config
|
||||
|
||||
from tests.common import (
|
||||
get_test_config_dir, patch_yaml_files, mock_coro)
|
||||
get_test_config_dir, patch_yaml_files)
|
||||
|
||||
CONFIG_DIR = get_test_config_dir()
|
||||
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
|
||||
@@ -45,7 +42,6 @@ VERSION_PATH = os.path.join(CONFIG_DIR, config_util.VERSION_FILE)
|
||||
GROUP_PATH = os.path.join(CONFIG_DIR, GROUP_CONFIG_PATH)
|
||||
AUTOMATIONS_PATH = os.path.join(CONFIG_DIR, AUTOMATIONS_CONFIG_PATH)
|
||||
SCRIPTS_PATH = os.path.join(CONFIG_DIR, SCRIPTS_CONFIG_PATH)
|
||||
CUSTOMIZE_PATH = os.path.join(CONFIG_DIR, CUSTOMIZE_CONFIG_PATH)
|
||||
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
|
||||
|
||||
|
||||
@@ -77,20 +73,16 @@ def teardown():
|
||||
if os.path.isfile(SCRIPTS_PATH):
|
||||
os.remove(SCRIPTS_PATH)
|
||||
|
||||
if os.path.isfile(CUSTOMIZE_PATH):
|
||||
os.remove(CUSTOMIZE_PATH)
|
||||
|
||||
|
||||
async def test_create_default_config(hass):
|
||||
"""Test creation of default config."""
|
||||
await config_util.async_create_default_config(hass, CONFIG_DIR, False)
|
||||
await config_util.async_create_default_config(hass, CONFIG_DIR)
|
||||
|
||||
assert os.path.isfile(YAML_PATH)
|
||||
assert os.path.isfile(SECRET_PATH)
|
||||
assert os.path.isfile(VERSION_PATH)
|
||||
assert os.path.isfile(GROUP_PATH)
|
||||
assert os.path.isfile(AUTOMATIONS_PATH)
|
||||
assert os.path.isfile(CUSTOMIZE_PATH)
|
||||
|
||||
|
||||
def test_find_config_file_yaml():
|
||||
@@ -106,7 +98,7 @@ async def test_ensure_config_exists_creates_config(hass):
|
||||
If not creates a new config file.
|
||||
"""
|
||||
with mock.patch('builtins.print') as mock_print:
|
||||
await config_util.async_ensure_config_exists(hass, CONFIG_DIR, False)
|
||||
await config_util.async_ensure_config_exists(hass, CONFIG_DIR)
|
||||
|
||||
assert os.path.isfile(YAML_PATH)
|
||||
assert mock_print.called
|
||||
@@ -115,7 +107,7 @@ async def test_ensure_config_exists_creates_config(hass):
|
||||
async def test_ensure_config_exists_uses_existing_config(hass):
|
||||
"""Test that calling ensure_config_exists uses existing config."""
|
||||
create_file(YAML_PATH)
|
||||
await config_util.async_ensure_config_exists(hass, CONFIG_DIR, False)
|
||||
await config_util.async_ensure_config_exists(hass, CONFIG_DIR)
|
||||
|
||||
with open(YAML_PATH) as f:
|
||||
content = f.read()
|
||||
@@ -168,38 +160,6 @@ def test_load_yaml_config_preserves_key_order():
|
||||
list(config_util.load_yaml_config_file(YAML_PATH).items())
|
||||
|
||||
|
||||
async def test_create_default_config_detect_location(hass):
|
||||
"""Test that detect location sets the correct config keys."""
|
||||
with mock.patch('homeassistant.util.location.async_detect_location_info',
|
||||
return_value=mock_coro(location_util.LocationInfo(
|
||||
'0.0.0.0', 'US', 'United States', 'CA', 'California',
|
||||
'San Diego', '92122', 'America/Los_Angeles', 32.8594,
|
||||
-117.2073, True))), \
|
||||
mock.patch('homeassistant.util.location.async_get_elevation',
|
||||
return_value=mock_coro(101)), \
|
||||
mock.patch('builtins.print') as mock_print:
|
||||
await config_util.async_ensure_config_exists(hass, CONFIG_DIR)
|
||||
|
||||
config = config_util.load_yaml_config_file(YAML_PATH)
|
||||
|
||||
assert DOMAIN in config
|
||||
|
||||
ha_conf = config[DOMAIN]
|
||||
|
||||
expected_values = {
|
||||
CONF_LATITUDE: 32.8594,
|
||||
CONF_LONGITUDE: -117.2073,
|
||||
CONF_ELEVATION: 101,
|
||||
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_METRIC,
|
||||
CONF_NAME: 'Home',
|
||||
CONF_TIME_ZONE: 'America/Los_Angeles',
|
||||
CONF_CUSTOMIZE: OrderedDict(),
|
||||
}
|
||||
|
||||
assert expected_values == ha_conf
|
||||
assert mock_print.called
|
||||
|
||||
|
||||
async def test_create_default_config_returns_none_if_write_error(hass):
|
||||
"""Test the writing of a default configuration.
|
||||
|
||||
@@ -207,7 +167,7 @@ async def test_create_default_config_returns_none_if_write_error(hass):
|
||||
"""
|
||||
with mock.patch('builtins.print') as mock_print:
|
||||
assert await config_util.async_create_default_config(
|
||||
hass, os.path.join(CONFIG_DIR, 'non_existing_dir/'), False) is None
|
||||
hass, os.path.join(CONFIG_DIR, 'non_existing_dir/')) is None
|
||||
assert mock_print.called
|
||||
|
||||
|
||||
@@ -418,7 +378,7 @@ def test_migrate_no_file_on_upgrade(mock_os, mock_shutil, hass):
|
||||
|
||||
async def test_loading_configuration_from_storage(hass, hass_storage):
|
||||
"""Test loading core config onto hass object."""
|
||||
hass_storage["homeassistant.core_config"] = {
|
||||
hass_storage["core.config"] = {
|
||||
'data': {
|
||||
'elevation': 10,
|
||||
'latitude': 55,
|
||||
@@ -427,7 +387,7 @@ async def test_loading_configuration_from_storage(hass, hass_storage):
|
||||
'time_zone': 'Europe/Copenhagen',
|
||||
'unit_system': 'metric'
|
||||
},
|
||||
'key': 'homeassistant.core_config',
|
||||
'key': 'core.config',
|
||||
'version': 1
|
||||
}
|
||||
await config_util.async_process_ha_core_config(
|
||||
@@ -455,23 +415,23 @@ async def test_updating_configuration(hass, hass_storage):
|
||||
'time_zone': 'Europe/Copenhagen',
|
||||
'unit_system': 'metric'
|
||||
},
|
||||
'key': 'homeassistant.core_config',
|
||||
'key': 'core.config',
|
||||
'version': 1
|
||||
}
|
||||
hass_storage["homeassistant.core_config"] = dict(core_data)
|
||||
hass_storage["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_storage["core.config"] == new_core_data
|
||||
assert hass.config.latitude == 50
|
||||
|
||||
|
||||
async def test_override_stored_configuration(hass, hass_storage):
|
||||
"""Test loading core and YAML config onto hass object."""
|
||||
hass_storage["homeassistant.core_config"] = {
|
||||
hass_storage["core.config"] = {
|
||||
'data': {
|
||||
'elevation': 10,
|
||||
'latitude': 55,
|
||||
@@ -480,7 +440,7 @@ async def test_override_stored_configuration(hass, hass_storage):
|
||||
'time_zone': 'Europe/Copenhagen',
|
||||
'unit_system': 'metric'
|
||||
},
|
||||
'key': 'homeassistant.core_config',
|
||||
'key': 'core.config',
|
||||
'version': 1
|
||||
}
|
||||
await config_util.async_process_ha_core_config(hass, {
|
||||
@@ -571,59 +531,6 @@ async def test_loading_configuration_from_packages(hass):
|
||||
})
|
||||
|
||||
|
||||
@asynctest.mock.patch(
|
||||
'homeassistant.util.location.async_detect_location_info',
|
||||
autospec=True, return_value=mock_coro(location_util.LocationInfo(
|
||||
'0.0.0.0', 'US', 'United States', 'CA',
|
||||
'California', 'San Diego', '92122',
|
||||
'America/Los_Angeles', 32.8594, -117.2073, True)))
|
||||
@asynctest.mock.patch('homeassistant.util.location.async_get_elevation',
|
||||
autospec=True, return_value=mock_coro(101))
|
||||
async def test_discovering_configuration(mock_detect, mock_elevation, hass):
|
||||
"""Test auto discovery for missing core configs."""
|
||||
hass.config.latitude = None
|
||||
hass.config.longitude = None
|
||||
hass.config.elevation = None
|
||||
hass.config.location_name = None
|
||||
hass.config.time_zone = None
|
||||
|
||||
await config_util.async_process_ha_core_config(hass, {})
|
||||
|
||||
assert hass.config.latitude == 32.8594
|
||||
assert hass.config.longitude == -117.2073
|
||||
assert hass.config.elevation == 101
|
||||
assert hass.config.location_name == 'San Diego'
|
||||
assert hass.config.units.name == CONF_UNIT_SYSTEM_METRIC
|
||||
assert hass.config.units.is_metric
|
||||
assert hass.config.time_zone.zone == 'America/Los_Angeles'
|
||||
assert hass.config.config_source == config_util.SOURCE_DISCOVERED
|
||||
|
||||
|
||||
@asynctest.mock.patch('homeassistant.util.location.async_detect_location_info',
|
||||
autospec=True, return_value=mock_coro(None))
|
||||
@asynctest.mock.patch('homeassistant.util.location.async_get_elevation',
|
||||
return_value=mock_coro(0))
|
||||
async def test_discovering_configuration_auto_detect_fails(mock_detect,
|
||||
mock_elevation,
|
||||
hass):
|
||||
"""Test config remains unchanged if discovery fails."""
|
||||
hass.config = Config(hass)
|
||||
hass.config.config_dir = "/test/config"
|
||||
|
||||
await config_util.async_process_ha_core_config(hass, {})
|
||||
|
||||
blankConfig = Config(hass)
|
||||
assert hass.config.latitude == blankConfig.latitude
|
||||
assert hass.config.longitude == blankConfig.longitude
|
||||
assert hass.config.elevation == blankConfig.elevation
|
||||
assert hass.config.location_name == blankConfig.location_name
|
||||
assert hass.config.units == blankConfig.units
|
||||
assert hass.config.time_zone == blankConfig.time_zone
|
||||
assert len(hass.config.whitelist_external_dirs) == 1
|
||||
assert "/test/config/www" in hass.config.whitelist_external_dirs
|
||||
assert hass.config.config_source == config_util.SOURCE_DISCOVERED
|
||||
|
||||
|
||||
@asynctest.mock.patch(
|
||||
'homeassistant.scripts.check_config.check_ha_config_file')
|
||||
async def test_check_ha_config_file_correct(mock_check, hass):
|
||||
|
||||
+5
-5
@@ -891,17 +891,17 @@ class TestConfig(unittest.TestCase):
|
||||
"""Test as dict."""
|
||||
self.config.config_dir = '/tmp/ha-config'
|
||||
expected = {
|
||||
'latitude': None,
|
||||
'longitude': None,
|
||||
'elevation': None,
|
||||
'latitude': 0,
|
||||
'longitude': 0,
|
||||
'elevation': 0,
|
||||
CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(),
|
||||
'location_name': None,
|
||||
'location_name': "Home",
|
||||
'time_zone': 'UTC',
|
||||
'components': set(),
|
||||
'config_dir': '/tmp/ha-config',
|
||||
'whitelist_external_dirs': set(),
|
||||
'version': __version__,
|
||||
'config_source': None,
|
||||
'config_source': "default",
|
||||
}
|
||||
|
||||
assert expected == self.config.as_dict()
|
||||
|
||||
@@ -116,10 +116,8 @@ async def test_detect_location_info_ip_api(aioclient_mock, session):
|
||||
|
||||
async def test_detect_location_info_both_queries_fail(session):
|
||||
"""Ensure we return None if both queries fail."""
|
||||
with patch('homeassistant.util.location.async_get_elevation',
|
||||
return_value=mock_coro(0)), \
|
||||
patch('homeassistant.util.location._get_ipapi',
|
||||
return_value=mock_coro(None)), \
|
||||
with patch('homeassistant.util.location._get_ipapi',
|
||||
return_value=mock_coro(None)), \
|
||||
patch('homeassistant.util.location._get_ip_api',
|
||||
return_value=mock_coro(None)):
|
||||
info = await location_util.async_detect_location_info(
|
||||
@@ -137,26 +135,3 @@ async def test_ip_api_query_raises(raising_session):
|
||||
"""Test ip api query when the request to API fails."""
|
||||
info = await location_util._get_ip_api(raising_session)
|
||||
assert info is None
|
||||
|
||||
|
||||
async def test_elevation_query_raises(raising_session):
|
||||
"""Test elevation when the request to API fails."""
|
||||
elevation = await location_util.async_get_elevation(
|
||||
raising_session, 10, 10, _test_real=True)
|
||||
assert elevation == 0
|
||||
|
||||
|
||||
async def test_elevation_query_fails(aioclient_mock, session):
|
||||
"""Test elevation when the request to API fails."""
|
||||
aioclient_mock.get(location_util.ELEVATION_URL, text='{}', status=401)
|
||||
elevation = await location_util.async_get_elevation(
|
||||
session, 10, 10, _test_real=True)
|
||||
assert elevation == 0
|
||||
|
||||
|
||||
async def test_elevation_query_nonjson(aioclient_mock, session):
|
||||
"""Test if elevation API returns a non JSON value."""
|
||||
aioclient_mock.get(location_util.ELEVATION_URL, text='{ I am not JSON }')
|
||||
elevation = await location_util.async_get_elevation(
|
||||
session, 10, 10, _test_real=True)
|
||||
assert elevation == 0
|
||||
Reference in new issue
Block a user