Fix-esphome-validation-line-number (#3815)

This commit is contained in:
Guillermo Ruffino 2022-09-20 02:23:55 -03:00 committed by Jesse Hills
parent 97e067a277
commit 33f296e05b
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
3 changed files with 23 additions and 9 deletions

View File

@ -165,15 +165,19 @@ class Config(OrderedDict, fv.FinalValidateConfig):
return err return err
return None return None
def get_deepest_document_range_for_path(self, path): def get_deepest_document_range_for_path(self, path, get_key=False):
# type: (ConfigPath) -> Optional[ESPHomeDataBase] # type: (ConfigPath, bool) -> Optional[ESPHomeDataBase]
data = self data = self
doc_range = None doc_range = None
for item_index in path: for index, path_item in enumerate(path):
try: try:
if item_index in data: if path_item in data:
doc_range = [x for x in data.keys() if x == item_index][0].esp_range key_data = [x for x in data.keys() if x == path_item][0]
data = data[item_index] if isinstance(key_data, ESPHomeDataBase):
doc_range = key_data.esp_range
if get_key and index == len(path) - 1:
return doc_range
data = data[path_item]
except (KeyError, IndexError, TypeError, AttributeError): except (KeyError, IndexError, TypeError, AttributeError):
return doc_range return doc_range
if isinstance(data, core.ID): if isinstance(data, core.ID):
@ -281,7 +285,7 @@ class ConfigValidationStep(abc.ABC):
class LoadValidationStep(ConfigValidationStep): class LoadValidationStep(ConfigValidationStep):
"""Load step, this step is called once for each domain config fragment. """Load step, this step is called once for each domain config fragment.
Responsibilties: Responsibilities:
- Load component code - Load component code
- Ensure all AUTO_LOADs are added - Ensure all AUTO_LOADs are added
- Set output paths of result - Set output paths of result
@ -738,6 +742,10 @@ def validate_config(config, command_line_substitutions) -> Config:
result.add_validation_step(LoadValidationStep(key, config[key])) result.add_validation_step(LoadValidationStep(key, config[key]))
result.run_validation_steps() result.run_validation_steps()
if result.errors:
# do not try to validate further as we don't know what the target is
return result
for domain, conf in config.items(): for domain, conf in config.items():
result.add_validation_step(LoadValidationStep(domain, conf)) result.add_validation_step(LoadValidationStep(domain, conf))
result.add_validation_step(IDPassValidationStep()) result.add_validation_step(IDPassValidationStep())

View File

@ -179,7 +179,11 @@ def preload_core_config(config, result):
] ]
if not has_oldstyle and not newstyle_found: if not has_oldstyle and not newstyle_found:
raise cv.Invalid("Platform missing for core options!", [CONF_ESPHOME]) raise cv.Invalid(
"Platform missing. You must include one of the available platform keys: "
+ ", ".join(TARGET_PLATFORMS),
[CONF_ESPHOME],
)
if has_oldstyle and newstyle_found: if has_oldstyle and newstyle_found:
raise cv.Invalid( raise cv.Invalid(
f"Please remove the `platform` key from the [esphome] block. You're already using the new style with the [{conf[CONF_PLATFORM]}] block", f"Please remove the `platform` key from the [esphome] block. You're already using the new style with the [{conf[CONF_PLATFORM]}] block",

View File

@ -12,7 +12,9 @@ from typing import Optional
def _get_invalid_range(res, invalid): def _get_invalid_range(res, invalid):
# type: (Config, cv.Invalid) -> Optional[DocumentRange] # type: (Config, cv.Invalid) -> Optional[DocumentRange]
return res.get_deepest_document_range_for_path(invalid.path) return res.get_deepest_document_range_for_path(
invalid.path, invalid.error_message == "extra keys not allowed"
)
def _dump_range(range): def _dump_range(range):