2016-03-07 23:06:04 +00:00
|
|
|
"""Module to help with parsing and generating configuration files."""
|
2017-01-14 06:01:47 +00:00
|
|
|
from collections import OrderedDict
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-07-14 02:26:21 +00:00
|
|
|
# pylint: disable=no-name-in-module
|
2019-06-08 15:19:00 +00:00
|
|
|
from distutils.version import LooseVersion # pylint: disable=import-error
|
2015-04-26 17:05:01 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2017-02-08 17:17:52 +00:00
|
|
|
import re
|
2016-06-27 16:02:45 +00:00
|
|
|
import shutil
|
2018-09-11 09:21:48 +00:00
|
|
|
from typing import ( # noqa: F401 pylint: disable=unused-import
|
2019-07-31 19:25:30 +00:00
|
|
|
Any,
|
|
|
|
Tuple,
|
|
|
|
Optional,
|
|
|
|
Dict,
|
|
|
|
List,
|
|
|
|
Union,
|
|
|
|
Callable,
|
|
|
|
Sequence,
|
|
|
|
Set,
|
|
|
|
)
|
2018-07-23 08:24:39 +00:00
|
|
|
from types import ModuleType
|
2016-03-28 01:48:51 +00:00
|
|
|
import voluptuous as vol
|
2017-03-01 04:33:19 +00:00
|
|
|
from voluptuous.humanize import humanize_error
|
2015-04-26 17:05:01 +00:00
|
|
|
|
2018-05-01 16:20:41 +00:00
|
|
|
from homeassistant import auth
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.auth import (
|
|
|
|
providers as auth_providers,
|
|
|
|
mfa_modules as auth_mfa_modules,
|
|
|
|
)
|
2015-04-26 17:05:01 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
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_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,
|
2019-04-14 14:23:01 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.core import DOMAIN as CONF_CORE, SOURCE_YAML, HomeAssistant, callback
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2019-08-07 22:35:50 +00:00
|
|
|
from homeassistant.loader import Integration, IntegrationNotFound
|
|
|
|
from homeassistant.requirements import (
|
|
|
|
async_get_integration_with_requirements,
|
|
|
|
RequirementsNotFound,
|
|
|
|
)
|
2017-10-05 16:10:29 +00:00
|
|
|
from homeassistant.util.yaml import load_yaml, SECRET_YAML
|
2019-06-03 19:37:27 +00:00
|
|
|
from homeassistant.util.package import is_docker_env
|
2016-03-28 01:48:51 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-08-09 03:42:25 +00:00
|
|
|
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM
|
2017-02-16 03:47:30 +00:00
|
|
|
from homeassistant.helpers.entity_values import EntityValues
|
2017-03-01 04:33:19 +00:00
|
|
|
from homeassistant.helpers import config_per_platform, extract_domain_configs
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_PERSISTENT_ERRORS = "bootstrap_persistent_errors"
|
2018-01-12 14:29:58 +00:00
|
|
|
RE_YAML_ERROR = re.compile(r"homeassistant\.util\.yaml")
|
|
|
|
RE_ASCII = re.compile(r"\033\[[^m]*m")
|
2019-07-31 19:25:30 +00:00
|
|
|
HA_COMPONENT_URL = "[{}](https://home-assistant.io/components/{}/)"
|
|
|
|
YAML_CONFIG_FILE = "configuration.yaml"
|
|
|
|
VERSION_FILE = ".HA_VERSION"
|
|
|
|
CONFIG_DIR_NAME = ".homeassistant"
|
|
|
|
DATA_CUSTOMIZE = "hass_customize"
|
|
|
|
|
|
|
|
FILE_MIGRATION = (("ios.conf", ".ios.conf"),)
|
2017-06-02 06:50:04 +00:00
|
|
|
|
2016-07-30 17:40:51 +00:00
|
|
|
DEFAULT_CONFIG = """
|
2019-02-08 04:07:15 +00:00
|
|
|
# Configure a default setup of Home Assistant (frontend, api, etc)
|
|
|
|
default_config:
|
2016-07-30 17:40:51 +00:00
|
|
|
|
2018-08-23 20:16:31 +00:00
|
|
|
# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
|
|
|
|
# http:
|
|
|
|
# base_url: example.duckdns.org:8123
|
2016-07-30 17:40:51 +00:00
|
|
|
|
2016-12-18 22:59:45 +00:00
|
|
|
# Text to speech
|
|
|
|
tts:
|
2019-04-16 21:27:07 +00:00
|
|
|
- platform: google_translate
|
2016-12-18 22:59:45 +00:00
|
|
|
|
2017-02-24 06:53:16 +00:00
|
|
|
group: !include groups.yaml
|
2017-05-10 01:44:00 +00:00
|
|
|
automation: !include automations.yaml
|
2017-08-16 05:09:10 +00:00
|
|
|
script: !include scripts.yaml
|
2016-07-30 17:40:51 +00:00
|
|
|
"""
|
2017-10-05 16:10:29 +00:00
|
|
|
DEFAULT_SECRETS = """
|
|
|
|
# Use this file to store secrets like usernames and passwords.
|
|
|
|
# Learn more at https://home-assistant.io/docs/configuration/secrets/
|
2018-08-23 20:16:31 +00:00
|
|
|
some_password: welcome
|
2017-10-05 16:10:29 +00:00
|
|
|
"""
|
2019-04-16 21:27:07 +00:00
|
|
|
TTS_PRE_92 = """
|
|
|
|
tts:
|
|
|
|
- platform: google
|
|
|
|
"""
|
|
|
|
TTS_92 = """
|
|
|
|
tts:
|
|
|
|
- platform: google_translate
|
|
|
|
service_name: google_say
|
|
|
|
"""
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def _no_duplicate_auth_provider(
|
|
|
|
configs: Sequence[Dict[str, Any]]
|
|
|
|
) -> Sequence[Dict[str, Any]]:
|
2018-08-28 18:54:01 +00:00
|
|
|
"""No duplicate auth provider config allowed in a list.
|
|
|
|
|
|
|
|
Each type of auth provider can only have one config without optional id.
|
|
|
|
Unique id is required if same type of auth provider used multiple times.
|
|
|
|
"""
|
|
|
|
config_keys = set() # type: Set[Tuple[str, Optional[str]]]
|
|
|
|
for config in configs:
|
|
|
|
key = (config[CONF_TYPE], config.get(CONF_ID))
|
|
|
|
if key in config_keys:
|
|
|
|
raise vol.Invalid(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Duplicate auth provider {} found. Please add unique IDs if "
|
|
|
|
"you want to have the same auth provider twice".format(
|
2018-08-28 18:54:01 +00:00
|
|
|
config[CONF_TYPE]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-08-28 18:54:01 +00:00
|
|
|
config_keys.add(key)
|
|
|
|
return configs
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def _no_duplicate_auth_mfa_module(
|
|
|
|
configs: Sequence[Dict[str, Any]]
|
|
|
|
) -> Sequence[Dict[str, Any]]:
|
2018-08-28 18:54:01 +00:00
|
|
|
"""No duplicate auth mfa module item allowed in a list.
|
|
|
|
|
|
|
|
Each type of mfa module can only have one config without optional id.
|
|
|
|
A global unique id is required if same type of mfa module used multiple
|
|
|
|
times.
|
|
|
|
Note: this is different than auth provider
|
|
|
|
"""
|
|
|
|
config_keys = set() # type: Set[str]
|
|
|
|
for config in configs:
|
|
|
|
key = config.get(CONF_ID, config[CONF_TYPE])
|
|
|
|
if key in config_keys:
|
|
|
|
raise vol.Invalid(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Duplicate mfa module {} found. Please add unique IDs if "
|
|
|
|
"you want to have the same mfa module twice".format(config[CONF_TYPE])
|
|
|
|
)
|
2018-08-28 18:54:01 +00:00
|
|
|
config_keys.add(key)
|
|
|
|
return configs
|
|
|
|
|
|
|
|
|
2019-01-21 17:45:11 +00:00
|
|
|
PACKAGES_CONFIG_SCHEMA = cv.schema_with_slug_keys( # Package names are slugs
|
|
|
|
vol.Schema({cv.string: vol.Any(dict, list, None)}) # Component config
|
|
|
|
)
|
2017-01-14 06:01:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CUSTOMIZE_DICT_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
|
|
|
|
vol.Optional(ATTR_HIDDEN): cv.boolean,
|
|
|
|
vol.Optional(ATTR_ASSUMED_STATE): cv.boolean,
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
|
|
|
CUSTOMIZE_CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_CUSTOMIZE, default={}): vol.Schema(
|
|
|
|
{cv.entity_id: CUSTOMIZE_DICT_SCHEMA}
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_CUSTOMIZE_DOMAIN, default={}): vol.Schema(
|
|
|
|
{cv.string: CUSTOMIZE_DICT_SCHEMA}
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_CUSTOMIZE_GLOB, default={}): vol.Schema(
|
|
|
|
{cv.string: CUSTOMIZE_DICT_SCHEMA}
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
CORE_CONFIG_SCHEMA = CUSTOMIZE_CONFIG_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
CONF_NAME: vol.Coerce(str),
|
|
|
|
CONF_LATITUDE: cv.latitude,
|
|
|
|
CONF_LONGITUDE: cv.longitude,
|
|
|
|
CONF_ELEVATION: vol.Coerce(int),
|
|
|
|
vol.Optional(CONF_TEMPERATURE_UNIT): cv.temperature_unit,
|
|
|
|
CONF_UNIT_SYSTEM: cv.unit_system,
|
|
|
|
CONF_TIME_ZONE: cv.time_zone,
|
|
|
|
vol.Optional(CONF_WHITELIST_EXTERNAL_DIRS):
|
2017-06-25 22:10:30 +00:00
|
|
|
# pylint: disable=no-value-for-parameter
|
|
|
|
vol.All(cv.ensure_list, [vol.IsDir()]),
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_PACKAGES, default={}): PACKAGES_CONFIG_SCHEMA,
|
|
|
|
vol.Optional(CONF_AUTH_PROVIDERS): vol.All(
|
|
|
|
cv.ensure_list,
|
|
|
|
[
|
|
|
|
auth_providers.AUTH_PROVIDER_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
CONF_TYPE: vol.NotIn(
|
|
|
|
["insecure_example"],
|
|
|
|
"The insecure_example auth provider"
|
|
|
|
" is for testing only.",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
],
|
|
|
|
_no_duplicate_auth_provider,
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_AUTH_MFA_MODULES): vol.All(
|
|
|
|
cv.ensure_list,
|
|
|
|
[
|
|
|
|
auth_mfa_modules.MULTI_FACTOR_AUTH_MODULE_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
CONF_TYPE: vol.NotIn(
|
|
|
|
["insecure_example"],
|
|
|
|
"The insecure_example mfa module" " is for testing only.",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
],
|
|
|
|
_no_duplicate_auth_mfa_module,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2016-03-28 01:48:51 +00:00
|
|
|
|
|
|
|
|
2016-07-21 05:38:52 +00:00
|
|
|
def get_default_config_dir() -> str:
|
2017-07-18 14:23:57 +00:00
|
|
|
"""Put together the default configuration directory based on the OS."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data_dir = os.getenv("APPDATA") if os.name == "nt" else os.path.expanduser("~")
|
2018-05-12 21:44:53 +00:00
|
|
|
return os.path.join(data_dir, CONFIG_DIR_NAME) # type: ignore
|
2015-08-30 01:11:24 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_ensure_config_exists(
|
|
|
|
hass: HomeAssistant, config_dir: str
|
|
|
|
) -> Optional[str]:
|
2017-07-18 14:23:57 +00:00
|
|
|
"""Ensure a configuration file exists in given configuration directory.
|
2016-03-07 23:06:04 +00:00
|
|
|
|
|
|
|
Creating a default one if needed.
|
2017-07-18 14:23:57 +00:00
|
|
|
Return path to the configuration file.
|
2016-03-07 23:06:04 +00:00
|
|
|
"""
|
2015-04-26 17:05:01 +00:00
|
|
|
config_path = find_config_file(config_dir)
|
|
|
|
|
|
|
|
if config_path is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
print("Unable to find configuration. Creating default one in", config_dir)
|
2019-05-23 00:24:46 +00:00
|
|
|
config_path = await async_create_default_config(hass, config_dir)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
return config_path
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_create_default_config(
|
|
|
|
hass: HomeAssistant, config_dir: str
|
|
|
|
) -> Optional[str]:
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Create a default configuration file in given configuration directory.
|
|
|
|
|
|
|
|
Return path to new config file if success, None if failed.
|
2016-10-28 19:26:52 +00:00
|
|
|
This method needs to run in an executor.
|
2016-03-07 23:06:04 +00:00
|
|
|
"""
|
2019-05-23 00:24:46 +00:00
|
|
|
return await hass.async_add_executor_job(_write_default_config, config_dir)
|
2019-05-08 18:15:04 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def _write_default_config(config_dir: str) -> Optional[str]:
|
2019-05-08 18:15:04 +00:00
|
|
|
"""Write the default config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.config.group import CONFIG_PATH as GROUP_CONFIG_PATH
|
2017-05-10 01:44:00 +00:00
|
|
|
from homeassistant.components.config.automation import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_PATH as AUTOMATION_CONFIG_PATH,
|
|
|
|
)
|
|
|
|
from homeassistant.components.config.script import CONFIG_PATH as SCRIPT_CONFIG_PATH
|
2017-02-24 06:53:16 +00:00
|
|
|
|
2015-04-26 17:05:01 +00:00
|
|
|
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)
|
2017-10-05 16:10:29 +00:00
|
|
|
secret_path = os.path.join(config_dir, SECRET_YAML)
|
2016-06-27 16:02:45 +00:00
|
|
|
version_path = os.path.join(config_dir, VERSION_FILE)
|
2017-02-24 06:53:16 +00:00
|
|
|
group_yaml_path = os.path.join(config_dir, GROUP_CONFIG_PATH)
|
2017-05-10 01:44:00 +00:00
|
|
|
automation_yaml_path = os.path.join(config_dir, AUTOMATION_CONFIG_PATH)
|
2017-08-16 05:09:10 +00:00
|
|
|
script_yaml_path = os.path.join(config_dir, SCRIPT_CONFIG_PATH)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
# Writing files with YAML does not create the most human readable results
|
|
|
|
# So we're hard coding a YAML template.
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(config_path, "wt") as config_file:
|
2016-07-30 17:40:51 +00:00
|
|
|
config_file.write(DEFAULT_CONFIG)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(secret_path, "wt") as secret_file:
|
2017-10-05 16:10:29 +00:00
|
|
|
secret_file.write(DEFAULT_SECRETS)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(version_path, "wt") as version_file:
|
2016-06-27 16:02:45 +00:00
|
|
|
version_file.write(__version__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(group_yaml_path, "wt"):
|
2017-02-24 06:53:16 +00:00
|
|
|
pass
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(automation_yaml_path, "wt") as fil:
|
|
|
|
fil.write("[]")
|
2017-05-10 01:44:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(script_yaml_path, "wt"):
|
2017-08-16 05:09:10 +00:00
|
|
|
pass
|
|
|
|
|
2015-04-26 17:05:01 +00:00
|
|
|
return config_path
|
|
|
|
|
|
|
|
except IOError:
|
2017-07-18 14:23:57 +00:00
|
|
|
print("Unable to create default configuration file", config_path)
|
2015-04-26 17:05:01 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
async def async_hass_config_yaml(hass: HomeAssistant) -> Dict:
|
2017-07-18 14:23:57 +00:00
|
|
|
"""Load YAML from a Home Assistant configuration file.
|
2016-10-27 07:16:23 +00:00
|
|
|
|
2017-07-18 14:23:57 +00:00
|
|
|
This function allow a component inside the asyncio loop to reload its
|
2018-12-03 09:56:26 +00:00
|
|
|
configuration by itself. Include package merge.
|
2016-10-27 07:16:23 +00:00
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
def _load_hass_yaml_config() -> Dict:
|
2016-10-27 07:16:23 +00:00
|
|
|
path = find_config_file(hass.config.config_dir)
|
2018-07-23 08:24:39 +00:00
|
|
|
if path is None:
|
|
|
|
raise HomeAssistantError(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Config file not found in: {}".format(hass.config.config_dir)
|
|
|
|
)
|
2018-12-03 09:56:26 +00:00
|
|
|
config = load_yaml_config_file(path)
|
|
|
|
return config
|
2016-10-27 07:16:23 +00:00
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
config = await hass.async_add_executor_job(_load_hass_yaml_config)
|
|
|
|
core_config = config.get(CONF_CORE, {})
|
2019-07-31 19:25:30 +00:00
|
|
|
await merge_packages_config(hass, config, core_config.get(CONF_PACKAGES, {}))
|
2019-04-14 14:23:01 +00:00
|
|
|
return config
|
2016-10-27 07:16:23 +00:00
|
|
|
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
def find_config_file(config_dir: Optional[str]) -> Optional[str]:
|
2018-07-13 17:14:45 +00:00
|
|
|
"""Look in given directory for supported configuration files."""
|
2018-07-23 08:24:39 +00:00
|
|
|
if config_dir is None:
|
|
|
|
return None
|
2015-08-04 20:21:25 +00:00
|
|
|
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
2015-08-04 20:21:25 +00:00
|
|
|
return config_path if os.path.isfile(config_path) else None
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
def load_yaml_config_file(config_path: str) -> Dict[Any, Any]:
|
2016-10-28 19:26:52 +00:00
|
|
|
"""Parse a YAML configuration file.
|
|
|
|
|
2019-05-19 10:01:29 +00:00
|
|
|
Raises FileNotFoundError or HomeAssistantError.
|
|
|
|
|
2016-10-28 19:26:52 +00:00
|
|
|
This method needs to run in an executor.
|
|
|
|
"""
|
2019-05-19 10:01:29 +00:00
|
|
|
conf_dict = load_yaml(config_path)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
if not isinstance(conf_dict, dict):
|
2017-07-18 14:23:57 +00:00
|
|
|
msg = "The configuration file {} does not contain a dictionary".format(
|
2019-07-31 19:25:30 +00:00
|
|
|
os.path.basename(config_path)
|
|
|
|
)
|
2016-06-22 16:13:18 +00:00
|
|
|
_LOGGER.error(msg)
|
|
|
|
raise HomeAssistantError(msg)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
2018-03-09 03:34:24 +00:00
|
|
|
# Convert values to dictionaries if they are None
|
|
|
|
for key, value in conf_dict.items():
|
|
|
|
conf_dict[key] = value or {}
|
2015-04-26 17:05:01 +00:00
|
|
|
return conf_dict
|
2016-06-27 16:02:45 +00:00
|
|
|
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
def process_ha_config_upgrade(hass: HomeAssistant) -> None:
|
2017-07-18 14:23:57 +00:00
|
|
|
"""Upgrade configuration if necessary.
|
2016-10-27 07:16:23 +00:00
|
|
|
|
2016-10-28 19:26:52 +00:00
|
|
|
This method needs to run in an executor.
|
2016-10-27 07:16:23 +00:00
|
|
|
"""
|
2016-06-27 16:02:45 +00:00
|
|
|
version_path = hass.config.path(VERSION_FILE)
|
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(version_path, "rt") as inp:
|
2016-06-27 16:02:45 +00:00
|
|
|
conf_version = inp.readline().strip()
|
|
|
|
except FileNotFoundError:
|
|
|
|
# Last version to not have this file
|
2019-07-31 19:25:30 +00:00
|
|
|
conf_version = "0.7.7"
|
2016-06-27 16:02:45 +00:00
|
|
|
|
|
|
|
if conf_version == __version__:
|
|
|
|
return
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info(
|
|
|
|
"Upgrading configuration directory from %s to %s", conf_version, __version__
|
|
|
|
)
|
2016-06-27 16:02:45 +00:00
|
|
|
|
2019-06-08 15:19:00 +00:00
|
|
|
version_obj = LooseVersion(conf_version)
|
2019-06-03 19:37:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if version_obj < LooseVersion("0.50"):
|
2017-07-20 05:59:21 +00:00
|
|
|
# 0.50 introduced persistent deps dir.
|
2019-07-31 19:25:30 +00:00
|
|
|
lib_path = hass.config.path("deps")
|
2017-07-14 02:26:21 +00:00
|
|
|
if os.path.isdir(lib_path):
|
|
|
|
shutil.rmtree(lib_path)
|
2016-06-27 16:02:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if version_obj < LooseVersion("0.92"):
|
2019-04-16 21:27:07 +00:00
|
|
|
# 0.92 moved google/tts.py to google_translate/tts.py
|
|
|
|
config_path = find_config_file(hass.config.config_dir)
|
|
|
|
assert config_path is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(config_path, "rt", encoding="utf-8") as config_file:
|
2019-04-16 21:27:07 +00:00
|
|
|
config_raw = config_file.read()
|
|
|
|
|
|
|
|
if TTS_PRE_92 in config_raw:
|
|
|
|
_LOGGER.info("Migrating google tts to google_translate tts")
|
|
|
|
config_raw = config_raw.replace(TTS_PRE_92, TTS_92)
|
2019-04-25 19:58:10 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(config_path, "wt", encoding="utf-8") as config_file:
|
2019-04-25 19:58:10 +00:00
|
|
|
config_file.write(config_raw)
|
|
|
|
except IOError:
|
|
|
|
_LOGGER.exception("Migrating to google_translate tts failed")
|
|
|
|
pass
|
2019-04-16 21:27:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if version_obj < LooseVersion("0.94") and is_docker_env():
|
2019-06-03 19:37:27 +00:00
|
|
|
# In 0.94 we no longer install packages inside the deps folder when
|
|
|
|
# running inside a Docker container.
|
2019-07-31 19:25:30 +00:00
|
|
|
lib_path = hass.config.path("deps")
|
2019-06-03 19:37:27 +00:00
|
|
|
if os.path.isdir(lib_path):
|
|
|
|
shutil.rmtree(lib_path)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(version_path, "wt") as outp:
|
2016-06-27 16:02:45 +00:00
|
|
|
outp.write(__version__)
|
|
|
|
|
2019-04-16 21:27:07 +00:00
|
|
|
_LOGGER.debug("Migrating old system configuration files to new locations")
|
2017-06-02 06:50:04 +00:00
|
|
|
for oldf, newf in FILE_MIGRATION:
|
|
|
|
if os.path.isfile(hass.config.path(oldf)):
|
2017-07-18 14:23:57 +00:00
|
|
|
_LOGGER.info("Migrating %s to %s", oldf, newf)
|
2017-06-02 06:50:04 +00:00
|
|
|
os.rename(hass.config.path(oldf), hass.config.path(newf))
|
|
|
|
|
2016-06-27 16:02:45 +00:00
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
@callback
|
2019-07-31 19:25:30 +00:00
|
|
|
def async_log_exception(
|
|
|
|
ex: vol.Invalid, domain: str, config: Dict, hass: HomeAssistant
|
|
|
|
) -> None:
|
2018-03-09 03:34:24 +00:00
|
|
|
"""Log an error for configuration validation.
|
2017-03-01 04:33:19 +00:00
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
|
|
|
if hass is not None:
|
|
|
|
async_notify_setup_error(hass, domain, True)
|
2018-03-09 03:34:24 +00:00
|
|
|
_LOGGER.error(_format_config_error(ex, domain, config))
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2018-07-23 08:24:39 +00:00
|
|
|
def _format_config_error(ex: vol.Invalid, domain: str, config: Dict) -> str:
|
2018-03-09 03:34:24 +00:00
|
|
|
"""Generate log exception for configuration validation.
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2018-03-09 03:34:24 +00:00
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
|
|
|
message = "Invalid config for [{}]: ".format(domain)
|
2019-07-31 19:25:30 +00:00
|
|
|
if "extra keys not allowed" in ex.error_message:
|
|
|
|
message += (
|
|
|
|
"[{option}] is an invalid option for [{domain}]. "
|
|
|
|
"Check: {domain}->{path}.".format(
|
|
|
|
option=ex.path[-1],
|
|
|
|
domain=domain,
|
|
|
|
path="->".join(str(m) for m in ex.path),
|
|
|
|
)
|
|
|
|
)
|
2017-03-01 04:33:19 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
message += "{}.".format(humanize_error(config, ex))
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2019-02-04 18:58:06 +00:00
|
|
|
try:
|
|
|
|
domain_config = config.get(domain, config)
|
|
|
|
except AttributeError:
|
|
|
|
domain_config = config
|
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
message += " (See {}, line {}). ".format(
|
2019-07-31 19:25:30 +00:00
|
|
|
getattr(domain_config, "__config_file__", "?"),
|
|
|
|
getattr(domain_config, "__line__", "?"),
|
|
|
|
)
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2018-10-30 15:38:09 +00:00
|
|
|
if domain != CONF_CORE:
|
2019-07-31 19:25:30 +00:00
|
|
|
message += (
|
|
|
|
"Please check the docs at "
|
|
|
|
"https://home-assistant.io/components/{}/".format(domain)
|
|
|
|
)
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2018-03-09 03:34:24 +00:00
|
|
|
return message
|
2017-03-01 04:33:19 +00:00
|
|
|
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
async def async_process_ha_core_config(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: Dict,
|
|
|
|
api_password: Optional[str] = None,
|
|
|
|
trusted_networks: Optional[Any] = None,
|
|
|
|
) -> None:
|
2017-07-18 14:23:57 +00:00
|
|
|
"""Process the [homeassistant] section from the configuration.
|
2016-10-27 07:16:23 +00:00
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2016-06-27 16:02:45 +00:00
|
|
|
config = CORE_CONFIG_SCHEMA(config)
|
2018-05-01 16:20:41 +00:00
|
|
|
|
|
|
|
# Only load auth during startup.
|
2019-07-31 19:25:30 +00:00
|
|
|
if not hasattr(hass, "auth"):
|
2018-08-23 11:38:08 +00:00
|
|
|
auth_conf = config.get(CONF_AUTH_PROVIDERS)
|
|
|
|
|
|
|
|
if auth_conf is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
auth_conf = [{"type": "homeassistant"}]
|
2019-03-11 02:55:36 +00:00
|
|
|
if api_password:
|
2019-07-31 19:25:30 +00:00
|
|
|
auth_conf.append(
|
|
|
|
{"type": "legacy_api_password", "api_password": api_password}
|
|
|
|
)
|
2019-02-26 22:42:48 +00:00
|
|
|
if trusted_networks:
|
2019-07-31 19:25:30 +00:00
|
|
|
auth_conf.append(
|
|
|
|
{"type": "trusted_networks", "trusted_networks": trusted_networks}
|
|
|
|
)
|
2018-08-23 11:38:08 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mfa_conf = config.get(
|
|
|
|
CONF_AUTH_MFA_MODULES,
|
|
|
|
[{"type": "totp", "id": "totp", "name": "Authenticator app"}],
|
|
|
|
)
|
2018-08-26 20:38:52 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
setattr(
|
|
|
|
hass, "auth", await auth.auth_manager_from_config(hass, auth_conf, mfa_conf)
|
|
|
|
)
|
2018-05-01 16:20:41 +00:00
|
|
|
|
2019-05-20 18:02:36 +00:00
|
|
|
await hass.config.async_load()
|
2016-06-27 16:02:45 +00:00
|
|
|
|
2019-05-16 14:27:53 +00:00
|
|
|
hac = hass.config
|
2016-06-27 16:02:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if any(
|
|
|
|
[
|
|
|
|
k in config
|
|
|
|
for k in [
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_ELEVATION,
|
|
|
|
CONF_TIME_ZONE,
|
|
|
|
CONF_UNIT_SYSTEM,
|
|
|
|
]
|
|
|
|
]
|
|
|
|
):
|
2019-05-16 14:27:53 +00:00
|
|
|
hac.config_source = SOURCE_YAML
|
2016-06-27 16:02:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for key, attr in (
|
|
|
|
(CONF_LATITUDE, "latitude"),
|
|
|
|
(CONF_LONGITUDE, "longitude"),
|
|
|
|
(CONF_NAME, "location_name"),
|
|
|
|
(CONF_ELEVATION, "elevation"),
|
|
|
|
):
|
2016-06-27 16:02:45 +00:00
|
|
|
if key in config:
|
|
|
|
setattr(hac, attr, config[key])
|
|
|
|
|
2019-05-20 18:02:36 +00:00
|
|
|
if CONF_TIME_ZONE in config:
|
|
|
|
hac.set_time_zone(config[CONF_TIME_ZONE])
|
2016-06-27 16:02:45 +00:00
|
|
|
|
2017-07-18 14:23:57 +00:00
|
|
|
# Init whitelist external dir
|
2019-07-31 19:25:30 +00:00
|
|
|
hac.whitelist_external_dirs = {hass.config.path("www")}
|
2017-06-25 22:10:30 +00:00
|
|
|
if CONF_WHITELIST_EXTERNAL_DIRS in config:
|
2019-07-31 19:25:30 +00:00
|
|
|
hac.whitelist_external_dirs.update(set(config[CONF_WHITELIST_EXTERNAL_DIRS]))
|
2017-06-25 22:10:30 +00:00
|
|
|
|
2017-02-16 03:47:30 +00:00
|
|
|
# Customize
|
|
|
|
cust_exact = dict(config[CONF_CUSTOMIZE])
|
|
|
|
cust_domain = dict(config[CONF_CUSTOMIZE_DOMAIN])
|
2017-02-25 20:54:04 +00:00
|
|
|
cust_glob = OrderedDict(config[CONF_CUSTOMIZE_GLOB])
|
2017-02-16 03:47:30 +00:00
|
|
|
|
|
|
|
for name, pkg in config[CONF_PACKAGES].items():
|
|
|
|
pkg_cust = pkg.get(CONF_CORE)
|
|
|
|
|
|
|
|
if pkg_cust is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
pkg_cust = CUSTOMIZE_CONFIG_SCHEMA(pkg_cust)
|
|
|
|
except vol.Invalid:
|
2017-07-18 14:23:57 +00:00
|
|
|
_LOGGER.warning("Package %s contains invalid customize", name)
|
2017-02-16 03:47:30 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
cust_exact.update(pkg_cust[CONF_CUSTOMIZE])
|
|
|
|
cust_domain.update(pkg_cust[CONF_CUSTOMIZE_DOMAIN])
|
|
|
|
cust_glob.update(pkg_cust[CONF_CUSTOMIZE_GLOB])
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.data[DATA_CUSTOMIZE] = EntityValues(cust_exact, cust_domain, cust_glob)
|
2016-06-27 16:02:45 +00:00
|
|
|
|
2016-07-31 20:24:49 +00:00
|
|
|
if CONF_UNIT_SYSTEM in config:
|
|
|
|
if config[CONF_UNIT_SYSTEM] == CONF_UNIT_SYSTEM_IMPERIAL:
|
|
|
|
hac.units = IMPERIAL_SYSTEM
|
|
|
|
else:
|
|
|
|
hac.units = METRIC_SYSTEM
|
|
|
|
elif CONF_TEMPERATURE_UNIT in config:
|
|
|
|
unit = config[CONF_TEMPERATURE_UNIT]
|
|
|
|
if unit == TEMP_CELSIUS:
|
|
|
|
hac.units = METRIC_SYSTEM
|
|
|
|
else:
|
|
|
|
hac.units = IMPERIAL_SYSTEM
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Found deprecated temperature unit in core "
|
|
|
|
"configuration expected unit system. Replace '%s: %s' "
|
|
|
|
"with '%s: %s'",
|
|
|
|
CONF_TEMPERATURE_UNIT,
|
|
|
|
unit,
|
|
|
|
CONF_UNIT_SYSTEM,
|
|
|
|
hac.units.name,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _log_pkg_error(package: str, component: str, config: Dict, message: str) -> None:
|
2018-03-09 03:34:24 +00:00
|
|
|
"""Log an error while merging packages."""
|
2019-07-05 22:24:26 +00:00
|
|
|
message = "Package {} setup failed. Integration {} {}".format(
|
2019-07-31 19:25:30 +00:00
|
|
|
package, component, message
|
|
|
|
)
|
2017-01-14 06:01:47 +00:00
|
|
|
|
|
|
|
pack_config = config[CONF_CORE][CONF_PACKAGES].get(package, config)
|
|
|
|
message += " (See {}:{}). ".format(
|
2019-07-31 19:25:30 +00:00
|
|
|
getattr(pack_config, "__config_file__", "?"),
|
|
|
|
getattr(pack_config, "__line__", "?"),
|
|
|
|
)
|
2017-01-14 06:01:47 +00:00
|
|
|
|
|
|
|
_LOGGER.error(message)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def _identify_config_schema(module: ModuleType) -> Tuple[Optional[str], Optional[Dict]]:
|
2017-01-14 06:01:47 +00:00
|
|
|
"""Extract the schema and identify list or dict based."""
|
|
|
|
try:
|
2018-07-23 08:24:39 +00:00
|
|
|
schema = module.CONFIG_SCHEMA.schema[module.DOMAIN] # type: ignore
|
2017-01-14 06:01:47 +00:00
|
|
|
except (AttributeError, KeyError):
|
2018-07-23 08:24:39 +00:00
|
|
|
return None, None
|
2017-01-14 06:01:47 +00:00
|
|
|
t_schema = str(schema)
|
2019-07-31 19:25:30 +00:00
|
|
|
if t_schema.startswith("{") or "schema_with_slug_keys" in t_schema:
|
|
|
|
return ("dict", schema)
|
|
|
|
if t_schema.startswith(("[", "All(<function ensure_list")):
|
|
|
|
return ("list", schema)
|
|
|
|
return "", schema
|
2017-01-14 06:01:47 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def _recursive_merge(conf: Dict[str, Any], package: Dict[str, Any]) -> Union[bool, str]:
|
2018-05-25 20:41:50 +00:00
|
|
|
"""Merge package into conf, recursively."""
|
2018-07-23 08:24:39 +00:00
|
|
|
error = False # type: Union[bool, str]
|
2018-05-25 20:41:50 +00:00
|
|
|
for key, pack_conf in package.items():
|
|
|
|
if isinstance(pack_conf, dict):
|
|
|
|
if not pack_conf:
|
|
|
|
continue
|
|
|
|
conf[key] = conf.get(key, OrderedDict())
|
2018-06-16 10:55:32 +00:00
|
|
|
error = _recursive_merge(conf=conf[key], package=pack_conf)
|
2018-05-25 20:41:50 +00:00
|
|
|
|
|
|
|
elif isinstance(pack_conf, list):
|
2019-08-10 23:30:33 +00:00
|
|
|
conf[key] = cv.remove_falsy(
|
|
|
|
cv.ensure_list(conf.get(key)) + cv.ensure_list(pack_conf)
|
|
|
|
)
|
2018-05-25 20:41:50 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
if conf.get(key) is not None:
|
2018-06-16 10:55:32 +00:00
|
|
|
return key
|
2018-07-23 08:16:05 +00:00
|
|
|
conf[key] = pack_conf
|
2018-06-16 10:55:32 +00:00
|
|
|
return error
|
2018-05-25 20:41:50 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def merge_packages_config(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: Dict,
|
|
|
|
packages: Dict[str, Any],
|
|
|
|
_log_pkg_error: Callable = _log_pkg_error,
|
|
|
|
) -> Dict:
|
2017-07-18 14:23:57 +00:00
|
|
|
"""Merge packages into the top-level configuration. Mutate config."""
|
2017-01-14 06:01:47 +00:00
|
|
|
# pylint: disable=too-many-nested-blocks
|
|
|
|
PACKAGES_CONFIG_SCHEMA(packages)
|
|
|
|
for pack_name, pack_conf in packages.items():
|
|
|
|
for comp_name, comp_conf in pack_conf.items():
|
2017-01-27 06:26:49 +00:00
|
|
|
if comp_name == CONF_CORE:
|
|
|
|
continue
|
2018-09-24 08:17:24 +00:00
|
|
|
# If component name is given with a trailing description, remove it
|
|
|
|
# when looking for component
|
2019-07-31 19:25:30 +00:00
|
|
|
domain = comp_name.split(" ")[0]
|
2017-01-14 06:01:47 +00:00
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
try:
|
2019-08-07 22:35:50 +00:00
|
|
|
integration = await async_get_integration_with_requirements(
|
|
|
|
hass, domain
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-14 14:23:01 +00:00
|
|
|
component = integration.get_component()
|
2019-08-07 22:35:50 +00:00
|
|
|
except (IntegrationNotFound, RequirementsNotFound, ImportError) as ex:
|
|
|
|
_log_pkg_error(pack_name, comp_name, config, str(ex))
|
2019-04-14 14:23:01 +00:00
|
|
|
continue
|
|
|
|
|
2019-08-10 23:30:33 +00:00
|
|
|
merge_list = hasattr(component, "PLATFORM_SCHEMA")
|
2017-01-14 06:01:47 +00:00
|
|
|
|
2019-08-10 23:30:33 +00:00
|
|
|
if not merge_list and hasattr(component, "CONFIG_SCHEMA"):
|
2017-01-14 06:01:47 +00:00
|
|
|
merge_type, _ = _identify_config_schema(component)
|
2019-08-10 23:30:33 +00:00
|
|
|
merge_list = merge_type == "list"
|
2017-01-14 06:01:47 +00:00
|
|
|
|
2019-08-10 23:30:33 +00:00
|
|
|
if merge_list:
|
|
|
|
config[comp_name] = cv.remove_falsy(
|
|
|
|
cv.ensure_list(config.get(comp_name)) + cv.ensure_list(comp_conf)
|
|
|
|
)
|
|
|
|
continue
|
2017-01-14 06:01:47 +00:00
|
|
|
|
2018-06-16 10:55:32 +00:00
|
|
|
if comp_conf is None:
|
|
|
|
comp_conf = OrderedDict()
|
|
|
|
|
|
|
|
if not isinstance(comp_conf, dict):
|
|
|
|
_log_pkg_error(
|
2019-07-31 19:25:30 +00:00
|
|
|
pack_name, comp_name, config, "cannot be merged. Expected a dict."
|
|
|
|
)
|
2018-06-16 10:55:32 +00:00
|
|
|
continue
|
2017-01-14 06:01:47 +00:00
|
|
|
|
2018-06-16 10:55:32 +00:00
|
|
|
if comp_name not in config or config[comp_name] is None:
|
|
|
|
config[comp_name] = OrderedDict()
|
|
|
|
|
|
|
|
if not isinstance(config[comp_name], dict):
|
|
|
|
_log_pkg_error(
|
2019-07-31 19:25:30 +00:00
|
|
|
pack_name,
|
|
|
|
comp_name,
|
|
|
|
config,
|
|
|
|
"cannot be merged. Dict expected in main config.",
|
|
|
|
)
|
2017-01-14 06:01:47 +00:00
|
|
|
continue
|
2018-06-16 10:55:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
error = _recursive_merge(conf=config[comp_name], package=comp_conf)
|
2018-06-16 10:55:32 +00:00
|
|
|
if error:
|
2019-07-31 19:25:30 +00:00
|
|
|
_log_pkg_error(
|
|
|
|
pack_name, comp_name, config, "has duplicate key '{}'".format(error)
|
|
|
|
)
|
2017-01-14 06:01:47 +00:00
|
|
|
|
|
|
|
return config
|
2017-01-27 06:26:49 +00:00
|
|
|
|
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
async def async_process_component_config(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass: HomeAssistant, config: Dict, integration: Integration
|
|
|
|
) -> Optional[Dict]:
|
2017-07-18 14:23:57 +00:00
|
|
|
"""Check component configuration and return processed configuration.
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2018-03-09 03:34:24 +00:00
|
|
|
Returns None on error.
|
2017-03-01 04:33:19 +00:00
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
2019-04-14 14:23:01 +00:00
|
|
|
domain = integration.domain
|
2019-05-13 06:54:55 +00:00
|
|
|
try:
|
|
|
|
component = integration.get_component()
|
|
|
|
except ImportError as ex:
|
|
|
|
_LOGGER.error("Unable to import %s: %s", domain, ex)
|
|
|
|
return None
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if hasattr(component, "CONFIG_SCHEMA"):
|
2017-03-01 04:33:19 +00:00
|
|
|
try:
|
2019-04-14 14:23:01 +00:00
|
|
|
return component.CONFIG_SCHEMA(config) # type: ignore
|
2017-03-01 04:33:19 +00:00
|
|
|
except vol.Invalid as ex:
|
|
|
|
async_log_exception(ex, domain, config, hass)
|
|
|
|
return None
|
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
component_platform_schema = getattr(
|
2019-07-31 19:25:30 +00:00
|
|
|
component, "PLATFORM_SCHEMA_BASE", getattr(component, "PLATFORM_SCHEMA", None)
|
|
|
|
)
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
if component_platform_schema is None:
|
|
|
|
return config
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
platforms = []
|
|
|
|
for p_name, p_config in config_per_platform(config, domain):
|
|
|
|
# Validate component specific platform schema
|
|
|
|
try:
|
|
|
|
p_validated = component_platform_schema(p_config)
|
|
|
|
except vol.Invalid as ex:
|
|
|
|
async_log_exception(ex, domain, p_config, hass)
|
|
|
|
continue
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
# Not all platform components follow same pattern for platforms
|
|
|
|
# So if p_name is None we are not going to validate platform
|
|
|
|
# (the automation component is one of them)
|
|
|
|
if p_name is None:
|
|
|
|
platforms.append(p_validated)
|
|
|
|
continue
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
try:
|
2019-08-07 22:35:50 +00:00
|
|
|
p_integration = await async_get_integration_with_requirements(hass, p_name)
|
|
|
|
except (RequirementsNotFound, IntegrationNotFound) as ex:
|
|
|
|
_LOGGER.error("Platform error: %s - %s", domain, ex)
|
2019-07-14 21:13:37 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2019-04-14 14:23:01 +00:00
|
|
|
platform = p_integration.get_platform(domain)
|
2019-07-14 21:13:37 +00:00
|
|
|
except ImportError:
|
2019-08-07 22:35:50 +00:00
|
|
|
_LOGGER.exception("Platform error: %s", domain)
|
2019-04-14 14:23:01 +00:00
|
|
|
continue
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
# Validate platform specific schema
|
2019-07-31 19:25:30 +00:00
|
|
|
if hasattr(platform, "PLATFORM_SCHEMA"):
|
2019-04-14 14:23:01 +00:00
|
|
|
# pylint: disable=no-member
|
|
|
|
try:
|
|
|
|
p_validated = platform.PLATFORM_SCHEMA( # type: ignore
|
2019-07-31 19:25:30 +00:00
|
|
|
p_config
|
|
|
|
)
|
2019-04-14 14:23:01 +00:00
|
|
|
except vol.Invalid as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
async_log_exception(ex, "{}.{}".format(domain, p_name), p_config, hass)
|
2019-04-14 14:23:01 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
platforms.append(p_validated)
|
2017-03-01 04:33:19 +00:00
|
|
|
|
2019-04-14 14:23:01 +00:00
|
|
|
# Create a copy of the configuration with all config for current
|
|
|
|
# component removed and add validated config back in.
|
2019-05-15 21:43:45 +00:00
|
|
|
config = config_without_domain(config, domain)
|
2019-04-14 14:23:01 +00:00
|
|
|
config[domain] = platforms
|
2017-03-01 04:33:19 +00:00
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2019-05-15 21:43:45 +00:00
|
|
|
@callback
|
|
|
|
def config_without_domain(config: Dict, domain: str) -> Dict:
|
|
|
|
"""Return a config with all configuration for a domain removed."""
|
|
|
|
filter_keys = extract_domain_configs(config, domain)
|
2019-07-31 19:25:30 +00:00
|
|
|
return {key: value for key, value in config.items() if key not in filter_keys}
|
2019-05-15 21:43:45 +00:00
|
|
|
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
async def async_check_ha_config_file(hass: HomeAssistant) -> Optional[str]:
|
2017-07-18 14:23:57 +00:00
|
|
|
"""Check if Home Assistant configuration file is valid.
|
2017-02-08 17:17:52 +00:00
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
2019-07-10 18:56:50 +00:00
|
|
|
import homeassistant.helpers.check_config as check_config
|
2018-03-19 21:20:04 +00:00
|
|
|
|
2019-07-10 18:56:50 +00:00
|
|
|
res = await check_config.async_check_ha_config_file(hass)
|
2018-03-19 21:20:04 +00:00
|
|
|
|
|
|
|
if not res.errors:
|
|
|
|
return None
|
2019-07-10 18:56:50 +00:00
|
|
|
return res.error_str
|
2017-03-01 04:33:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2018-07-23 08:24:39 +00:00
|
|
|
def async_notify_setup_error(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass: HomeAssistant, component: str, display_link: bool = False
|
|
|
|
) -> None:
|
2017-03-01 04:33:19 +00:00
|
|
|
"""Print a persistent notification.
|
|
|
|
|
|
|
|
This method must be run in the event loop.
|
|
|
|
"""
|
|
|
|
from homeassistant.components import persistent_notification
|
|
|
|
|
|
|
|
errors = hass.data.get(DATA_PERSISTENT_ERRORS)
|
|
|
|
|
|
|
|
if errors is None:
|
|
|
|
errors = hass.data[DATA_PERSISTENT_ERRORS] = {}
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
errors[component] = errors.get(component) or display_link
|
2017-11-03 17:12:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
message = "The following components and platforms could not be set up:\n\n"
|
2017-11-03 17:12:45 +00:00
|
|
|
|
|
|
|
for name, link in errors.items():
|
|
|
|
if link:
|
2019-07-31 19:25:30 +00:00
|
|
|
part = HA_COMPONENT_URL.format(name.replace("_", "-"), name)
|
2017-11-03 17:12:45 +00:00
|
|
|
else:
|
|
|
|
part = name
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
message += " - {}\n".format(part)
|
2017-11-03 17:12:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
message += "\nPlease check your config."
|
2017-11-03 17:12:45 +00:00
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
persistent_notification.async_create(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, message, "Invalid config", "invalid_config"
|
|
|
|
)
|