2018-04-07 01:23:03 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import importlib
|
|
|
|
import logging
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
from voluptuous.humanize import humanize_error
|
|
|
|
|
|
|
|
import esphomeyaml.config_validation as cv
|
2018-06-07 17:06:50 +02:00
|
|
|
from esphomeyaml import core, yaml_util, automation
|
2018-05-16 19:45:33 +02:00
|
|
|
from esphomeyaml.const import CONF_BOARD, CONF_BOARD_FLASH_MODE, CONF_ESPHOMEYAML, \
|
2018-06-07 17:06:50 +02:00
|
|
|
CONF_LIBRARY_URI, CONF_NAME, CONF_PLATFORM, CONF_SIMPLIFY, CONF_USE_BUILD_FLAGS, CONF_WIFI, \
|
|
|
|
ESP_PLATFORMS, ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266, CONF_ON_BOOT, CONF_TRIGGER_ID, \
|
|
|
|
CONF_PRIORITY, CONF_ON_SHUTDOWN
|
2018-04-07 01:23:03 +02:00
|
|
|
from esphomeyaml.core import ESPHomeYAMLError
|
2018-06-07 17:06:50 +02:00
|
|
|
from esphomeyaml.helpers import App, add, color, esphomelib_ns, Pvariable, NoArg, const_char_p
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-06-06 21:01:20 +02:00
|
|
|
DEFAULT_LIBRARY_URI = u'https://github.com/OttoWinter/esphomelib.git#v1.6.2'
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-05-16 19:45:33 +02:00
|
|
|
BUILD_FLASH_MODES = ['qio', 'qout', 'dio', 'dout']
|
2018-06-07 17:06:50 +02:00
|
|
|
StartupTrigger = esphomelib_ns.StartupTrigger
|
|
|
|
ShutdownTrigger = esphomelib_ns.ShutdownTrigger
|
2018-05-16 19:45:33 +02:00
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
CORE_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_NAME): cv.valid_name,
|
2018-04-18 18:43:13 +02:00
|
|
|
vol.Required(CONF_PLATFORM): cv.string,
|
2018-04-07 01:23:03 +02:00
|
|
|
vol.Required(CONF_BOARD): cv.string,
|
|
|
|
vol.Optional(CONF_LIBRARY_URI, default=DEFAULT_LIBRARY_URI): cv.string,
|
|
|
|
vol.Optional(CONF_SIMPLIFY, default=True): cv.boolean,
|
2018-05-14 11:50:56 +02:00
|
|
|
vol.Optional(CONF_USE_BUILD_FLAGS, default=True): cv.boolean,
|
2018-05-20 12:41:52 +02:00
|
|
|
vol.Optional(CONF_BOARD_FLASH_MODE): vol.All(vol.Lower, cv.one_of(*BUILD_FLASH_MODES)),
|
2018-06-07 17:06:50 +02:00
|
|
|
vol.Optional(CONF_ON_BOOT): vol.All(cv.ensure_list, [automation.AUTOMATION_SCHEMA.extend({
|
|
|
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(StartupTrigger),
|
|
|
|
vol.Optional(CONF_PRIORITY): vol.Coerce(float),
|
|
|
|
})]),
|
|
|
|
vol.Optional(CONF_ON_SHUTDOWN): vol.All(cv.ensure_list, [automation.AUTOMATION_SCHEMA.extend({
|
|
|
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(ShutdownTrigger),
|
|
|
|
})]),
|
2018-04-07 01:23:03 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
REQUIRED_COMPONENTS = [
|
2018-04-18 18:43:13 +02:00
|
|
|
CONF_ESPHOMEYAML, CONF_WIFI
|
2018-04-07 01:23:03 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
_COMPONENT_CACHE = {}
|
|
|
|
_ALL_COMPONENTS = []
|
|
|
|
|
|
|
|
|
|
|
|
def core_to_code(config):
|
|
|
|
add(App.set_name(config[CONF_NAME]))
|
|
|
|
|
2018-06-07 17:06:50 +02:00
|
|
|
for conf in config.get(CONF_ON_BOOT, []):
|
|
|
|
rhs = App.register_component(StartupTrigger.new(conf.get(CONF_PRIORITY)))
|
|
|
|
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
|
|
|
|
automation.build_automation(trigger, NoArg, conf)
|
|
|
|
|
|
|
|
for conf in config.get(CONF_ON_SHUTDOWN, []):
|
|
|
|
trigger = Pvariable(conf[CONF_TRIGGER_ID], ShutdownTrigger.new())
|
|
|
|
automation.build_automation(trigger, const_char_p, conf)
|
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
def get_component(domain):
|
|
|
|
if domain in _COMPONENT_CACHE:
|
|
|
|
return _COMPONENT_CACHE[domain]
|
|
|
|
|
|
|
|
path = 'esphomeyaml.components.{}'.format(domain)
|
|
|
|
try:
|
|
|
|
module = importlib.import_module(path)
|
|
|
|
except ImportError as err:
|
|
|
|
_LOGGER.debug(err)
|
|
|
|
else:
|
|
|
|
_COMPONENT_CACHE[domain] = module
|
|
|
|
return module
|
|
|
|
|
|
|
|
_LOGGER.error("Unable to find component %s", domain)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_platform(domain, platform):
|
|
|
|
return get_component("{}.{}".format(domain, platform))
|
|
|
|
|
|
|
|
|
|
|
|
def is_platform_component(component):
|
|
|
|
return hasattr(component, 'PLATFORM_SCHEMA')
|
|
|
|
|
|
|
|
|
2018-04-18 18:43:13 +02:00
|
|
|
def iter_components(config):
|
|
|
|
for domain, conf in config.iteritems():
|
|
|
|
if domain == CONF_ESPHOMEYAML:
|
|
|
|
continue
|
|
|
|
component = get_component(domain)
|
|
|
|
yield domain, component, conf
|
|
|
|
if is_platform_component(component):
|
|
|
|
for p_config in conf:
|
|
|
|
p_name = u"{}.{}".format(domain, p_config[CONF_PLATFORM])
|
|
|
|
platform = get_component(p_name)
|
|
|
|
yield p_name, platform, p_config
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Config(OrderedDict):
|
|
|
|
def __init__(self):
|
|
|
|
super(Config, self).__init__()
|
|
|
|
self.errors = []
|
|
|
|
|
|
|
|
def add_error(self, message, domain=None, config=None):
|
|
|
|
if not isinstance(message, unicode):
|
|
|
|
message = unicode(message)
|
|
|
|
self.errors.append((message, domain, config))
|
|
|
|
|
|
|
|
|
2018-06-02 22:22:20 +02:00
|
|
|
def iter_ids(config, prefix=None, parent=None):
|
|
|
|
prefix = prefix or []
|
|
|
|
parent = parent or {}
|
|
|
|
if isinstance(config, core.ID):
|
|
|
|
yield config, prefix, parent
|
|
|
|
elif isinstance(config, core.Lambda):
|
|
|
|
for id in config.requires_ids:
|
|
|
|
yield id, prefix, parent
|
|
|
|
elif isinstance(config, list):
|
|
|
|
for i, item in enumerate(config):
|
|
|
|
for result in iter_ids(item, prefix + [str(i)], config):
|
|
|
|
yield result
|
|
|
|
elif isinstance(config, dict):
|
|
|
|
for key, value in config.iteritems():
|
|
|
|
for result in iter_ids(value, prefix + [str(key)], config):
|
|
|
|
yield result
|
|
|
|
|
|
|
|
|
|
|
|
def do_id_pass(result):
|
|
|
|
declare_ids = []
|
|
|
|
searching_ids = []
|
|
|
|
for id, prefix, config in iter_ids(result):
|
|
|
|
if id.is_declaration:
|
|
|
|
if id.id is not None and any(v[0].id == id.id for v in declare_ids):
|
|
|
|
result.add_error("ID {} redefined!".format(id.id), '.'.join(prefix), config)
|
|
|
|
continue
|
|
|
|
declare_ids.append((id, prefix, config))
|
|
|
|
else:
|
|
|
|
searching_ids.append((id, prefix, config))
|
|
|
|
# Resolve default ids after manual IDs
|
|
|
|
for id, _, _ in declare_ids:
|
|
|
|
id.resolve([v[0].id for v in declare_ids])
|
|
|
|
|
|
|
|
# Check searched IDs
|
|
|
|
for id, prefix, config in searching_ids:
|
|
|
|
if id.id is not None and not any(v[0].id == id.id for v in declare_ids):
|
|
|
|
result.add_error("Couldn't find ID {}".format(id.id), '.'.join(prefix), config)
|
|
|
|
if id.id is None and id.type is not None:
|
|
|
|
id.id = next((v[0].id for v in declare_ids if v[0].type == id.type), None)
|
|
|
|
if id.id is None:
|
|
|
|
result.add_error("Couldn't resolve ID for type {}".format(id.type),
|
|
|
|
'.'.join(prefix), config)
|
|
|
|
|
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
def validate_config(config):
|
|
|
|
global _ALL_COMPONENTS
|
|
|
|
|
|
|
|
for req in REQUIRED_COMPONENTS:
|
|
|
|
if req not in config:
|
2018-04-10 17:17:46 +02:00
|
|
|
raise ESPHomeYAMLError("Component {} is required for esphomeyaml.".format(req))
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
_ALL_COMPONENTS = list(config.keys())
|
|
|
|
|
|
|
|
result = Config()
|
|
|
|
|
|
|
|
def _comp_error(ex, domain, config):
|
|
|
|
result.add_error(_format_config_error(ex, domain, config), domain, config)
|
|
|
|
|
|
|
|
try:
|
2018-04-18 18:43:13 +02:00
|
|
|
result[CONF_ESPHOMEYAML] = CORE_SCHEMA(config[CONF_ESPHOMEYAML])
|
2018-04-07 01:23:03 +02:00
|
|
|
except vol.Invalid as ex:
|
|
|
|
_comp_error(ex, CONF_ESPHOMEYAML, config)
|
|
|
|
|
|
|
|
for domain, conf in config.iteritems():
|
|
|
|
if domain == CONF_ESPHOMEYAML:
|
|
|
|
continue
|
|
|
|
if conf is None:
|
|
|
|
conf = {}
|
|
|
|
component = get_component(domain)
|
|
|
|
if component is None:
|
|
|
|
result.add_error(u"Component not found: {}".format(domain))
|
|
|
|
continue
|
|
|
|
|
|
|
|
esp_platforms = getattr(component, 'ESP_PLATFORMS', ESP_PLATFORMS)
|
2018-04-18 18:43:13 +02:00
|
|
|
if core.ESP_PLATFORM not in esp_platforms:
|
|
|
|
result.add_error(u"Component {} doesn't support {}.".format(domain, core.ESP_PLATFORM))
|
2018-04-07 01:23:03 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
success = True
|
|
|
|
dependencies = getattr(component, 'DEPENDENCIES', [])
|
|
|
|
for dependency in dependencies:
|
|
|
|
if dependency not in _ALL_COMPONENTS:
|
2018-06-06 10:27:59 +02:00
|
|
|
result.add_error(u"Component {} requires component {}".format(domain, dependency))
|
2018-04-07 01:23:03 +02:00
|
|
|
success = False
|
|
|
|
if not success:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if hasattr(component, 'CONFIG_SCHEMA'):
|
|
|
|
try:
|
|
|
|
validated = component.CONFIG_SCHEMA(conf)
|
|
|
|
result[domain] = validated
|
|
|
|
except vol.Invalid as ex:
|
2018-04-18 18:43:13 +02:00
|
|
|
_comp_error(ex, domain, conf)
|
2018-04-07 01:23:03 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not hasattr(component, 'PLATFORM_SCHEMA'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
platforms = []
|
2018-04-10 17:17:46 +02:00
|
|
|
for p_config in conf:
|
2018-04-07 01:23:03 +02:00
|
|
|
if not isinstance(p_config, dict):
|
2018-04-18 18:43:13 +02:00
|
|
|
result.add_error(u"Platform schemas must have 'platform:' key")
|
2018-04-07 01:23:03 +02:00
|
|
|
continue
|
|
|
|
p_name = p_config.get(u'platform')
|
|
|
|
if p_name is None:
|
|
|
|
result.add_error(u"No platform specified for {}".format(domain))
|
|
|
|
continue
|
|
|
|
platform = get_platform(domain, p_name)
|
|
|
|
if platform is None:
|
2018-06-11 10:01:54 +02:00
|
|
|
result.add_error(u"Platform not found: {}.{}".format(domain, p_name))
|
2018-04-07 01:23:03 +02:00
|
|
|
continue
|
|
|
|
|
2018-04-18 18:43:13 +02:00
|
|
|
success = True
|
|
|
|
dependencies = getattr(platform, 'DEPENDENCIES', [])
|
|
|
|
for dependency in dependencies:
|
|
|
|
if dependency not in _ALL_COMPONENTS:
|
2018-06-06 10:27:59 +02:00
|
|
|
result.add_error(u"Platform {}.{} requires component {}".format(domain, p_name,
|
|
|
|
dependency))
|
2018-04-18 18:43:13 +02:00
|
|
|
success = False
|
|
|
|
if not success:
|
|
|
|
continue
|
|
|
|
|
2018-05-20 12:41:52 +02:00
|
|
|
esp_platforms = getattr(platform, 'ESP_PLATFORMS', ESP_PLATFORMS)
|
|
|
|
if core.ESP_PLATFORM not in esp_platforms:
|
|
|
|
result.add_error(
|
|
|
|
u"Platform {}.{} doesn't support {}.".format(domain, p_name, core.ESP_PLATFORM))
|
|
|
|
continue
|
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
if hasattr(platform, u'PLATFORM_SCHEMA'):
|
|
|
|
try:
|
|
|
|
p_validated = platform.PLATFORM_SCHEMA(p_config)
|
|
|
|
except vol.Invalid as ex:
|
|
|
|
_comp_error(ex, u'{}.{}'.format(domain, p_name), p_config)
|
|
|
|
continue
|
|
|
|
platforms.append(p_validated)
|
|
|
|
result[domain] = platforms
|
2018-06-02 22:22:20 +02:00
|
|
|
|
|
|
|
do_id_pass(result)
|
2018-04-07 01:23:03 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2018-04-18 18:43:13 +02:00
|
|
|
REQUIRED = ['esphomeyaml', 'wifi']
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _format_config_error(ex, domain, config):
|
|
|
|
message = u"Invalid config for [{}]: ".format(domain)
|
|
|
|
if u'extra keys not allowed' in ex.error_message:
|
|
|
|
message += u'[{}] is an invalid option for [{}]. Check: {}->{}.' \
|
|
|
|
.format(ex.path[-1], domain, domain,
|
|
|
|
u'->'.join(str(m) for m in ex.path))
|
|
|
|
else:
|
|
|
|
message += u'{}.'.format(humanize_error(config, ex))
|
|
|
|
|
2018-05-06 15:56:12 +02:00
|
|
|
if isinstance(config, list):
|
|
|
|
return message
|
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
domain_config = config.get(domain, config)
|
|
|
|
message += u" (See {}, line {}). ".format(
|
|
|
|
getattr(domain_config, '__config_file__', '?'),
|
|
|
|
getattr(domain_config, '__line__', '?'))
|
|
|
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(path):
|
|
|
|
try:
|
|
|
|
config = yaml_util.load_yaml(path)
|
|
|
|
except OSError:
|
|
|
|
raise ESPHomeYAMLError(u"Could not read configuration file at {}".format(path))
|
2018-05-06 15:56:12 +02:00
|
|
|
core.RAW_CONFIG = config
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-04-18 18:43:13 +02:00
|
|
|
if CONF_ESPHOMEYAML not in config:
|
|
|
|
raise ESPHomeYAMLError(u"No esphomeyaml section in config")
|
|
|
|
core_conf = config[CONF_ESPHOMEYAML]
|
2018-06-01 18:06:18 +02:00
|
|
|
if CONF_PLATFORM not in core_conf:
|
|
|
|
raise ESPHomeYAMLError("esphomeyaml.platform not specified.")
|
|
|
|
esp_platform = unicode(core_conf[CONF_PLATFORM])
|
2018-04-07 01:23:03 +02:00
|
|
|
esp_platform = esp_platform.upper()
|
2018-04-18 18:43:13 +02:00
|
|
|
if '8266' in esp_platform:
|
|
|
|
esp_platform = ESP_PLATFORM_ESP8266
|
|
|
|
if '32' in esp_platform:
|
|
|
|
esp_platform = ESP_PLATFORM_ESP32
|
|
|
|
core.ESP_PLATFORM = esp_platform
|
2018-06-01 18:06:18 +02:00
|
|
|
if CONF_BOARD not in core_conf:
|
|
|
|
raise ESPHomeYAMLError("esphomeyaml.board not specified.")
|
|
|
|
core.BOARD = unicode(core_conf[CONF_BOARD])
|
2018-04-18 18:43:13 +02:00
|
|
|
core.SIMPLIFY = cv.boolean(core_conf.get(CONF_SIMPLIFY, True))
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
result = validate_config(config)
|
2018-06-02 22:22:20 +02:00
|
|
|
except ESPHomeYAMLError:
|
|
|
|
raise
|
2018-04-10 16:21:32 +02:00
|
|
|
except Exception:
|
2018-06-02 22:22:20 +02:00
|
|
|
_LOGGER.error(u"Unexpected exception while reading configuration:")
|
2018-04-07 01:23:03 +02:00
|
|
|
raise
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def line_info(obj, **kwargs):
|
|
|
|
"""Display line config source."""
|
|
|
|
if hasattr(obj, '__config_file__'):
|
|
|
|
return color('cyan', "[source {}:{}]"
|
|
|
|
.format(obj.__config_file__, obj.__line__ or '?'),
|
|
|
|
**kwargs)
|
|
|
|
return '?'
|
|
|
|
|
|
|
|
|
|
|
|
def dump_dict(layer, indent_count=3, listi=False, **kwargs):
|
|
|
|
def sort_dict_key(val):
|
|
|
|
"""Return the dict key for sorting."""
|
|
|
|
key = str.lower(val[0])
|
|
|
|
return '0' if key == 'platform' else key
|
|
|
|
|
|
|
|
indent_str = indent_count * ' '
|
|
|
|
if listi or isinstance(layer, list):
|
|
|
|
indent_str = indent_str[:-1] + '-'
|
|
|
|
if isinstance(layer, dict):
|
|
|
|
for key, value in sorted(layer.items(), key=sort_dict_key):
|
|
|
|
if isinstance(value, (dict, list)):
|
|
|
|
print(indent_str, key + ':', line_info(value, **kwargs))
|
|
|
|
dump_dict(value, indent_count + 2)
|
|
|
|
else:
|
|
|
|
print(indent_str, key + ':', value)
|
|
|
|
indent_str = indent_count * ' '
|
|
|
|
if isinstance(layer, (list, tuple)):
|
|
|
|
for i in layer:
|
|
|
|
if isinstance(i, dict):
|
|
|
|
dump_dict(i, indent_count + 2, True)
|
|
|
|
else:
|
|
|
|
print(' ', indent_str, i)
|
|
|
|
|
|
|
|
|
|
|
|
def read_config(path):
|
2018-06-02 22:22:20 +02:00
|
|
|
_LOGGER.info("Reading configuration...")
|
2018-04-10 20:18:02 +02:00
|
|
|
try:
|
|
|
|
res = load_config(path)
|
2018-04-11 18:29:21 +02:00
|
|
|
except ESPHomeYAMLError as err:
|
|
|
|
_LOGGER.error(u"Error while reading config: %s", err)
|
2018-04-10 20:18:02 +02:00
|
|
|
return None
|
2018-04-07 01:23:03 +02:00
|
|
|
excepts = {}
|
2018-06-02 22:22:20 +02:00
|
|
|
for message, domain, config in res.errors:
|
|
|
|
domain = domain or u"General Error"
|
|
|
|
excepts.setdefault(domain, []).append(message)
|
|
|
|
if config is not None:
|
|
|
|
excepts[domain].append(config)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
if excepts:
|
|
|
|
print(color('bold_white', u"Failed config"))
|
|
|
|
for domain, config in excepts.iteritems():
|
|
|
|
print(' ', color('bold_red', domain + ':'), color('red', '', reset='red'))
|
|
|
|
dump_dict(config, reset='red')
|
|
|
|
print(color('reset'))
|
|
|
|
return None
|
|
|
|
return dict(**res)
|