mirror of
https://github.com/esphome/esphome.git
synced 2024-11-29 02:04:13 +01:00
Reject template select/number/switches that don't handle user input (#2230)
This commit is contained in:
parent
f364788c03
commit
54de0ca0da
4 changed files with 58 additions and 19 deletions
|
@ -29,12 +29,16 @@ def validate_min_max(config):
|
||||||
|
|
||||||
def validate(config):
|
def validate(config):
|
||||||
if CONF_LAMBDA in config:
|
if CONF_LAMBDA in config:
|
||||||
if CONF_OPTIMISTIC in config:
|
if config[CONF_OPTIMISTIC]:
|
||||||
raise cv.Invalid("optimistic cannot be used with lambda")
|
raise cv.Invalid("optimistic cannot be used with lambda")
|
||||||
if CONF_INITIAL_VALUE in config:
|
if CONF_INITIAL_VALUE in config:
|
||||||
raise cv.Invalid("initial_value cannot be used with lambda")
|
raise cv.Invalid("initial_value cannot be used with lambda")
|
||||||
if CONF_RESTORE_VALUE in config:
|
if CONF_RESTORE_VALUE in config:
|
||||||
raise cv.Invalid("restore_value cannot be used with lambda")
|
raise cv.Invalid("restore_value cannot be used with lambda")
|
||||||
|
if not config[CONF_OPTIMISTIC] and CONF_SET_ACTION not in config:
|
||||||
|
raise cv.Invalid(
|
||||||
|
"Either optimistic mode must be enabled, or set_action must be set, to handle the number being set."
|
||||||
|
)
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,7 +50,7 @@ CONFIG_SCHEMA = cv.All(
|
||||||
cv.Required(CONF_MIN_VALUE): cv.float_,
|
cv.Required(CONF_MIN_VALUE): cv.float_,
|
||||||
cv.Required(CONF_STEP): cv.positive_float,
|
cv.Required(CONF_STEP): cv.positive_float,
|
||||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||||
cv.Optional(CONF_OPTIMISTIC): cv.boolean,
|
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
||||||
cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
|
cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
|
||||||
cv.Optional(CONF_INITIAL_VALUE): cv.float_,
|
cv.Optional(CONF_INITIAL_VALUE): cv.float_,
|
||||||
cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
|
cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
|
||||||
|
@ -75,8 +79,7 @@ async def to_code(config):
|
||||||
cg.add(var.set_template(template_))
|
cg.add(var.set_template(template_))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if CONF_OPTIMISTIC in config:
|
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
|
||||||
if CONF_INITIAL_VALUE in config:
|
if CONF_INITIAL_VALUE in config:
|
||||||
cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))
|
cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE]))
|
||||||
if CONF_RESTORE_VALUE in config:
|
if CONF_RESTORE_VALUE in config:
|
||||||
|
|
|
@ -30,6 +30,21 @@ def validate_initial_value_in_options(config):
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def validate(config):
|
||||||
|
if CONF_LAMBDA in config:
|
||||||
|
if config[CONF_OPTIMISTIC]:
|
||||||
|
raise cv.Invalid("optimistic cannot be used with lambda")
|
||||||
|
if CONF_INITIAL_OPTION in config:
|
||||||
|
raise cv.Invalid("initial_value cannot be used with lambda")
|
||||||
|
if CONF_RESTORE_VALUE in config:
|
||||||
|
raise cv.Invalid("restore_value cannot be used with lambda")
|
||||||
|
if not config[CONF_OPTIMISTIC] and CONF_SET_ACTION not in config:
|
||||||
|
raise cv.Invalid(
|
||||||
|
"Either optimistic mode must be enabled, or set_action must be set, to handle the option being set."
|
||||||
|
)
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
select.SELECT_SCHEMA.extend(
|
select.SELECT_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
|
@ -38,13 +53,14 @@ CONFIG_SCHEMA = cv.All(
|
||||||
cv.ensure_list(cv.string_strict), cv.Length(min=1)
|
cv.ensure_list(cv.string_strict), cv.Length(min=1)
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||||
cv.Optional(CONF_OPTIMISTIC): cv.boolean,
|
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
||||||
cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
|
cv.Optional(CONF_SET_ACTION): automation.validate_automation(single=True),
|
||||||
cv.Optional(CONF_INITIAL_OPTION): cv.string_strict,
|
cv.Optional(CONF_INITIAL_OPTION): cv.string_strict,
|
||||||
cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
|
cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
|
||||||
}
|
}
|
||||||
).extend(cv.polling_component_schema("60s")),
|
).extend(cv.polling_component_schema("60s")),
|
||||||
validate_initial_value_in_options,
|
validate_initial_value_in_options,
|
||||||
|
validate,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,9 +76,7 @@ async def to_code(config):
|
||||||
cg.add(var.set_template(template_))
|
cg.add(var.set_template(template_))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if CONF_OPTIMISTIC in config:
|
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
|
||||||
|
|
||||||
cg.add(var.set_initial_option(config[CONF_INITIAL_OPTION]))
|
cg.add(var.set_initial_option(config[CONF_INITIAL_OPTION]))
|
||||||
|
|
||||||
if CONF_RESTORE_VALUE in config:
|
if CONF_RESTORE_VALUE in config:
|
||||||
|
|
|
@ -16,17 +16,38 @@ from .. import template_ns
|
||||||
|
|
||||||
TemplateSwitch = template_ns.class_("TemplateSwitch", switch.Switch, cg.Component)
|
TemplateSwitch = template_ns.class_("TemplateSwitch", switch.Switch, cg.Component)
|
||||||
|
|
||||||
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend(
|
|
||||||
{
|
def validate(config):
|
||||||
cv.GenerateID(): cv.declare_id(TemplateSwitch),
|
if (
|
||||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
not config[CONF_OPTIMISTIC]
|
||||||
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
and CONF_TURN_ON_ACTION not in config
|
||||||
cv.Optional(CONF_ASSUMED_STATE, default=False): cv.boolean,
|
and CONF_TURN_OFF_ACTION not in config
|
||||||
cv.Optional(CONF_TURN_OFF_ACTION): automation.validate_automation(single=True),
|
):
|
||||||
cv.Optional(CONF_TURN_ON_ACTION): automation.validate_automation(single=True),
|
raise cv.Invalid(
|
||||||
cv.Optional(CONF_RESTORE_STATE, default=False): cv.boolean,
|
"Either optimistic mode must be enabled, or turn_on_action or turn_off_action must be set, "
|
||||||
}
|
"to handle the switch being set."
|
||||||
).extend(cv.COMPONENT_SCHEMA)
|
)
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = cv.All(
|
||||||
|
switch.SWITCH_SCHEMA.extend(
|
||||||
|
{
|
||||||
|
cv.GenerateID(): cv.declare_id(TemplateSwitch),
|
||||||
|
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||||
|
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
||||||
|
cv.Optional(CONF_ASSUMED_STATE, default=False): cv.boolean,
|
||||||
|
cv.Optional(CONF_TURN_OFF_ACTION): automation.validate_automation(
|
||||||
|
single=True
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_TURN_ON_ACTION): automation.validate_automation(
|
||||||
|
single=True
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_RESTORE_STATE, default=False): cv.boolean,
|
||||||
|
}
|
||||||
|
).extend(cv.COMPONENT_SCHEMA),
|
||||||
|
validate,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
|
|
|
@ -1862,6 +1862,7 @@ switch:
|
||||||
inverted: False
|
inverted: False
|
||||||
- platform: template
|
- platform: template
|
||||||
id: ble1_status
|
id: ble1_status
|
||||||
|
optimistic: true
|
||||||
|
|
||||||
fan:
|
fan:
|
||||||
- platform: binary
|
- platform: binary
|
||||||
|
|
Loading…
Reference in a new issue