mirror of
https://github.com/esphome/esphome.git
synced 2025-09-16 10:12:21 +01:00
Dashboard editor live validation (#540)
* Dashboard editor validation * Improve range detection * Lint
This commit is contained in:
@@ -1,19 +1,16 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
from esphome.config import load_config, _format_vol_invalid
|
||||
from esphome.core import CORE
|
||||
from esphome.py_compat import text_type, safe_input
|
||||
from esphome.yaml_util import ESPHomeDataBase
|
||||
|
||||
|
||||
def _get_invalid_range(res, invalid):
|
||||
# type: (Config, vol.Invalid) -> Optional[DocumentRange]
|
||||
obj = res.get_deepest_value_for_path(invalid.path)
|
||||
if isinstance(obj, ESPHomeDataBase) and obj.esp_range is not None:
|
||||
return obj.esp_range
|
||||
return None
|
||||
return res.get_deepest_document_range_for_path(invalid.path)
|
||||
|
||||
|
||||
def _dump_range(range):
|
||||
@@ -53,13 +50,18 @@ class VSCodeResult(object):
|
||||
})
|
||||
|
||||
|
||||
def read_config():
|
||||
def read_config(args):
|
||||
while True:
|
||||
CORE.reset()
|
||||
data = json.loads(safe_input())
|
||||
assert data['type'] == 'validate'
|
||||
CORE.vscode = True
|
||||
CORE.config_path = data['file']
|
||||
CORE.ace = args.ace
|
||||
f = data['file']
|
||||
if CORE.ace:
|
||||
CORE.config_path = os.path.join(args.configuration, f)
|
||||
else:
|
||||
CORE.config_path = data['file']
|
||||
vs = VSCodeResult()
|
||||
try:
|
||||
res = load_config()
|
||||
@@ -67,6 +69,9 @@ def read_config():
|
||||
vs.add_yaml_error(text_type(err))
|
||||
else:
|
||||
for err in res.errors:
|
||||
range_ = _get_invalid_range(res, err)
|
||||
vs.add_validation_error(range_, _format_vol_invalid(err, res))
|
||||
try:
|
||||
range_ = _get_invalid_range(res, err)
|
||||
vs.add_validation_error(range_, _format_vol_invalid(err, res))
|
||||
except Exception: # pylint: disable=broad-except
|
||||
continue
|
||||
print(vs.dump())
|
||||
|
Reference in New Issue
Block a user