mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Reject template select/number/switches that don't handle user input (#2230)
This commit is contained in:
		| @@ -29,12 +29,16 @@ def validate_min_max(config): | ||||
|  | ||||
| def validate(config): | ||||
|     if CONF_LAMBDA in config: | ||||
|         if CONF_OPTIMISTIC in config: | ||||
|         if config[CONF_OPTIMISTIC]: | ||||
|             raise cv.Invalid("optimistic cannot be used with lambda") | ||||
|         if CONF_INITIAL_VALUE 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 number being set." | ||||
|         ) | ||||
|     return config | ||||
|  | ||||
|  | ||||
| @@ -46,7 +50,7 @@ CONFIG_SCHEMA = cv.All( | ||||
|             cv.Required(CONF_MIN_VALUE): cv.float_, | ||||
|             cv.Required(CONF_STEP): cv.positive_float, | ||||
|             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_INITIAL_VALUE): cv.float_, | ||||
|             cv.Optional(CONF_RESTORE_VALUE): cv.boolean, | ||||
| @@ -75,8 +79,7 @@ async def to_code(config): | ||||
|         cg.add(var.set_template(template_)) | ||||
|  | ||||
|     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: | ||||
|             cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE])) | ||||
|         if CONF_RESTORE_VALUE in config: | ||||
|   | ||||
| @@ -30,6 +30,21 @@ def validate_initial_value_in_options(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( | ||||
|     select.SELECT_SCHEMA.extend( | ||||
|         { | ||||
| @@ -38,13 +53,14 @@ CONFIG_SCHEMA = cv.All( | ||||
|                 cv.ensure_list(cv.string_strict), cv.Length(min=1) | ||||
|             ), | ||||
|             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_INITIAL_OPTION): cv.string_strict, | ||||
|             cv.Optional(CONF_RESTORE_VALUE): cv.boolean, | ||||
|         } | ||||
|     ).extend(cv.polling_component_schema("60s")), | ||||
|     validate_initial_value_in_options, | ||||
|     validate, | ||||
| ) | ||||
|  | ||||
|  | ||||
| @@ -60,9 +76,7 @@ async def to_code(config): | ||||
|         cg.add(var.set_template(template_)) | ||||
|  | ||||
|     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])) | ||||
|  | ||||
|         if CONF_RESTORE_VALUE in config: | ||||
|   | ||||
| @@ -16,17 +16,38 @@ from .. import template_ns | ||||
|  | ||||
| TemplateSwitch = template_ns.class_("TemplateSwitch", switch.Switch, cg.Component) | ||||
|  | ||||
| CONFIG_SCHEMA = 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) | ||||
|  | ||||
| def validate(config): | ||||
|     if ( | ||||
|         not config[CONF_OPTIMISTIC] | ||||
|         and CONF_TURN_ON_ACTION not in config | ||||
|         and CONF_TURN_OFF_ACTION not in config | ||||
|     ): | ||||
|         raise cv.Invalid( | ||||
|             "Either optimistic mode must be enabled, or turn_on_action or turn_off_action must be set, " | ||||
|             "to handle the switch being set." | ||||
|         ) | ||||
|     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): | ||||
|   | ||||
| @@ -1862,6 +1862,7 @@ switch: | ||||
|       inverted: False | ||||
|   - platform: template | ||||
|     id: ble1_status | ||||
|     optimistic: true | ||||
|  | ||||
| fan: | ||||
|   - platform: binary | ||||
|   | ||||
		Reference in New Issue
	
	Block a user