Load HA core config from storage (#23872)
* Load HA core config from storage * Tweak * Lint, review comments * Fix test * Add tests * Lint * Address comments
This commit is contained in:
parent
9be384690a
commit
c2fc8a0d61
4 changed files with 137 additions and 15 deletions
|
@ -50,6 +50,13 @@ FILE_MIGRATION = (
|
|||
('ios.conf', '.ios.conf'),
|
||||
)
|
||||
|
||||
CORE_STORAGE_KEY = 'homeassistant.core_config'
|
||||
CORE_STORAGE_VERSION = 1
|
||||
|
||||
SOURCE_DISCOVERED = 'discovered'
|
||||
SOURCE_STORAGE = 'storage'
|
||||
SOURCE_YAML = 'yaml'
|
||||
|
||||
DEFAULT_CORE_CONFIG = (
|
||||
# Tuples (attribute, default, auto detect property, description)
|
||||
(CONF_NAME, 'Home', None, 'Name of the location where Home Assistant is '
|
||||
|
@ -473,6 +480,42 @@ def _format_config_error(ex: vol.Invalid, domain: str, config: Dict) -> str:
|
|||
return message
|
||||
|
||||
|
||||
def _set_time_zone(hass: HomeAssistant, time_zone_str: Optional[str]) -> None:
|
||||
"""Help to set the time zone."""
|
||||
if time_zone_str is None:
|
||||
return
|
||||
|
||||
time_zone = date_util.get_time_zone(time_zone_str)
|
||||
|
||||
if time_zone:
|
||||
hass.config.time_zone = time_zone
|
||||
date_util.set_default_time_zone(time_zone)
|
||||
else:
|
||||
_LOGGER.error("Received invalid time zone %s", time_zone_str)
|
||||
|
||||
|
||||
async def async_load_ha_core_config(hass: HomeAssistant) -> None:
|
||||
"""Store [homeassistant] core config."""
|
||||
store = hass.helpers.storage.Store(CORE_STORAGE_VERSION, CORE_STORAGE_KEY,
|
||||
private=True)
|
||||
data = await store.async_load()
|
||||
if not data:
|
||||
return
|
||||
|
||||
hac = hass.config
|
||||
hac.config_source = SOURCE_STORAGE
|
||||
hac.latitude = data['latitude']
|
||||
hac.longitude = data['longitude']
|
||||
hac.elevation = data['elevation']
|
||||
unit_system = data['unit_system']
|
||||
if unit_system == CONF_UNIT_SYSTEM_IMPERIAL:
|
||||
hac.units = IMPERIAL_SYSTEM
|
||||
else:
|
||||
hac.units = METRIC_SYSTEM
|
||||
hac.location_name = data['location_name']
|
||||
_set_time_zone(hass, data['time_zone'])
|
||||
|
||||
|
||||
async def async_process_ha_core_config(
|
||||
hass: HomeAssistant, config: Dict,
|
||||
api_password: Optional[str] = None,
|
||||
|
@ -511,20 +554,14 @@ async def async_process_ha_core_config(
|
|||
auth_conf,
|
||||
mfa_conf))
|
||||
|
||||
await async_load_ha_core_config(hass)
|
||||
|
||||
hac = hass.config
|
||||
|
||||
def set_time_zone(time_zone_str: Optional[str]) -> None:
|
||||
"""Help to set the time zone."""
|
||||
if time_zone_str is None:
|
||||
return
|
||||
|
||||
time_zone = date_util.get_time_zone(time_zone_str)
|
||||
|
||||
if time_zone:
|
||||
hac.time_zone = time_zone
|
||||
date_util.set_default_time_zone(time_zone)
|
||||
else:
|
||||
_LOGGER.error("Received invalid time zone %s", time_zone_str)
|
||||
if any([k in config for k in [
|
||||
CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, CONF_ELEVATION,
|
||||
CONF_TIME_ZONE, CONF_UNIT_SYSTEM]]):
|
||||
hac.config_source = SOURCE_YAML
|
||||
|
||||
for key, attr in ((CONF_LATITUDE, 'latitude'),
|
||||
(CONF_LONGITUDE, 'longitude'),
|
||||
|
@ -533,7 +570,7 @@ async def async_process_ha_core_config(
|
|||
if key in config:
|
||||
setattr(hac, attr, config[key])
|
||||
|
||||
set_time_zone(config.get(CONF_TIME_ZONE))
|
||||
_set_time_zone(hass, config.get(CONF_TIME_ZONE))
|
||||
|
||||
# Init whitelist external dir
|
||||
hac.whitelist_external_dirs = {hass.config.path('www')}
|
||||
|
@ -591,6 +628,7 @@ async def async_process_ha_core_config(
|
|||
# If we miss some of the needed values, auto detect them
|
||||
if None in (hac.latitude, hac.longitude, hac.units,
|
||||
hac.time_zone):
|
||||
hac.config_source = SOURCE_DISCOVERED
|
||||
info = await loc_util.async_detect_location_info(
|
||||
hass.helpers.aiohttp_client.async_get_clientsession()
|
||||
)
|
||||
|
@ -613,7 +651,7 @@ async def async_process_ha_core_config(
|
|||
discovered.append(('name', info.city))
|
||||
|
||||
if hac.time_zone is None:
|
||||
set_time_zone(info.time_zone)
|
||||
_set_time_zone(hass, info.time_zone)
|
||||
discovered.append(('time_zone', info.time_zone))
|
||||
|
||||
if hac.elevation is None and hac.latitude is not None and \
|
||||
|
@ -630,6 +668,24 @@ async def async_process_ha_core_config(
|
|||
", ".join('{}: {}'.format(key, val) for key, val in discovered))
|
||||
|
||||
|
||||
async def async_store_ha_core_config(hass: HomeAssistant) -> None:
|
||||
"""Store [homeassistant] core config."""
|
||||
config = hass.config.as_dict()
|
||||
|
||||
data = {
|
||||
'latitude': config['latitude'],
|
||||
'longitude': config['longitude'],
|
||||
'elevation': config['elevation'],
|
||||
'unit_system': hass.config.units.name,
|
||||
'location_name': config['location_name'],
|
||||
'time_zone': config['time_zone'],
|
||||
}
|
||||
|
||||
store = hass.helpers.storage.Store(CORE_STORAGE_VERSION, CORE_STORAGE_KEY,
|
||||
private=True)
|
||||
await store.async_save(data)
|
||||
|
||||
|
||||
def _log_pkg_error(
|
||||
package: str, component: str, config: Dict, message: str) -> None:
|
||||
"""Log an error while merging packages."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue