Sort some code in core_config (#129388)

This commit is contained in:
Erik Montnemery 2024-10-29 17:26:08 +01:00 committed by GitHub
parent c8818bcce3
commit ca3d13b5cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -354,33 +354,33 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: dict) -> Non
if any( if any(
k in config k in config
for k in ( for k in (
CONF_COUNTRY,
CONF_CURRENCY,
CONF_ELEVATION,
CONF_EXTERNAL_URL,
CONF_INTERNAL_URL,
CONF_LANGUAGE,
CONF_LATITUDE, CONF_LATITUDE,
CONF_LONGITUDE, CONF_LONGITUDE,
CONF_NAME, CONF_NAME,
CONF_ELEVATION, CONF_RADIUS,
CONF_TIME_ZONE, CONF_TIME_ZONE,
CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM,
CONF_EXTERNAL_URL,
CONF_INTERNAL_URL,
CONF_CURRENCY,
CONF_COUNTRY,
CONF_LANGUAGE,
CONF_RADIUS,
) )
): ):
hac.config_source = ConfigSource.YAML hac.config_source = ConfigSource.YAML
for key, attr in ( for key, attr in (
(CONF_COUNTRY, "country"),
(CONF_CURRENCY, "currency"),
(CONF_ELEVATION, "elevation"),
(CONF_EXTERNAL_URL, "external_url"),
(CONF_INTERNAL_URL, "internal_url"),
(CONF_LANGUAGE, "language"),
(CONF_LATITUDE, "latitude"), (CONF_LATITUDE, "latitude"),
(CONF_LONGITUDE, "longitude"), (CONF_LONGITUDE, "longitude"),
(CONF_NAME, "location_name"),
(CONF_ELEVATION, "elevation"),
(CONF_INTERNAL_URL, "internal_url"),
(CONF_EXTERNAL_URL, "external_url"),
(CONF_MEDIA_DIRS, "media_dirs"), (CONF_MEDIA_DIRS, "media_dirs"),
(CONF_CURRENCY, "currency"), (CONF_NAME, "location_name"),
(CONF_COUNTRY, "country"),
(CONF_LANGUAGE, "language"),
(CONF_RADIUS, "radius"), (CONF_RADIUS, "radius"),
): ):
if key in config: if key in config:
@ -647,36 +647,36 @@ class Config:
return False return False
def as_dict(self) -> dict[str, Any]: def as_dict(self) -> dict[str, Any]:
"""Create a dictionary representation of the configuration. """Return a dictionary representation of the configuration.
Async friendly. Async friendly.
""" """
allowlist_external_dirs = list(self.allowlist_external_dirs) allowlist_external_dirs = list(self.allowlist_external_dirs)
return { return {
"latitude": self.latitude,
"longitude": self.longitude,
"elevation": self.elevation,
"unit_system": self.units.as_dict(),
"location_name": self.location_name,
"time_zone": self.time_zone,
"components": list(self.components),
"config_dir": self.config_dir,
# legacy, backwards compat
"whitelist_external_dirs": allowlist_external_dirs,
"allowlist_external_dirs": allowlist_external_dirs, "allowlist_external_dirs": allowlist_external_dirs,
"allowlist_external_urls": list(self.allowlist_external_urls), "allowlist_external_urls": list(self.allowlist_external_urls),
"version": __version__, "components": list(self.components),
"config_dir": self.config_dir,
"config_source": self.config_source, "config_source": self.config_source,
"recovery_mode": self.recovery_mode, "country": self.country,
"state": self.hass.state.value, "currency": self.currency,
"debug": self.debug,
"elevation": self.elevation,
"external_url": self.external_url, "external_url": self.external_url,
"internal_url": self.internal_url, "internal_url": self.internal_url,
"currency": self.currency,
"country": self.country,
"language": self.language, "language": self.language,
"safe_mode": self.safe_mode, "latitude": self.latitude,
"debug": self.debug, "location_name": self.location_name,
"longitude": self.longitude,
"radius": self.radius, "radius": self.radius,
"recovery_mode": self.recovery_mode,
"safe_mode": self.safe_mode,
"state": self.hass.state.value,
"time_zone": self.time_zone,
"unit_system": self.units.as_dict(),
"version": __version__,
# legacy, backwards compat
"whitelist_external_dirs": allowlist_external_dirs,
} }
async def async_set_time_zone(self, time_zone_str: str) -> None: async def async_set_time_zone(self, time_zone_str: str) -> None:
@ -710,49 +710,49 @@ class Config:
async def _async_update( async def _async_update(
self, self,
*, *,
source: ConfigSource, country: str | UndefinedType | None = UNDEFINED,
latitude: float | None = None, currency: str | None = None,
longitude: float | None = None,
elevation: int | None = None, elevation: int | None = None,
unit_system: str | None = None,
location_name: str | None = None,
time_zone: str | None = None,
external_url: str | UndefinedType | None = UNDEFINED, external_url: str | UndefinedType | None = UNDEFINED,
internal_url: str | UndefinedType | None = UNDEFINED, internal_url: str | UndefinedType | None = UNDEFINED,
currency: str | None = None,
country: str | UndefinedType | None = UNDEFINED,
language: str | None = None, language: str | None = None,
latitude: float | None = None,
location_name: str | None = None,
longitude: float | None = None,
radius: int | None = None, radius: int | None = None,
source: ConfigSource,
time_zone: str | None = None,
unit_system: str | None = None,
) -> None: ) -> None:
"""Update the configuration from a dictionary.""" """Update the configuration from a dictionary."""
self.config_source = source self.config_source = source
if latitude is not None: if country is not UNDEFINED:
self.latitude = latitude self.country = country
if longitude is not None: if currency is not None:
self.longitude = longitude self.currency = currency
if elevation is not None: if elevation is not None:
self.elevation = elevation self.elevation = elevation
if external_url is not UNDEFINED:
self.external_url = external_url
if internal_url is not UNDEFINED:
self.internal_url = internal_url
if language is not None:
self.language = language
if latitude is not None:
self.latitude = latitude
if location_name is not None:
self.location_name = location_name
if longitude is not None:
self.longitude = longitude
if radius is not None:
self.radius = radius
if time_zone is not None:
await self.async_set_time_zone(time_zone)
if unit_system is not None: if unit_system is not None:
try: try:
self.units = get_unit_system(unit_system) self.units = get_unit_system(unit_system)
except ValueError: except ValueError:
self.units = METRIC_SYSTEM self.units = METRIC_SYSTEM
if location_name is not None:
self.location_name = location_name
if time_zone is not None:
await self.async_set_time_zone(time_zone)
if external_url is not UNDEFINED:
self.external_url = external_url
if internal_url is not UNDEFINED:
self.internal_url = internal_url
if currency is not None:
self.currency = currency
if country is not UNDEFINED:
self.country = country
if language is not None:
self.language = language
if radius is not None:
self.radius = radius
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."""