Make core config source an enum (#61966)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Ville Skyttä 2021-12-19 19:02:52 +02:00 committed by GitHub
parent 1bd904b5b5
commit 1ec8619687
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 18 deletions

View File

@ -52,7 +52,12 @@ from homeassistant.const import (
TEMP_CELSIUS,
__version__,
)
from homeassistant.core import DOMAIN as CONF_CORE, SOURCE_YAML, HomeAssistant, callback
from homeassistant.core import (
DOMAIN as CONF_CORE,
ConfigSource,
HomeAssistant,
callback,
)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_per_platform, extract_domain_configs
import homeassistant.helpers.config_validation as cv
@ -542,7 +547,7 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: dict) -> Non
CONF_CURRENCY,
)
):
hac.config_source = SOURCE_YAML
hac.config_source = ConfigSource.YAML
for key, attr in (
(CONF_LATITUDE, "latitude"),

View File

@ -26,6 +26,7 @@ import voluptuous as vol
import yarl
from homeassistant import async_timeout_backcompat, block_async_io, loader, util
from homeassistant.backports.enum import StrEnum
from homeassistant.const import (
ATTR_DOMAIN,
ATTR_FRIENDLY_NAME,
@ -103,10 +104,20 @@ BLOCK_LOG_TIMEOUT = 60
# How long we wait for the result of a service call
SERVICE_CALL_LIMIT = 10 # seconds
# Source of core configuration
SOURCE_DISCOVERED = "discovered"
SOURCE_STORAGE = "storage"
SOURCE_YAML = "yaml"
class ConfigSource(StrEnum):
"""Source of core configuration."""
DEFAULT = "default"
DISCOVERED = "discovered"
STORAGE = "storage"
YAML = "yaml"
# SOURCE_* are deprecated as of Home Assistant 2022.2, use ConfigSource instead
SOURCE_DISCOVERED = ConfigSource.DISCOVERED.value
SOURCE_STORAGE = ConfigSource.STORAGE.value
SOURCE_YAML = ConfigSource.YAML.value
# How long to wait until things that run on startup have to finish.
TIMEOUT_EVENT_START = 15
@ -1557,7 +1568,7 @@ class Config:
self.external_url: str | None = None
self.currency: str = "EUR"
self.config_source: str = "default"
self.config_source: ConfigSource = ConfigSource.DEFAULT
# If True, pip install is skipped for requirements on startup
self.skip_pip: bool = False
@ -1676,7 +1687,7 @@ class Config:
def _update(
self,
*,
source: str,
source: ConfigSource,
latitude: float | None = None,
longitude: float | None = None,
elevation: int | None = None,
@ -1714,7 +1725,7 @@ class Config:
async def async_update(self, **kwargs: Any) -> None:
"""Update the configuration from a dictionary."""
self._update(source=SOURCE_STORAGE, **kwargs)
self._update(source=ConfigSource.STORAGE, **kwargs)
await self.async_store()
self.hass.bus.async_fire(EVENT_CORE_CONFIG_UPDATE, kwargs)
@ -1742,7 +1753,7 @@ class Config:
_LOGGER.warning("Invalid internal_url set. It's not allowed to have a path")
self._update(
source=SOURCE_STORAGE,
source=ConfigSource.STORAGE,
latitude=data.get("latitude"),
longitude=data.get("longitude"),
elevation=data.get("elevation"),

View File

@ -27,7 +27,7 @@ from homeassistant.const import (
CONF_UNIT_SYSTEM_METRIC,
__version__,
)
from homeassistant.core import SOURCE_STORAGE, HomeAssistantError
from homeassistant.core import ConfigSource, HomeAssistantError
from homeassistant.helpers import config_validation as cv
import homeassistant.helpers.check_config as check_config
from homeassistant.helpers.entity import Entity
@ -395,7 +395,7 @@ async def test_loading_configuration_from_storage(hass, hass_storage):
assert hass.config.currency == "EUR"
assert len(hass.config.allowlist_external_dirs) == 3
assert "/etc" in hass.config.allowlist_external_dirs
assert hass.config.config_source == SOURCE_STORAGE
assert hass.config.config_source is ConfigSource.STORAGE
async def test_loading_configuration_from_storage_with_yaml_only(hass, hass_storage):
@ -425,7 +425,7 @@ async def test_loading_configuration_from_storage_with_yaml_only(hass, hass_stor
assert len(hass.config.allowlist_external_dirs) == 3
assert "/etc" in hass.config.allowlist_external_dirs
assert hass.config.media_dirs == {"mymedia": "/usr"}
assert hass.config.config_source == SOURCE_STORAGE
assert hass.config.config_source is ConfigSource.STORAGE
async def test_updating_configuration(hass, hass_storage):
@ -486,7 +486,7 @@ async def test_override_stored_configuration(hass, hass_storage):
assert hass.config.time_zone == "Europe/Copenhagen"
assert len(hass.config.allowlist_external_dirs) == 3
assert "/etc" in hass.config.allowlist_external_dirs
assert hass.config.config_source == config_util.SOURCE_YAML
assert hass.config.config_source is ConfigSource.YAML
async def test_loading_configuration(hass):
@ -521,7 +521,7 @@ async def test_loading_configuration(hass):
assert "/etc" in hass.config.allowlist_external_dirs
assert "/usr" in hass.config.allowlist_external_dirs
assert hass.config.media_dirs == {"mymedia": "/usr"}
assert hass.config.config_source == config_util.SOURCE_YAML
assert hass.config.config_source is ConfigSource.YAML
assert hass.config.legacy_templates is True
assert hass.config.currency == "EUR"
@ -550,7 +550,7 @@ async def test_loading_configuration_temperature_unit(hass):
assert hass.config.time_zone == "America/New_York"
assert hass.config.external_url == "https://www.example.com"
assert hass.config.internal_url == "http://example.local"
assert hass.config.config_source == config_util.SOURCE_YAML
assert hass.config.config_source is ConfigSource.YAML
assert hass.config.currency == "EUR"

View File

@ -902,7 +902,7 @@ def test_config_defaults():
assert config.time_zone == "UTC"
assert config.internal_url is None
assert config.external_url is None
assert config.config_source == "default"
assert config.config_source is ha.ConfigSource.DEFAULT
assert config.skip_pip is False
assert config.components == set()
assert config.api is None
@ -948,7 +948,7 @@ def test_config_as_dict():
"allowlist_external_dirs": set(),
"allowlist_external_urls": set(),
"version": __version__,
"config_source": "default",
"config_source": ha.ConfigSource.DEFAULT,
"safe_mode": False,
"state": "RUNNING",
"external_url": None,