-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Load HA core config from storage #23872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
502e41f
fb3bbe7
7edaa0c
81b4369
4e6e03a
69afb72
7e367d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,13 @@ | |
| ('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,40 @@ 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 data: | ||
| hac = hass.config | ||
| hac.config_source = SOURCE_STORAGE | ||
| hac.latitude = data['latitude'] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How will this handle users backrolling? If we decide to change the schema here, some of these keys might be invalid. If a user then tries backporting to a version before the schema change this will raise an exception - and since this is core code it could (I think) stop HA from starting up.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, how about validating the data with a schema, and rejecting it if it doesn't pass?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that could probably work |
||
| 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 +552,14 @@ async def async_process_ha_core_config( | |
| auth_conf, | ||
| mfa_conf)) | ||
|
|
||
| hac = hass.config | ||
| await async_load_ha_core_config(hass) | ||
|
|
||
| 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) | ||
| hac = hass.config | ||
|
|
||
| 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]]): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating a list is unnecessary here (https://twitter.com/raymondh/status/1125487457443532800). Admittedly, this is a micro-optimization though.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's even a pico-optimization ;)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean? You just have to remove the |
||
| hac.config_source = SOURCE_YAML | ||
|
|
||
| for key, attr in ((CONF_LATITUDE, 'latitude'), | ||
| (CONF_LONGITUDE, 'longitude'), | ||
|
|
@@ -533,7 +568,7 @@ def set_time_zone(time_zone_str: Optional[str]) -> None: | |
| 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 +626,7 @@ def set_time_zone(time_zone_str: Optional[str]) -> None: | |
| # If we miss some of the needed values, auto detect them | ||
| if None in (hac.latitude, hac.longitude, hac.units, | ||
|
emontnemery marked this conversation as resolved.
|
||
| 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 +649,7 @@ def set_time_zone(time_zone_str: Optional[str]) -> None: | |
| 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 +666,24 @@ def set_time_zone(time_zone_str: Optional[str]) -> None: | |
| ", ".join('{}: {}'.format(key, val) for key, val in discovered)) | ||
|
|
||
|
|
||
| async def async_store_ha_core_config(hass: HomeAssistant) -> None: | ||
|
emontnemery marked this conversation as resolved.
|
||
| """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.""" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.