1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-06 21:32:21 +01:00
* Add black

Update pre commit

Update pre commit

add empty line

* Format with black
This commit is contained in:
Guillermo Ruffino
2021-03-07 16:03:16 -03:00
committed by GitHub
parent 2b60b0f1fa
commit 69879920eb
398 changed files with 21624 additions and 12644 deletions

View File

@@ -11,8 +11,13 @@ from contextlib import contextmanager
import voluptuous as vol
from esphome import core, core_config, yaml_util
from esphome.const import CONF_ESPHOME, CONF_PLATFORM, ESP_PLATFORMS, CONF_PACKAGES, \
CONF_SUBSTITUTIONS
from esphome.const import (
CONF_ESPHOME,
CONF_PLATFORM,
ESP_PLATFORMS,
CONF_PACKAGES,
CONF_SUBSTITUTIONS,
)
from esphome.core import CORE, EsphomeError # noqa
from esphome.helpers import color, indent
from esphome.util import safe_print, OrderedDict
@@ -36,39 +41,39 @@ class ComponentManifest:
@property
def is_platform_component(self):
return getattr(self.module, 'IS_PLATFORM_COMPONENT', False)
return getattr(self.module, "IS_PLATFORM_COMPONENT", False)
@property
def config_schema(self):
return getattr(self.module, 'CONFIG_SCHEMA', None)
return getattr(self.module, "CONFIG_SCHEMA", None)
@property
def is_multi_conf(self):
return getattr(self.module, 'MULTI_CONF', False)
return getattr(self.module, "MULTI_CONF", False)
@property
def to_code(self):
return getattr(self.module, 'to_code', None)
return getattr(self.module, "to_code", None)
@property
def esp_platforms(self):
return getattr(self.module, 'ESP_PLATFORMS', ESP_PLATFORMS)
return getattr(self.module, "ESP_PLATFORMS", ESP_PLATFORMS)
@property
def dependencies(self):
return getattr(self.module, 'DEPENDENCIES', [])
return getattr(self.module, "DEPENDENCIES", [])
@property
def conflicts_with(self):
return getattr(self.module, 'CONFLICTS_WITH', [])
return getattr(self.module, "CONFLICTS_WITH", [])
@property
def auto_load(self):
return getattr(self.module, 'AUTO_LOAD', [])
return getattr(self.module, "AUTO_LOAD", [])
@property
def codeowners(self) -> List[str]:
return getattr(self.module, 'CODEOWNERS', [])
return getattr(self.module, "CODEOWNERS", [])
def _get_flags_set(self, name, config):
if not hasattr(self.module, name):
@@ -85,11 +90,11 @@ class ComponentManifest:
@property
def source_files(self):
if self._is_core:
core_p = os.path.abspath(os.path.join(os.path.dirname(__file__), 'core'))
source_files = core.find_source_files(os.path.join(core_p, 'dummy'))
core_p = os.path.abspath(os.path.join(os.path.dirname(__file__), "core"))
source_files = core.find_source_files(os.path.join(core_p, "dummy"))
ret = {}
for f in source_files:
ret[f'esphome/core/{f}'] = os.path.join(core_p, f)
ret[f"esphome/core/{f}"] = os.path.join(core_p, f)
return ret
source_files = core.find_source_files(self.module.__file__)
@@ -100,13 +105,15 @@ class ComponentManifest:
full_file = os.path.join(directory, x)
rel = os.path.relpath(full_file, self.base_components_path)
# Always use / for C++ include names
rel = rel.replace(os.sep, '/')
target_file = f'esphome/components/{rel}'
rel = rel.replace(os.sep, "/")
target_file = f"esphome/components/{rel}"
ret[target_file] = full_file
return ret
CORE_COMPONENTS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), 'components'))
CORE_COMPONENTS_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), "components")
)
_UNDEF = object()
CUSTOM_COMPONENTS_PATH = _UNDEF
@@ -115,7 +122,7 @@ def _mount_config_dir():
global CUSTOM_COMPONENTS_PATH
if CUSTOM_COMPONENTS_PATH is not _UNDEF:
return
custom_path = os.path.abspath(os.path.join(CORE.config_dir, 'custom_components'))
custom_path = os.path.abspath(os.path.join(CORE.config_dir, "custom_components"))
if not os.path.isdir(custom_path):
CUSTOM_COMPONENTS_PATH = None
return
@@ -131,25 +138,29 @@ def _lookup_module(domain, is_platform):
_mount_config_dir()
# First look for custom_components
try:
module = importlib.import_module(f'custom_components.{domain}')
module = importlib.import_module(f"custom_components.{domain}")
except ImportError as e:
# ImportError when no such module
if 'No module named' not in str(e):
_LOGGER.warning("Unable to import custom component %s:", domain, exc_info=True)
if "No module named" not in str(e):
_LOGGER.warning(
"Unable to import custom component %s:", domain, exc_info=True
)
except Exception: # pylint: disable=broad-except
# Other error means component has an issue
_LOGGER.error("Unable to load custom component %s:", domain, exc_info=True)
return None
else:
# Found in custom components
manif = ComponentManifest(module, CUSTOM_COMPONENTS_PATH, is_platform=is_platform)
manif = ComponentManifest(
module, CUSTOM_COMPONENTS_PATH, is_platform=is_platform
)
_COMPONENT_CACHE[domain] = manif
return manif
try:
module = importlib.import_module(f'esphome.components.{domain}')
module = importlib.import_module(f"esphome.components.{domain}")
except ImportError as e:
if 'No module named' not in str(e):
if "No module named" not in str(e):
_LOGGER.error("Unable to import component %s:", domain, exc_info=True)
return None
except Exception: # pylint: disable=broad-except
@@ -162,17 +173,20 @@ def _lookup_module(domain, is_platform):
def get_component(domain):
assert '.' not in domain
assert "." not in domain
return _lookup_module(domain, False)
def get_platform(domain, platform):
full = f'{platform}.{domain}'
full = f"{platform}.{domain}"
return _lookup_module(full, True)
_COMPONENT_CACHE['esphome'] = ComponentManifest(
core_config, CORE_COMPONENTS_PATH, is_core=True, is_platform=False,
_COMPONENT_CACHE["esphome"] = ComponentManifest(
core_config,
CORE_COMPONENTS_PATH,
is_core=True,
is_platform=False,
)
@@ -197,7 +211,7 @@ ConfigPath = List[Union[str, int]]
def _path_begins_with(path, other): # type: (ConfigPath, ConfigPath) -> bool
if len(path) < len(other):
return False
return path[:len(other)] == other
return path[: len(other)] == other
class Config(OrderedDict):
@@ -328,7 +342,7 @@ def do_id_pass(result): # type: (Config) -> None
# Look for duplicate definitions
match = next((v for v in declare_ids if v[0].id == id.id), None)
if match is not None:
opath = '->'.join(str(v) for v in match[1])
opath = "->".join(str(v) for v in match[1])
result.add_str_error(f"ID {id.id} redefined! Check {opath}", path)
continue
declare_ids.append((id, path))
@@ -348,21 +362,31 @@ def do_id_pass(result): # type: (Config) -> None
if match is None:
# No declared ID with this name
import difflib
error = ("Couldn't find ID '{}'. Please check you have defined "
"an ID with that name in your configuration.".format(id.id))
error = (
"Couldn't find ID '{}'. Please check you have defined "
"an ID with that name in your configuration.".format(id.id)
)
# Find candidates
matches = difflib.get_close_matches(id.id, [v[0].id for v in declare_ids])
matches = difflib.get_close_matches(
id.id, [v[0].id for v in declare_ids]
)
if matches:
matches_s = ', '.join(f'"{x}"' for x in matches)
matches_s = ", ".join(f'"{x}"' for x in matches)
error += f" These IDs look similar: {matches_s}."
result.add_str_error(error, path)
continue
if not isinstance(match.type, MockObjClass) or not isinstance(id.type, MockObjClass):
if not isinstance(match.type, MockObjClass) or not isinstance(
id.type, MockObjClass
):
continue
if not match.type.inherits_from(id.type):
result.add_str_error("ID '{}' of type {} doesn't inherit from {}. Please "
"double check your ID is pointing to the correct value"
"".format(id.id, match.type, id.type), path)
result.add_str_error(
"ID '{}' of type {} doesn't inherit from {}. Please "
"double check your ID is pointing to the correct value"
"".format(id.id, match.type, id.type),
path,
)
if id.id is None and id.type is not None:
for v in declare_ids:
@@ -385,12 +409,14 @@ def recursive_check_replaceme(value):
return cv.Schema({cv.valid: recursive_check_replaceme})(value)
if isinstance(value, ESPForceValue):
pass
if isinstance(value, str) and value == 'REPLACEME':
raise cv.Invalid("Found 'REPLACEME' in configuration, this is most likely an error. "
"Please make sure you have replaced all fields from the sample "
"configuration.\n"
"If you want to use the literal REPLACEME string, "
"please use \"!force REPLACEME\"")
if isinstance(value, str) and value == "REPLACEME":
raise cv.Invalid(
"Found 'REPLACEME' in configuration, this is most likely an error. "
"Please make sure you have replaced all fields from the sample "
"configuration.\n"
"If you want to use the literal REPLACEME string, "
'please use "!force REPLACEME"'
)
return value
@@ -400,6 +426,7 @@ def validate_config(config, command_line_substitutions):
# 0. Load packages
if CONF_PACKAGES in config:
from esphome.components.packages import do_packages_pass
result.add_output_path([CONF_PACKAGES], CONF_PACKAGES)
try:
config = do_packages_pass(config)
@@ -411,7 +438,11 @@ def validate_config(config, command_line_substitutions):
# 1. Load substitutions
if CONF_SUBSTITUTIONS in config:
from esphome.components import substitutions
result[CONF_SUBSTITUTIONS] = {**config[CONF_SUBSTITUTIONS], **command_line_substitutions}
result[CONF_SUBSTITUTIONS] = {
**config[CONF_SUBSTITUTIONS],
**command_line_substitutions,
}
result.add_output_path([CONF_SUBSTITUTIONS], CONF_SUBSTITUTIONS)
try:
substitutions.do_substitution_pass(config, command_line_substitutions)
@@ -425,14 +456,19 @@ def validate_config(config, command_line_substitutions):
except vol.Invalid as err:
result.add_error(err)
if 'esphomeyaml' in config:
_LOGGER.warning("The esphomeyaml section has been renamed to esphome in 1.11.0. "
"Please replace 'esphomeyaml:' in your configuration with 'esphome:'.")
config[CONF_ESPHOME] = config.pop('esphomeyaml')
if "esphomeyaml" in config:
_LOGGER.warning(
"The esphomeyaml section has been renamed to esphome in 1.11.0. "
"Please replace 'esphomeyaml:' in your configuration with 'esphome:'."
)
config[CONF_ESPHOME] = config.pop("esphomeyaml")
if CONF_ESPHOME not in config:
result.add_str_error("'esphome' section missing from configuration. Please make sure "
"your configuration has an 'esphome:' line in it.", [])
result.add_str_error(
"'esphome' section missing from configuration. Please make sure "
"your configuration has an 'esphome:' line in it.",
[],
)
return result
# 2. Load partial core config
@@ -454,7 +490,9 @@ def validate_config(config, command_line_substitutions):
load_queue.append((domain, conf))
# List of items to enter next stage
check_queue = [] # type: List[Tuple[ConfigPath, str, ConfigType, ComponentManifest]]
check_queue = (
[]
) # type: List[Tuple[ConfigPath, str, ConfigType, ComponentManifest]]
# This step handles:
# - Adding output path
@@ -463,7 +501,7 @@ def validate_config(config, command_line_substitutions):
while load_queue:
domain, conf = load_queue.popleft()
if domain.startswith('.'):
if domain.startswith("."):
# Ignore top-level keys starting with a dot
continue
result.add_output_path([domain], domain)
@@ -499,19 +537,19 @@ def validate_config(config, command_line_substitutions):
for i, p_config in enumerate(conf):
path = [domain, i]
# Construct temporary unknown output path
p_domain = f'{domain}.unknown'
p_domain = f"{domain}.unknown"
result.add_output_path(path, p_domain)
result[domain][i] = p_config
if not isinstance(p_config, dict):
result.add_str_error("Platform schemas must be key-value pairs.", path)
continue
p_name = p_config.get('platform')
p_name = p_config.get("platform")
if p_name is None:
result.add_str_error("No platform specified! See 'platform' key.", path)
continue
# Remove temp output path and construct new one
result.remove_output_path(path, p_domain)
p_domain = f'{domain}.{p_name}'
p_domain = f"{domain}.{p_name}"
result.add_output_path(path, p_domain)
# Try Load platform
platform = get_platform(domain, p_name)
@@ -544,8 +582,10 @@ def validate_config(config, command_line_substitutions):
success = True
for dependency in comp.dependencies:
if dependency not in config:
result.add_str_error("Component {} requires component {}"
"".format(domain, dependency), path)
result.add_str_error(
"Component {} requires component {}" "".format(domain, dependency),
path,
)
success = False
if not success:
continue
@@ -553,22 +593,32 @@ def validate_config(config, command_line_substitutions):
success = True
for conflict in comp.conflicts_with:
if conflict in config:
result.add_str_error("Component {} cannot be used together with component {}"
"".format(domain, conflict), path)
result.add_str_error(
"Component {} cannot be used together with component {}"
"".format(domain, conflict),
path,
)
success = False
if not success:
continue
if CORE.esp_platform not in comp.esp_platforms:
result.add_str_error("Component {} doesn't support {}.".format(domain,
CORE.esp_platform),
path)
result.add_str_error(
"Component {} doesn't support {}.".format(domain, CORE.esp_platform),
path,
)
continue
if not comp.is_platform_component and comp.config_schema is None and \
not isinstance(conf, core.AutoLoad):
result.add_str_error("Component {} cannot be loaded via YAML "
"(no CONFIG_SCHEMA).".format(domain), path)
if (
not comp.is_platform_component
and comp.config_schema is None
and not isinstance(conf, core.AutoLoad)
):
result.add_str_error(
"Component {} cannot be loaded via YAML "
"(no CONFIG_SCHEMA).".format(domain),
path,
)
continue
if comp.is_multi_conf:
@@ -588,13 +638,13 @@ def validate_config(config, command_line_substitutions):
if comp.is_platform:
# Remove 'platform' key for validation
input_conf = OrderedDict(conf)
platform_val = input_conf.pop('platform')
platform_val = input_conf.pop("platform")
validated = comp.config_schema(input_conf)
# Ensure result is OrderedDict so we can call move_to_end
if not isinstance(validated, OrderedDict):
validated = OrderedDict(validated)
validated['platform'] = platform_val
validated.move_to_end('platform', last=False)
validated["platform"] = platform_val
validated.move_to_end("platform", last=False)
result.set_by_path(path, validated)
else:
validated = comp.config_schema(conf)
@@ -619,18 +669,20 @@ def _nested_getitem(data, path):
def humanize_error(config, validation_error):
validation_error = str(validation_error)
m = re.match(r'^(.*?)\s*(?:for dictionary value )?@ data\[.*$', validation_error, re.DOTALL)
m = re.match(
r"^(.*?)\s*(?:for dictionary value )?@ data\[.*$", validation_error, re.DOTALL
)
if m is not None:
validation_error = m.group(1)
validation_error = validation_error.strip()
if not validation_error.endswith('.'):
validation_error += '.'
if not validation_error.endswith("."):
validation_error += "."
return validation_error
def _get_parent_name(path, config):
if not path:
return '<root>'
return "<root>"
for domain_path, domain in config.output_paths:
if _path_begins_with(path, domain_path):
if len(path) > len(domain_path):
@@ -642,20 +694,22 @@ def _get_parent_name(path, config):
def _format_vol_invalid(ex, config):
# type: (vol.Invalid, Config) -> str
message = ''
message = ""
paren = _get_parent_name(ex.path[:-1], config)
if isinstance(ex, ExtraKeysInvalid):
if ex.candidates:
message += '[{}] is an invalid option for [{}]. Did you mean {}?'.format(
ex.path[-1], paren, ', '.join(f'[{x}]' for x in ex.candidates))
message += "[{}] is an invalid option for [{}]. Did you mean {}?".format(
ex.path[-1], paren, ", ".join(f"[{x}]" for x in ex.candidates)
)
else:
message += '[{}] is an invalid option for [{}]. Please check the indentation.'.format(
ex.path[-1], paren)
elif 'extra keys not allowed' in str(ex):
message += '[{}] is an invalid option for [{}].'.format(ex.path[-1], paren)
elif 'required key not provided' in str(ex):
message += "[{}] is an invalid option for [{}]. Please check the indentation.".format(
ex.path[-1], paren
)
elif "extra keys not allowed" in str(ex):
message += "[{}] is an invalid option for [{}].".format(ex.path[-1], paren)
elif "required key not provided" in str(ex):
message += "'{}' is a required option for [{}].".format(ex.path[-1], paren)
else:
message += humanize_error(config, ex)
@@ -707,8 +761,8 @@ def line_info(config, path, highlight=True):
if obj:
mark = obj.start_mark
source = "[source {}:{}]".format(mark.document, mark.line + 1)
return color('cyan', source)
return 'None'
return color("cyan", source)
return "None"
def _print_on_next_line(obj):
@@ -724,90 +778,94 @@ def _print_on_next_line(obj):
def dump_dict(config, path, at_root=True):
# type: (Config, ConfigPath, bool) -> Tuple[str, bool]
conf = config.get_nested_item(path)
ret = ''
ret = ""
multiline = False
if at_root:
error = config.get_error_for_path(path)
if error is not None:
ret += '\n' + color('bold_red', _format_vol_invalid(error, config)) + '\n'
ret += "\n" + color("bold_red", _format_vol_invalid(error, config)) + "\n"
if isinstance(conf, (list, tuple)):
multiline = True
if not conf:
ret += '[]'
ret += "[]"
multiline = False
for i in range(len(conf)):
path_ = path + [i]
error = config.get_error_for_path(path_)
if error is not None:
ret += '\n' + color('bold_red', _format_vol_invalid(error, config)) + '\n'
ret += (
"\n" + color("bold_red", _format_vol_invalid(error, config)) + "\n"
)
sep = '- '
sep = "- "
if config.is_in_error_path(path_):
sep = color('red', sep)
sep = color("red", sep)
msg, _ = dump_dict(config, path_, at_root=False)
msg = indent(msg)
inf = line_info(config, path_, highlight=config.is_in_error_path(path_))
if inf is not None:
msg = inf + '\n' + msg
msg = inf + "\n" + msg
elif msg:
msg = msg[2:]
ret += sep + msg + '\n'
ret += sep + msg + "\n"
elif isinstance(conf, dict):
multiline = True
if not conf:
ret += '{}'
ret += "{}"
multiline = False
for k in conf.keys():
path_ = path + [k]
error = config.get_error_for_path(path_)
if error is not None:
ret += '\n' + color('bold_red', _format_vol_invalid(error, config)) + '\n'
ret += (
"\n" + color("bold_red", _format_vol_invalid(error, config)) + "\n"
)
st = f'{k}: '
st = f"{k}: "
if config.is_in_error_path(path_):
st = color('red', st)
st = color("red", st)
msg, m = dump_dict(config, path_, at_root=False)
inf = line_info(config, path_, highlight=config.is_in_error_path(path_))
if m:
msg = '\n' + indent(msg)
msg = "\n" + indent(msg)
if inf is not None:
if m:
msg = ' ' + inf + msg
msg = " " + inf + msg
else:
msg = msg + ' ' + inf
ret += st + msg + '\n'
msg = msg + " " + inf
ret += st + msg + "\n"
elif isinstance(conf, str):
if is_secret(conf):
conf = '!secret {}'.format(is_secret(conf))
conf = "!secret {}".format(is_secret(conf))
if not conf:
conf += "''"
if len(conf) > 80:
conf = '|-\n' + indent(conf)
conf = "|-\n" + indent(conf)
error = config.get_error_for_path(path)
col = 'bold_red' if error else 'white'
col = "bold_red" if error else "white"
ret += color(col, str(conf))
elif isinstance(conf, core.Lambda):
if is_secret(conf):
conf = '!secret {}'.format(is_secret(conf))
conf = "!secret {}".format(is_secret(conf))
conf = '!lambda |-\n' + indent(str(conf.value))
conf = "!lambda |-\n" + indent(str(conf.value))
error = config.get_error_for_path(path)
col = 'bold_red' if error else 'white'
col = "bold_red" if error else "white"
ret += color(col, conf)
elif conf is None:
pass
else:
error = config.get_error_for_path(path)
col = 'bold_red' if error else 'white'
col = "bold_red" if error else "white"
ret += color(col, str(conf))
multiline = '\n' in ret
multiline = "\n" in ret
return ret, multiline
@@ -817,7 +875,9 @@ def strip_default_ids(config):
to_remove = []
for i, x in enumerate(config):
x = config[i] = strip_default_ids(x)
if (isinstance(x, core.ID) and not x.is_manual) or isinstance(x, core.AutoLoad):
if (isinstance(x, core.ID) and not x.is_manual) or isinstance(
x, core.AutoLoad
):
to_remove.append(x)
for x in to_remove:
config.remove(x)
@@ -825,7 +885,9 @@ def strip_default_ids(config):
to_remove = []
for k, v in config.items():
v = config[k] = strip_default_ids(v)
if (isinstance(v, core.ID) and not v.is_manual) or isinstance(v, core.AutoLoad):
if (isinstance(v, core.ID) and not v.is_manual) or isinstance(
v, core.AutoLoad
):
to_remove.append(k)
for k in to_remove:
config.pop(k)
@@ -843,16 +905,16 @@ def read_config(command_line_substitutions):
if not CORE.verbose:
res = strip_default_ids(res)
safe_print(color('bold_red', "Failed config"))
safe_print('')
safe_print(color("bold_red", "Failed config"))
safe_print("")
for path, domain in res.output_paths:
if not res.is_in_error_path(path):
continue
errstr = color('bold_red', f'{domain}:')
errstr = color("bold_red", f"{domain}:")
errline = line_info(res, path)
if errline:
errstr += ' ' + errline
errstr += " " + errline
safe_print(errstr)
safe_print(indent(dump_dict(res, path)[0]))
return None