Refactor access to ConfigStore (#80467)

* Refactore access to ConfigStore

* Make tests async

* Reset config during test
This commit is contained in:
epenet 2022-10-17 13:59:04 +02:00 committed by GitHub
parent 9680cd267f
commit a26b3e7a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 14 deletions

View File

@ -1789,6 +1789,8 @@ class Config:
"""Initialize a new config object."""
self.hass = hass
self._store = self._ConfigStore(self.hass)
self.latitude: float = 0
self.longitude: float = 0
self.elevation: int = 0
@ -1957,14 +1959,12 @@ class Config:
async def async_update(self, **kwargs: Any) -> None:
"""Update the configuration from a dictionary."""
self._update(source=ConfigSource.STORAGE, **kwargs)
await self.async_store()
await self._async_store()
self.hass.bus.async_fire(EVENT_CORE_CONFIG_UPDATE, kwargs)
async def async_load(self) -> None:
"""Load [homeassistant] core config."""
store = self._ConfigStore(self.hass)
if not (data := await store.async_load()):
if not (data := await self._store.async_load()):
return
# In 2021.9 we fixed validation to disallow a path (because that's never correct)
@ -1994,7 +1994,7 @@ class Config:
currency=data.get("currency"),
)
async def async_store(self) -> None:
async def _async_store(self) -> None:
"""Store [homeassistant] core config."""
data = {
"latitude": self.latitude,
@ -2010,8 +2010,7 @@ class Config:
"currency": self.currency,
}
store = self._ConfigStore(self.hass)
await store.async_save(data)
await self._store.async_save(data)
# Circular dependency prevents us from generating the class at top level
# pylint: disable-next=import-outside-toplevel

View File

@ -925,7 +925,7 @@ async def test_serviceregistry_callback_service_raise_exception(hass):
await hass.async_block_till_done()
def test_config_defaults():
async def test_config_defaults():
"""Test config defaults."""
hass = Mock()
config = ha.Config(hass)
@ -950,21 +950,21 @@ def test_config_defaults():
assert config.currency == "EUR"
def test_config_path_with_file():
async def test_config_path_with_file():
"""Test get_config_path method."""
config = ha.Config(None)
config.config_dir = "/test/ha-config"
assert config.path("test.conf") == "/test/ha-config/test.conf"
def test_config_path_with_dir_and_file():
async def test_config_path_with_dir_and_file():
"""Test get_config_path method."""
config = ha.Config(None)
config.config_dir = "/test/ha-config"
assert config.path("dir", "test.conf") == "/test/ha-config/dir/test.conf"
def test_config_as_dict():
async def test_config_as_dict():
"""Test as dict."""
config = ha.Config(None)
config.config_dir = "/test/ha-config"
@ -994,7 +994,7 @@ def test_config_as_dict():
assert expected == config.as_dict()
def test_config_is_allowed_path():
async def test_config_is_allowed_path():
"""Test is_allowed_path method."""
config = ha.Config(None)
with TemporaryDirectory() as tmp_dir:
@ -1026,7 +1026,7 @@ def test_config_is_allowed_path():
config.is_allowed_path(None)
def test_config_is_allowed_external_url():
async def test_config_is_allowed_external_url():
"""Test is_allowed_external_url method."""
config = ha.Config(None)
config.allowlist_external_urls = [
@ -1273,7 +1273,7 @@ async def test_additional_data_in_core_config(hass, hass_storage):
async def test_incorrect_internal_external_url(hass, hass_storage, caplog):
"""Test that we warn when detecting invalid internal/extenral url."""
"""Test that we warn when detecting invalid internal/external url."""
config = ha.Config(hass)
hass_storage[ha.CORE_STORAGE_KEY] = {
@ -1287,6 +1287,8 @@ async def test_incorrect_internal_external_url(hass, hass_storage, caplog):
assert "Invalid external_url set" not in caplog.text
assert "Invalid internal_url set" not in caplog.text
config = ha.Config(hass)
hass_storage[ha.CORE_STORAGE_KEY] = {
"version": 1,
"data": {