mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-29 22:24:26 +00:00 
			
		
		
		
	[switch] Fix schema generation (#8774)
This commit is contained in:
		| @@ -72,6 +72,9 @@ _SWITCH_SCHEMA = ( | ||||
|         { | ||||
|             cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTSwitchComponent), | ||||
|             cv.Optional(CONF_INVERTED): cv.boolean, | ||||
|             cv.Optional(CONF_RESTORE_MODE, default="ALWAYS_OFF"): cv.enum( | ||||
|                 RESTORE_MODES, upper=True, space="_" | ||||
|             ), | ||||
|             cv.Optional(CONF_ON_TURN_ON): automation.validate_automation( | ||||
|                 { | ||||
|                     cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SwitchTurnOnTrigger), | ||||
| @@ -89,54 +92,41 @@ _SWITCH_SCHEMA = ( | ||||
|  | ||||
|  | ||||
| def switch_schema( | ||||
|     class_: MockObjClass = cv.UNDEFINED, | ||||
|     class_: MockObjClass, | ||||
|     *, | ||||
|     entity_category: str = cv.UNDEFINED, | ||||
|     device_class: str = cv.UNDEFINED, | ||||
|     icon: str = cv.UNDEFINED, | ||||
|     block_inverted: bool = False, | ||||
|     default_restore_mode: str = "ALWAYS_OFF", | ||||
|     default_restore_mode: str = cv.UNDEFINED, | ||||
|     device_class: str = cv.UNDEFINED, | ||||
|     entity_category: str = cv.UNDEFINED, | ||||
|     icon: str = cv.UNDEFINED, | ||||
| ): | ||||
|     schema = _SWITCH_SCHEMA.extend( | ||||
|         { | ||||
|             cv.Optional(CONF_RESTORE_MODE, default=default_restore_mode): cv.enum( | ||||
|                 RESTORE_MODES, upper=True, space="_" | ||||
|             ), | ||||
|         } | ||||
|     ) | ||||
|     if class_ is not cv.UNDEFINED: | ||||
|         schema = schema.extend({cv.GenerateID(): cv.declare_id(class_)}) | ||||
|     if entity_category is not cv.UNDEFINED: | ||||
|         schema = schema.extend( | ||||
|             { | ||||
|                 cv.Optional( | ||||
|                     CONF_ENTITY_CATEGORY, default=entity_category | ||||
|                 ): cv.entity_category | ||||
|             } | ||||
|         ) | ||||
|     if device_class is not cv.UNDEFINED: | ||||
|         schema = schema.extend( | ||||
|             { | ||||
|                 cv.Optional( | ||||
|                     CONF_DEVICE_CLASS, default=device_class | ||||
|                 ): validate_device_class | ||||
|             } | ||||
|         ) | ||||
|     if icon is not cv.UNDEFINED: | ||||
|         schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon}) | ||||
|     schema = {cv.GenerateID(): cv.declare_id(class_)} | ||||
|  | ||||
|     for key, default, validator in [ | ||||
|         (CONF_DEVICE_CLASS, device_class, validate_device_class), | ||||
|         (CONF_ENTITY_CATEGORY, entity_category, cv.entity_category), | ||||
|         (CONF_ICON, icon, cv.icon), | ||||
|         ( | ||||
|             CONF_RESTORE_MODE, | ||||
|             default_restore_mode, | ||||
|             cv.enum(RESTORE_MODES, upper=True, space="_") | ||||
|             if default_restore_mode is not cv.UNDEFINED | ||||
|             else cv.UNDEFINED, | ||||
|         ), | ||||
|     ]: | ||||
|         if default is not cv.UNDEFINED: | ||||
|             schema[cv.Optional(key, default=default)] = validator | ||||
|  | ||||
|     if block_inverted: | ||||
|         schema = schema.extend( | ||||
|             { | ||||
|                 cv.Optional(CONF_INVERTED): cv.invalid( | ||||
|                     "Inverted is not supported for this platform!" | ||||
|                 ) | ||||
|             } | ||||
|         schema[cv.Optional(CONF_INVERTED)] = cv.invalid( | ||||
|             "Inverted is not supported for this platform!" | ||||
|         ) | ||||
|     return schema | ||||
|  | ||||
|     return _SWITCH_SCHEMA.extend(schema) | ||||
|  | ||||
|  | ||||
| # Remove before 2025.11.0 | ||||
| SWITCH_SCHEMA = switch_schema() | ||||
| SWITCH_SCHEMA = switch_schema(Switch) | ||||
| SWITCH_SCHEMA.add_extra(cv.deprecated_schema_constant("switch")) | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user