mirror of
https://github.com/home-assistant/core.git
synced 2025-12-25 05:26:47 +00:00
Require core config detection to be triggerd manually (#24019)
* Detect core config * Remove elevation * Lint * Lint * Fix type
This commit is contained in:
@@ -18,13 +18,13 @@ from homeassistant.auth import providers as auth_providers,\
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_ASSUMED_STATE,
|
||||
CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, CONF_PACKAGES, CONF_UNIT_SYSTEM,
|
||||
CONF_TIME_ZONE, CONF_ELEVATION, CONF_UNIT_SYSTEM_METRIC,
|
||||
CONF_TIME_ZONE, CONF_ELEVATION,
|
||||
CONF_UNIT_SYSTEM_IMPERIAL, CONF_TEMPERATURE_UNIT, TEMP_CELSIUS,
|
||||
__version__, CONF_CUSTOMIZE, CONF_CUSTOMIZE_DOMAIN, CONF_CUSTOMIZE_GLOB,
|
||||
CONF_WHITELIST_EXTERNAL_DIRS, CONF_AUTH_PROVIDERS, CONF_AUTH_MFA_MODULES,
|
||||
CONF_TYPE, CONF_ID)
|
||||
from homeassistant.core import (
|
||||
DOMAIN as CONF_CORE, SOURCE_DISCOVERED, SOURCE_YAML, HomeAssistant,
|
||||
DOMAIN as CONF_CORE, SOURCE_YAML, HomeAssistant,
|
||||
callback)
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.loader import (
|
||||
@@ -32,7 +32,6 @@ from homeassistant.loader import (
|
||||
)
|
||||
from homeassistant.util.yaml import load_yaml, SECRET_YAML
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.util import location as loc_util
|
||||
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM
|
||||
from homeassistant.helpers.entity_values import EntityValues
|
||||
from homeassistant.helpers import config_per_platform, extract_domain_configs
|
||||
@@ -52,22 +51,6 @@ FILE_MIGRATION = (
|
||||
('ios.conf', '.ios.conf'),
|
||||
)
|
||||
|
||||
DEFAULT_CORE_CONFIG = (
|
||||
# Tuples (attribute, default, auto detect property, description)
|
||||
(CONF_NAME, 'Home', None, 'Name of the location where Home Assistant is '
|
||||
'running'),
|
||||
(CONF_LATITUDE, 0, 'latitude', 'Location required to calculate the time'
|
||||
' the sun rises and sets'),
|
||||
(CONF_LONGITUDE, 0, 'longitude', None),
|
||||
(CONF_ELEVATION, 0, None, 'Impacts weather/sunrise data'
|
||||
' (altitude above sea level in meters)'),
|
||||
(CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM_METRIC, None,
|
||||
'{} for Metric, {} for Imperial'.format(CONF_UNIT_SYSTEM_METRIC,
|
||||
CONF_UNIT_SYSTEM_IMPERIAL)),
|
||||
(CONF_TIME_ZONE, 'UTC', 'time_zone', 'Pick yours from here: http://en.wiki'
|
||||
'pedia.org/wiki/List_of_tz_database_time_zones'),
|
||||
(CONF_CUSTOMIZE, '!include customize.yaml', None, 'Customization file'),
|
||||
) # type: Tuple[Tuple[str, Any, Any, Optional[str]], ...]
|
||||
DEFAULT_CONFIG = """
|
||||
# Configure a default setup of Home Assistant (frontend, api, etc)
|
||||
default_config:
|
||||
@@ -207,8 +190,7 @@ def get_default_config_dir() -> str:
|
||||
return os.path.join(data_dir, CONFIG_DIR_NAME) # type: ignore
|
||||
|
||||
|
||||
async def async_ensure_config_exists(hass: HomeAssistant, config_dir: str,
|
||||
detect_location: bool = True)\
|
||||
async def async_ensure_config_exists(hass: HomeAssistant, config_dir: str) \
|
||||
-> Optional[str]:
|
||||
"""Ensure a configuration file exists in given configuration directory.
|
||||
|
||||
@@ -220,49 +202,22 @@ async def async_ensure_config_exists(hass: HomeAssistant, config_dir: str,
|
||||
if config_path is None:
|
||||
print("Unable to find configuration. Creating default one in",
|
||||
config_dir)
|
||||
config_path = await async_create_default_config(
|
||||
hass, config_dir, detect_location)
|
||||
config_path = await async_create_default_config(hass, config_dir)
|
||||
|
||||
return config_path
|
||||
|
||||
|
||||
async def async_create_default_config(
|
||||
hass: HomeAssistant, config_dir: str, detect_location: bool = True
|
||||
) -> Optional[str]:
|
||||
async def async_create_default_config(hass: HomeAssistant, config_dir: str) \
|
||||
-> Optional[str]:
|
||||
"""Create a default configuration file in given configuration directory.
|
||||
|
||||
Return path to new config file if success, None if failed.
|
||||
This method needs to run in an executor.
|
||||
"""
|
||||
info = {attr: default for attr, default, _, _ in DEFAULT_CORE_CONFIG}
|
||||
|
||||
if detect_location:
|
||||
session = hass.helpers.aiohttp_client.async_get_clientsession()
|
||||
location_info = await loc_util.async_detect_location_info(session)
|
||||
else:
|
||||
location_info = None
|
||||
|
||||
if location_info:
|
||||
if location_info.use_metric:
|
||||
info[CONF_UNIT_SYSTEM] = CONF_UNIT_SYSTEM_METRIC
|
||||
else:
|
||||
info[CONF_UNIT_SYSTEM] = CONF_UNIT_SYSTEM_IMPERIAL
|
||||
|
||||
for attr, default, prop, _ in DEFAULT_CORE_CONFIG:
|
||||
if prop is None:
|
||||
continue
|
||||
info[attr] = getattr(location_info, prop) or default
|
||||
|
||||
if location_info.latitude and location_info.longitude:
|
||||
info[CONF_ELEVATION] = await loc_util.async_get_elevation(
|
||||
session, location_info.latitude, location_info.longitude)
|
||||
|
||||
return await hass.async_add_executor_job(
|
||||
_write_default_config, config_dir, info
|
||||
)
|
||||
return await hass.async_add_executor_job(_write_default_config, config_dir)
|
||||
|
||||
|
||||
def _write_default_config(config_dir: str, info: Dict)\
|
||||
def _write_default_config(config_dir: str)\
|
||||
-> Optional[str]:
|
||||
"""Write the default config."""
|
||||
from homeassistant.components.config.group import (
|
||||
@@ -271,8 +226,6 @@ def _write_default_config(config_dir: str, info: Dict)\
|
||||
CONFIG_PATH as AUTOMATION_CONFIG_PATH)
|
||||
from homeassistant.components.config.script import (
|
||||
CONFIG_PATH as SCRIPT_CONFIG_PATH)
|
||||
from homeassistant.components.config.customize import (
|
||||
CONFIG_PATH as CUSTOMIZE_CONFIG_PATH)
|
||||
|
||||
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)
|
||||
secret_path = os.path.join(config_dir, SECRET_YAML)
|
||||
@@ -280,21 +233,11 @@ def _write_default_config(config_dir: str, info: Dict)\
|
||||
group_yaml_path = os.path.join(config_dir, GROUP_CONFIG_PATH)
|
||||
automation_yaml_path = os.path.join(config_dir, AUTOMATION_CONFIG_PATH)
|
||||
script_yaml_path = os.path.join(config_dir, SCRIPT_CONFIG_PATH)
|
||||
customize_yaml_path = os.path.join(config_dir, CUSTOMIZE_CONFIG_PATH)
|
||||
|
||||
# Writing files with YAML does not create the most human readable results
|
||||
# So we're hard coding a YAML template.
|
||||
try:
|
||||
with open(config_path, 'wt') as config_file:
|
||||
config_file.write("homeassistant:\n")
|
||||
|
||||
for attr, _, _, description in DEFAULT_CORE_CONFIG:
|
||||
if info[attr] is None:
|
||||
continue
|
||||
elif description:
|
||||
config_file.write(" # {}\n".format(description))
|
||||
config_file.write(" {}: {}\n".format(attr, info[attr]))
|
||||
|
||||
config_file.write(DEFAULT_CONFIG)
|
||||
|
||||
with open(secret_path, 'wt') as secret_file:
|
||||
@@ -312,9 +255,6 @@ def _write_default_config(config_dir: str, info: Dict)\
|
||||
with open(script_yaml_path, 'wt'):
|
||||
pass
|
||||
|
||||
with open(customize_yaml_path, 'wt'):
|
||||
pass
|
||||
|
||||
return config_path
|
||||
|
||||
except IOError:
|
||||
@@ -576,55 +516,6 @@ async def async_process_ha_core_config(
|
||||
"with '%s: %s'", CONF_TEMPERATURE_UNIT, unit,
|
||||
CONF_UNIT_SYSTEM, hac.units.name)
|
||||
|
||||
# Shortcut if no auto-detection necessary
|
||||
if None not in (hac.latitude, hac.longitude, hac.units,
|
||||
hac.time_zone, hac.elevation):
|
||||
return
|
||||
|
||||
discovered = [] # type: List[Tuple[str, Any]]
|
||||
|
||||
# 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()
|
||||
)
|
||||
|
||||
if info is None:
|
||||
_LOGGER.error("Could not detect location information")
|
||||
return
|
||||
|
||||
if hac.latitude is None and hac.longitude is None:
|
||||
hac.latitude, hac.longitude = (info.latitude, info.longitude)
|
||||
discovered.append(('latitude', hac.latitude))
|
||||
discovered.append(('longitude', hac.longitude))
|
||||
|
||||
if hac.units is None:
|
||||
hac.units = METRIC_SYSTEM if info.use_metric else IMPERIAL_SYSTEM
|
||||
discovered.append((CONF_UNIT_SYSTEM, hac.units.name))
|
||||
|
||||
if hac.location_name is None:
|
||||
hac.location_name = info.city
|
||||
discovered.append(('name', info.city))
|
||||
|
||||
if hac.time_zone is None:
|
||||
hac.set_time_zone(info.time_zone)
|
||||
discovered.append(('time_zone', info.time_zone))
|
||||
|
||||
if hac.elevation is None and hac.latitude is not None and \
|
||||
hac.longitude is not None:
|
||||
elevation = await loc_util.async_get_elevation(
|
||||
hass.helpers.aiohttp_client.async_get_clientsession(),
|
||||
hac.latitude, hac.longitude)
|
||||
hac.elevation = elevation
|
||||
discovered.append(('elevation', elevation))
|
||||
|
||||
if discovered:
|
||||
_LOGGER.warning(
|
||||
"Incomplete core configuration. Auto detected %s",
|
||||
", ".join('{}: {}'.format(key, val) for key, val in discovered))
|
||||
|
||||
|
||||
def _log_pkg_error(
|
||||
package: str, component: str, config: Dict, message: str) -> None:
|
||||
|
||||
Reference in New Issue
Block a user