Fix handling of additional data in core config storage (#35660)

pull/35699/head^2
Franck Nijhof 2020-05-16 13:31:15 +02:00 committed by GitHub
parent aaf515ef67
commit d3ae8a938c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 11 deletions

View File

@ -1454,10 +1454,6 @@ class Config:
)
data = await store.async_load()
if data and "external_url" in data:
self._update(source=SOURCE_STORAGE, **data)
return
async def migrate_base_url(_: Event) -> None:
"""Migrate base_url to internal_url/external_url."""
if self.hass.config.api is None:
@ -1484,11 +1480,24 @@ class Config:
external_url=network.normalize_url(str(base_url))
)
# Try to migrate base_url to internal_url/external_url
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, migrate_base_url)
if data:
self._update(source=SOURCE_STORAGE, **data)
# Try to migrate base_url to internal_url/external_url
if "external_url" not in data:
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_START, migrate_base_url
)
self._update(
source=SOURCE_STORAGE,
latitude=data.get("latitude"),
longitude=data.get("longitude"),
elevation=data.get("elevation"),
unit_system=data.get("unit_system"),
location_name=data.get("location_name"),
time_zone=data.get("time_zone"),
external_url=data.get("external_url", _UNDEF),
internal_url=data.get("internal_url", _UNDEF),
)
async def async_store(self) -> None:
"""Store [homeassistant] core config."""

View File

@ -1294,17 +1294,17 @@ async def test_migration_base_url(hass, hass_storage):
with patch.object(hass.bus, "async_listen_once") as mock_listen:
# Empty config
await config.async_load()
assert len(mock_listen.mock_calls) == 1
assert len(mock_listen.mock_calls) == 0
# With just a name
stored["data"] = {"location_name": "Test Name"}
await config.async_load()
assert len(mock_listen.mock_calls) == 2
assert len(mock_listen.mock_calls) == 1
# With external url
stored["data"]["external_url"] = "https://example.com"
await config.async_load()
assert len(mock_listen.mock_calls) == 2
assert len(mock_listen.mock_calls) == 1
# Test that the event listener works
assert mock_listen.mock_calls[0][1][0] == EVENT_HOMEASSISTANT_START
@ -1319,3 +1319,14 @@ async def test_migration_base_url(hass, hass_storage):
hass.config.api = Mock(deprecated_base_url=internal)
await mock_listen.mock_calls[0][1][1](None)
assert config.internal_url == internal
async def test_additional_data_in_core_config(hass, hass_storage):
"""Test that we can handle additional data in core configuration."""
config = ha.Config(hass)
hass_storage[ha.CORE_STORAGE_KEY] = {
"version": 1,
"data": {"location_name": "Test Name", "additional_valid_key": "value"},
}
await config.async_load()
assert config.location_name == "Test Name"