2019-04-22 21:56:30 +02:00
|
|
|
import json
|
2019-05-11 11:41:09 +02:00
|
|
|
import os
|
2019-04-22 21:56:30 +02:00
|
|
|
|
2019-12-07 18:28:55 +01:00
|
|
|
# pylint: disable=unused-import
|
2019-12-04 17:13:34 +01:00
|
|
|
from esphome.config import load_config, _format_vol_invalid, Config
|
|
|
|
from esphome.core import CORE, DocumentRange
|
2019-12-07 18:28:55 +01:00
|
|
|
import esphome.config_validation as cv
|
2019-04-22 21:56:30 +02:00
|
|
|
|
2019-12-04 17:13:34 +01:00
|
|
|
# pylint: disable=unused-import, wrong-import-order
|
|
|
|
from typing import Optional
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
|
|
|
|
def _get_invalid_range(res, invalid):
|
2019-12-07 18:28:55 +01:00
|
|
|
# type: (Config, cv.Invalid) -> Optional[DocumentRange]
|
2019-05-11 11:41:09 +02:00
|
|
|
return res.get_deepest_document_range_for_path(invalid.path)
|
2019-04-22 21:56:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _dump_range(range):
|
|
|
|
# type: (Optional[DocumentRange]) -> Optional[dict]
|
|
|
|
if range is None:
|
|
|
|
return None
|
|
|
|
return {
|
|
|
|
'document': range.start_mark.document,
|
|
|
|
'start_line': range.start_mark.line,
|
|
|
|
'start_col': range.start_mark.column,
|
|
|
|
'end_line': range.end_mark.line,
|
|
|
|
'end_col': range.end_mark.column,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-07 18:28:55 +01:00
|
|
|
class VSCodeResult:
|
2019-04-22 21:56:30 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.yaml_errors = []
|
|
|
|
self.validation_errors = []
|
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
return json.dumps({
|
|
|
|
'type': 'result',
|
|
|
|
'yaml_errors': self.yaml_errors,
|
|
|
|
'validation_errors': self.validation_errors,
|
|
|
|
})
|
|
|
|
|
|
|
|
def add_yaml_error(self, message):
|
|
|
|
self.yaml_errors.append({
|
|
|
|
'message': message,
|
|
|
|
})
|
|
|
|
|
|
|
|
def add_validation_error(self, range_, message):
|
|
|
|
self.validation_errors.append({
|
|
|
|
'range': _dump_range(range_),
|
|
|
|
'message': message,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2019-05-11 11:41:09 +02:00
|
|
|
def read_config(args):
|
2019-04-22 21:56:30 +02:00
|
|
|
while True:
|
|
|
|
CORE.reset()
|
2019-12-07 18:28:55 +01:00
|
|
|
data = json.loads(input())
|
2019-04-22 21:56:30 +02:00
|
|
|
assert data['type'] == 'validate'
|
|
|
|
CORE.vscode = True
|
2019-05-11 11:41:09 +02:00
|
|
|
CORE.ace = args.ace
|
|
|
|
f = data['file']
|
|
|
|
if CORE.ace:
|
2019-07-01 11:09:06 +02:00
|
|
|
CORE.config_path = os.path.join(args.configuration[0], f)
|
2019-05-11 11:41:09 +02:00
|
|
|
else:
|
|
|
|
CORE.config_path = data['file']
|
2019-04-22 21:56:30 +02:00
|
|
|
vs = VSCodeResult()
|
|
|
|
try:
|
2020-06-21 20:33:01 +02:00
|
|
|
res = load_config(dict(args.substitution) if args.substitution else {})
|
2019-04-22 21:56:30 +02:00
|
|
|
except Exception as err: # pylint: disable=broad-except
|
2019-12-07 18:28:55 +01:00
|
|
|
vs.add_yaml_error(str(err))
|
2019-04-22 21:56:30 +02:00
|
|
|
else:
|
|
|
|
for err in res.errors:
|
2019-05-11 11:41:09 +02:00
|
|
|
try:
|
|
|
|
range_ = _get_invalid_range(res, err)
|
|
|
|
vs.add_validation_error(range_, _format_vol_invalid(err, res))
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
continue
|
2019-04-22 21:56:30 +02:00
|
|
|
print(vs.dump())
|