mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 16:37:52 +01:00
Glmnet schema 202105 (#2220)
This commit is contained in:
parent
9218e85bd6
commit
37f322585e
@ -25,6 +25,11 @@ JSC_DESCRIPTION = "description"
|
|||||||
JSC_ONEOF = "oneOf"
|
JSC_ONEOF = "oneOf"
|
||||||
JSC_PROPERTIES = "properties"
|
JSC_PROPERTIES = "properties"
|
||||||
JSC_REF = "$ref"
|
JSC_REF = "$ref"
|
||||||
|
|
||||||
|
# this should be required, but YAML Language server completion does not work properly if required are specified.
|
||||||
|
# still needed for other features / checks
|
||||||
|
JSC_REQUIRED = "required_"
|
||||||
|
|
||||||
SIMPLE_AUTOMATION = "simple_automation"
|
SIMPLE_AUTOMATION = "simple_automation"
|
||||||
|
|
||||||
schema_names = {}
|
schema_names = {}
|
||||||
@ -295,9 +300,17 @@ def get_automation_schema(name, vschema):
|
|||||||
# * an object with automation's schema and a then key
|
# * an object with automation's schema and a then key
|
||||||
# with again a single action or an array of actions
|
# with again a single action or an array of actions
|
||||||
|
|
||||||
|
if len(extra_jschema[JSC_PROPERTIES]) == 0:
|
||||||
|
return get_ref(SIMPLE_AUTOMATION)
|
||||||
|
|
||||||
extra_jschema[JSC_PROPERTIES]["then"] = add_definition_array_or_single_object(
|
extra_jschema[JSC_PROPERTIES]["then"] = add_definition_array_or_single_object(
|
||||||
get_ref(JSC_ACTION)
|
get_ref(JSC_ACTION)
|
||||||
)
|
)
|
||||||
|
# if there is a required element in extra_jschema then this automation does not support
|
||||||
|
# directly a list of actions
|
||||||
|
if JSC_REQUIRED in extra_jschema:
|
||||||
|
return create_ref(name, extra_vschema, extra_jschema)
|
||||||
|
|
||||||
jschema = add_definition_array_or_single_object(get_ref(JSC_ACTION))
|
jschema = add_definition_array_or_single_object(get_ref(JSC_ACTION))
|
||||||
jschema[JSC_ANYOF].append(extra_jschema)
|
jschema[JSC_ANYOF].append(extra_jschema)
|
||||||
|
|
||||||
@ -370,9 +383,14 @@ def get_entry(parent_key, vschema):
|
|||||||
# everything else just accept string and let ESPHome validate
|
# everything else just accept string and let ESPHome validate
|
||||||
try:
|
try:
|
||||||
from esphome.core import ID
|
from esphome.core import ID
|
||||||
|
from esphome.automation import Trigger, Automation
|
||||||
|
|
||||||
v = vschema(None)
|
v = vschema(None)
|
||||||
if isinstance(v, ID):
|
if isinstance(v, ID):
|
||||||
|
if v.type.base != "script::Script" and (
|
||||||
|
v.type.inherits_from(Trigger) or v.type == Automation
|
||||||
|
):
|
||||||
|
return None
|
||||||
entry = {"type": "string", "id_type": v.type.base}
|
entry = {"type": "string", "id_type": v.type.base}
|
||||||
elif isinstance(v, str):
|
elif isinstance(v, str):
|
||||||
entry = {"type": "string"}
|
entry = {"type": "string"}
|
||||||
@ -494,9 +512,11 @@ def convert_schema(path, vschema, un_extend=True):
|
|||||||
output = {}
|
output = {}
|
||||||
|
|
||||||
if str(vschema) in ejs.hidden_schemas:
|
if str(vschema) in ejs.hidden_schemas:
|
||||||
# this can get another think twist. When adding this I've already figured out
|
if ejs.hidden_schemas[str(vschema)] == "automation":
|
||||||
# interval and script in other way
|
vschema = vschema(ejs.jschema_extractor)
|
||||||
if path not in ["interval", "script"]:
|
jschema = get_jschema(path, vschema, True)
|
||||||
|
return add_definition_array_or_single_object(jschema)
|
||||||
|
else:
|
||||||
vschema = vschema(ejs.jschema_extractor)
|
vschema = vschema(ejs.jschema_extractor)
|
||||||
|
|
||||||
if un_extend:
|
if un_extend:
|
||||||
@ -515,9 +535,8 @@ def convert_schema(path, vschema, un_extend=True):
|
|||||||
return rhs
|
return rhs
|
||||||
|
|
||||||
# merge
|
# merge
|
||||||
|
|
||||||
if JSC_ALLOF in lhs and JSC_ALLOF in rhs:
|
if JSC_ALLOF in lhs and JSC_ALLOF in rhs:
|
||||||
output = lhs[JSC_ALLOF]
|
output = lhs
|
||||||
for k in rhs[JSC_ALLOF]:
|
for k in rhs[JSC_ALLOF]:
|
||||||
merge(output[JSC_ALLOF], k)
|
merge(output[JSC_ALLOF], k)
|
||||||
elif JSC_ALLOF in lhs:
|
elif JSC_ALLOF in lhs:
|
||||||
@ -574,6 +593,7 @@ def convert_schema(path, vschema, un_extend=True):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
props = output[JSC_PROPERTIES] = {}
|
props = output[JSC_PROPERTIES] = {}
|
||||||
|
required = []
|
||||||
|
|
||||||
output["type"] = ["object", "null"]
|
output["type"] = ["object", "null"]
|
||||||
if DUMP_COMMENTS:
|
if DUMP_COMMENTS:
|
||||||
@ -616,13 +636,21 @@ def convert_schema(path, vschema, un_extend=True):
|
|||||||
if prop: # Deprecated (cv.Invalid) properties not added
|
if prop: # Deprecated (cv.Invalid) properties not added
|
||||||
props[str(k)] = prop
|
props[str(k)] = prop
|
||||||
# TODO: see required, sometimes completions doesn't show up because of this...
|
# TODO: see required, sometimes completions doesn't show up because of this...
|
||||||
# if isinstance(k, cv.Required):
|
if isinstance(k, cv.Required):
|
||||||
# required.append(str(k))
|
required.append(str(k))
|
||||||
try:
|
try:
|
||||||
if str(k.default) != "...":
|
if str(k.default) != "...":
|
||||||
prop["default"] = k.default()
|
default_value = k.default()
|
||||||
|
# Yaml validator fails if `"default": null` ends up in the json schema
|
||||||
|
if default_value is not None:
|
||||||
|
if prop["type"] == "string":
|
||||||
|
default_value = str(default_value)
|
||||||
|
prop["default"] = default_value
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if len(required) > 0:
|
||||||
|
output[JSC_REQUIRED] = required
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
@ -648,6 +676,7 @@ def add_pin_registry():
|
|||||||
internal = definitions[schema_name]
|
internal = definitions[schema_name]
|
||||||
definitions[schema_name]["additionalItems"] = False
|
definitions[schema_name]["additionalItems"] = False
|
||||||
definitions[f"PIN.{mode}_INTERNAL"] = internal
|
definitions[f"PIN.{mode}_INTERNAL"] = internal
|
||||||
|
internal[JSC_PROPERTIES]["number"] = {"type": ["number", "string"]}
|
||||||
schemas = [get_ref(f"PIN.{mode}_INTERNAL")]
|
schemas = [get_ref(f"PIN.{mode}_INTERNAL")]
|
||||||
schemas[0]["required"] = ["number"]
|
schemas[0]["required"] = ["number"]
|
||||||
# accept string and object, for internal shorthand pin IO:
|
# accept string and object, for internal shorthand pin IO:
|
||||||
|
Loading…
Reference in New Issue
Block a user