mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 07:17:12 +00:00
Refactor access to ConfigStore (#80467)
* Refactore access to ConfigStore * Make tests async * Reset config during test
This commit is contained in:
parent
9680cd267f
commit
a26b3e7a34
@ -1789,6 +1789,8 @@ class Config:
|
|||||||
"""Initialize a new config object."""
|
"""Initialize a new config object."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
|
|
||||||
|
self._store = self._ConfigStore(self.hass)
|
||||||
|
|
||||||
self.latitude: float = 0
|
self.latitude: float = 0
|
||||||
self.longitude: float = 0
|
self.longitude: float = 0
|
||||||
self.elevation: int = 0
|
self.elevation: int = 0
|
||||||
@ -1957,14 +1959,12 @@ class Config:
|
|||||||
async def async_update(self, **kwargs: Any) -> None:
|
async def async_update(self, **kwargs: Any) -> None:
|
||||||
"""Update the configuration from a dictionary."""
|
"""Update the configuration from a dictionary."""
|
||||||
self._update(source=ConfigSource.STORAGE, **kwargs)
|
self._update(source=ConfigSource.STORAGE, **kwargs)
|
||||||
await self.async_store()
|
await self._async_store()
|
||||||
self.hass.bus.async_fire(EVENT_CORE_CONFIG_UPDATE, kwargs)
|
self.hass.bus.async_fire(EVENT_CORE_CONFIG_UPDATE, kwargs)
|
||||||
|
|
||||||
async def async_load(self) -> None:
|
async def async_load(self) -> None:
|
||||||
"""Load [homeassistant] core config."""
|
"""Load [homeassistant] core config."""
|
||||||
store = self._ConfigStore(self.hass)
|
if not (data := await self._store.async_load()):
|
||||||
|
|
||||||
if not (data := await store.async_load()):
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# In 2021.9 we fixed validation to disallow a path (because that's never correct)
|
# 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"),
|
currency=data.get("currency"),
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_store(self) -> None:
|
async def _async_store(self) -> None:
|
||||||
"""Store [homeassistant] core config."""
|
"""Store [homeassistant] core config."""
|
||||||
data = {
|
data = {
|
||||||
"latitude": self.latitude,
|
"latitude": self.latitude,
|
||||||
@ -2010,8 +2010,7 @@ class Config:
|
|||||||
"currency": self.currency,
|
"currency": self.currency,
|
||||||
}
|
}
|
||||||
|
|
||||||
store = self._ConfigStore(self.hass)
|
await self._store.async_save(data)
|
||||||
await store.async_save(data)
|
|
||||||
|
|
||||||
# Circular dependency prevents us from generating the class at top level
|
# Circular dependency prevents us from generating the class at top level
|
||||||
# pylint: disable-next=import-outside-toplevel
|
# pylint: disable-next=import-outside-toplevel
|
||||||
|
@ -925,7 +925,7 @@ async def test_serviceregistry_callback_service_raise_exception(hass):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
|
||||||
def test_config_defaults():
|
async def test_config_defaults():
|
||||||
"""Test config defaults."""
|
"""Test config defaults."""
|
||||||
hass = Mock()
|
hass = Mock()
|
||||||
config = ha.Config(hass)
|
config = ha.Config(hass)
|
||||||
@ -950,21 +950,21 @@ def test_config_defaults():
|
|||||||
assert config.currency == "EUR"
|
assert config.currency == "EUR"
|
||||||
|
|
||||||
|
|
||||||
def test_config_path_with_file():
|
async def test_config_path_with_file():
|
||||||
"""Test get_config_path method."""
|
"""Test get_config_path method."""
|
||||||
config = ha.Config(None)
|
config = ha.Config(None)
|
||||||
config.config_dir = "/test/ha-config"
|
config.config_dir = "/test/ha-config"
|
||||||
assert config.path("test.conf") == "/test/ha-config/test.conf"
|
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."""
|
"""Test get_config_path method."""
|
||||||
config = ha.Config(None)
|
config = ha.Config(None)
|
||||||
config.config_dir = "/test/ha-config"
|
config.config_dir = "/test/ha-config"
|
||||||
assert config.path("dir", "test.conf") == "/test/ha-config/dir/test.conf"
|
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."""
|
"""Test as dict."""
|
||||||
config = ha.Config(None)
|
config = ha.Config(None)
|
||||||
config.config_dir = "/test/ha-config"
|
config.config_dir = "/test/ha-config"
|
||||||
@ -994,7 +994,7 @@ def test_config_as_dict():
|
|||||||
assert expected == 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."""
|
"""Test is_allowed_path method."""
|
||||||
config = ha.Config(None)
|
config = ha.Config(None)
|
||||||
with TemporaryDirectory() as tmp_dir:
|
with TemporaryDirectory() as tmp_dir:
|
||||||
@ -1026,7 +1026,7 @@ def test_config_is_allowed_path():
|
|||||||
config.is_allowed_path(None)
|
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."""
|
"""Test is_allowed_external_url method."""
|
||||||
config = ha.Config(None)
|
config = ha.Config(None)
|
||||||
config.allowlist_external_urls = [
|
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):
|
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)
|
config = ha.Config(hass)
|
||||||
|
|
||||||
hass_storage[ha.CORE_STORAGE_KEY] = {
|
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 external_url set" not in caplog.text
|
||||||
assert "Invalid internal_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] = {
|
hass_storage[ha.CORE_STORAGE_KEY] = {
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"data": {
|
"data": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user