diff --git a/esphome/components/ble_client/switch/__init__.py b/esphome/components/ble_client/switch/__init__.py index e5b5ab281b..2304d65c01 100644 --- a/esphome/components/ble_client/switch/__init__.py +++ b/esphome/components/ble_client/switch/__init__.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import switch, ble_client -from esphome.const import CONF_ICON, CONF_ID, CONF_INVERTED, ICON_BLUETOOTH +from esphome.const import ICON_BLUETOOTH from .. import ble_client_ns BLEClientSwitch = ble_client_ns.class_( @@ -9,22 +9,13 @@ BLEClientSwitch = ble_client_ns.class_( ) CONFIG_SCHEMA = ( - switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(BLEClientSwitch), - cv.Optional(CONF_INVERTED): cv.invalid( - "BLE client switches do not support inverted mode!" - ), - cv.Optional(CONF_ICON, default=ICON_BLUETOOTH): switch.icon, - } - ) + switch.switch_schema(BLEClientSwitch, icon=ICON_BLUETOOTH, block_inverted=True) .extend(ble_client.BLE_CLIENT_SCHEMA) .extend(cv.COMPONENT_SCHEMA) ) async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) await ble_client.register_ble_node(var, config) diff --git a/esphome/components/copy/switch/__init__.py b/esphome/components/copy/switch/__init__.py index 6622412123..beffbe7fbb 100644 --- a/esphome/components/copy/switch/__init__.py +++ b/esphome/components/copy/switch/__init__.py @@ -5,7 +5,6 @@ from esphome.const import ( CONF_DEVICE_CLASS, CONF_ENTITY_CATEGORY, CONF_ICON, - CONF_ID, CONF_SOURCE_ID, ) from esphome.core.entity_helpers import inherit_property_from @@ -15,12 +14,15 @@ from .. import copy_ns CopySwitch = copy_ns.class_("CopySwitch", switch.Switch, cg.Component) -CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(CopySwitch), - cv.Required(CONF_SOURCE_ID): cv.use_id(switch.Switch), - } -).extend(cv.COMPONENT_SCHEMA) +CONFIG_SCHEMA = ( + switch.switch_schema(CopySwitch) + .extend( + { + cv.Required(CONF_SOURCE_ID): cv.use_id(switch.Switch), + } + ) + .extend(cv.COMPONENT_SCHEMA) +) FINAL_VALIDATE_SCHEMA = cv.All( inherit_property_from(CONF_ICON, CONF_SOURCE_ID), @@ -30,8 +32,7 @@ FINAL_VALIDATE_SCHEMA = cv.All( async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) - await switch.register_switch(var, config) + var = await switch.new_switch(config) await cg.register_component(var, config) source = await cg.get_variable(config[CONF_SOURCE_ID]) diff --git a/esphome/components/custom/switch/__init__.py b/esphome/components/custom/switch/__init__.py index e0b9d7751a..5538ae6aa0 100644 --- a/esphome/components/custom/switch/__init__.py +++ b/esphome/components/custom/switch/__init__.py @@ -10,13 +10,7 @@ CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(): cv.declare_id(CustomSwitchConstructor), cv.Required(CONF_LAMBDA): cv.returning_lambda, - cv.Required(CONF_SWITCHES): cv.ensure_list( - switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(switch.Switch), - } - ) - ), + cv.Required(CONF_SWITCHES): cv.ensure_list(switch.switch_schema(switch.Switch)), } ) diff --git a/esphome/components/demo/__init__.py b/esphome/components/demo/__init__.py index 7aa742919a..19b35828a5 100644 --- a/esphome/components/demo/__init__.py +++ b/esphome/components/demo/__init__.py @@ -349,13 +349,7 @@ CONFIG_SCHEMA = cv.Schema( CONF_ICON: ICON_BLUETOOTH, }, ], - ): [ - switch.SWITCH_SCHEMA.extend(cv.COMPONENT_SCHEMA).extend( - { - cv.GenerateID(): cv.declare_id(DemoSwitch), - } - ) - ], + ): [switch.switch_schema(DemoSwitch).extend(cv.COMPONENT_SCHEMA)], cv.Optional( CONF_TEXT_SENSORS, default=[ @@ -422,9 +416,8 @@ async def to_code(config): await cg.register_component(var, conf) for conf in config[CONF_SWITCHES]: - var = cg.new_Pvariable(conf[CONF_ID]) + var = await switch.new_switch(conf) await cg.register_component(var, conf) - await switch.register_switch(var, conf) for conf in config[CONF_TEXT_SENSORS]: var = await text_sensor.new_text_sensor(conf) diff --git a/esphome/components/gpio/switch/__init__.py b/esphome/components/gpio/switch/__init__.py index a03e16a2c1..f81cd5b6fb 100644 --- a/esphome/components/gpio/switch/__init__.py +++ b/esphome/components/gpio/switch/__init__.py @@ -2,7 +2,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import pins from esphome.components import switch -from esphome.const import CONF_ID, CONF_INTERLOCK, CONF_PIN, CONF_RESTORE_MODE +from esphome.const import CONF_INTERLOCK, CONF_PIN, CONF_RESTORE_MODE from .. import gpio_ns GPIOSwitch = gpio_ns.class_("GPIOSwitch", switch.Switch, cg.Component) @@ -18,25 +18,27 @@ RESTORE_MODES = { } CONF_INTERLOCK_WAIT_TIME = "interlock_wait_time" -CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(GPIOSwitch), - cv.Required(CONF_PIN): pins.gpio_output_pin_schema, - cv.Optional(CONF_RESTORE_MODE, default="RESTORE_DEFAULT_OFF"): cv.enum( - RESTORE_MODES, upper=True, space="_" - ), - cv.Optional(CONF_INTERLOCK): cv.ensure_list(cv.use_id(switch.Switch)), - cv.Optional( - CONF_INTERLOCK_WAIT_TIME, default="0ms" - ): cv.positive_time_period_milliseconds, - } -).extend(cv.COMPONENT_SCHEMA) +CONFIG_SCHEMA = ( + switch.switch_schema(GPIOSwitch) + .extend( + { + cv.Required(CONF_PIN): pins.gpio_output_pin_schema, + cv.Optional(CONF_RESTORE_MODE, default="RESTORE_DEFAULT_OFF"): cv.enum( + RESTORE_MODES, upper=True, space="_" + ), + cv.Optional(CONF_INTERLOCK): cv.ensure_list(cv.use_id(switch.Switch)), + cv.Optional( + CONF_INTERLOCK_WAIT_TIME, default="0ms" + ): cv.positive_time_period_milliseconds, + } + ) + .extend(cv.COMPONENT_SCHEMA) +) async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) pin = await cg.gpio_pin_expression(config[CONF_PIN]) cg.add(var.set_pin(pin)) diff --git a/esphome/components/modbus_controller/switch/__init__.py b/esphome/components/modbus_controller/switch/__init__.py index 0dfbd83cb8..9673a066e3 100644 --- a/esphome/components/modbus_controller/switch/__init__.py +++ b/esphome/components/modbus_controller/switch/__init__.py @@ -32,11 +32,11 @@ ModbusSwitch = modbus_controller_ns.class_( ) CONFIG_SCHEMA = cv.All( - switch.SWITCH_SCHEMA.extend(cv.COMPONENT_SCHEMA) + switch.switch_schema(ModbusSwitch) + .extend(cv.COMPONENT_SCHEMA) .extend(ModbusItemBaseSchema) .extend( { - cv.GenerateID(): cv.declare_id(ModbusSwitch), cv.Optional(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE), cv.Optional(CONF_USE_WRITE_MULTIPLE, default=False): cv.boolean, cv.Optional(CONF_WRITE_LAMBDA): cv.returning_lambda, diff --git a/esphome/components/nextion/switch/__init__.py b/esphome/components/nextion/switch/__init__.py index 068681fa14..91ab0cc81f 100644 --- a/esphome/components/nextion/switch/__init__.py +++ b/esphome/components/nextion/switch/__init__.py @@ -17,11 +17,7 @@ CODEOWNERS = ["@senexcrenshaw"] NextionSwitch = nextion_ns.class_("NextionSwitch", switch.Switch, cg.PollingComponent) CONFIG_SCHEMA = cv.All( - switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(NextionSwitch), - } - ) + switch.switch_schema(NextionSwitch) .extend(CONFIG_SWITCH_COMPONENT_SCHEMA) .extend(cv.polling_component_schema("never")), cv.has_exactly_one_key(CONF_COMPONENT_NAME, CONF_VARIABLE_NAME), diff --git a/esphome/components/output/switch/__init__.py b/esphome/components/output/switch/__init__.py index 46135d117e..3a23c1e33f 100644 --- a/esphome/components/output/switch/__init__.py +++ b/esphome/components/output/switch/__init__.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import output, switch -from esphome.const import CONF_ID, CONF_OUTPUT, CONF_RESTORE_MODE +from esphome.const import CONF_OUTPUT, CONF_RESTORE_MODE from .. import output_ns OutputSwitch = output_ns.class_("OutputSwitch", switch.Switch, cg.Component) @@ -16,21 +16,23 @@ RESTORE_MODES = { "RESTORE_INVERTED_DEFAULT_ON": OutputSwitchRestoreMode.OUTPUT_SWITCH_RESTORE_INVERTED_DEFAULT_ON, } -CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(OutputSwitch), - cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput), - cv.Optional(CONF_RESTORE_MODE, default="RESTORE_DEFAULT_OFF"): cv.enum( - RESTORE_MODES, upper=True, space="_" - ), - } -).extend(cv.COMPONENT_SCHEMA) +CONFIG_SCHEMA = ( + switch.switch_schema(OutputSwitch) + .extend( + { + cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput), + cv.Optional(CONF_RESTORE_MODE, default="RESTORE_DEFAULT_OFF"): cv.enum( + RESTORE_MODES, upper=True, space="_" + ), + } + ) + .extend(cv.COMPONENT_SCHEMA) +) async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) output_ = await cg.get_variable(config[CONF_OUTPUT]) cg.add(var.set_output(output_)) diff --git a/esphome/components/pipsolar/switch/__init__.py b/esphome/components/pipsolar/switch/__init__.py index 5ff33b10ff..7658c7d4f8 100644 --- a/esphome/components/pipsolar/switch/__init__.py +++ b/esphome/components/pipsolar/switch/__init__.py @@ -1,12 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import switch -from esphome.const import ( - CONF_ID, - CONF_INVERTED, - CONF_ICON, - ICON_POWER, -) +from esphome.const import ICON_POWER from .. import CONF_PIPSOLAR_ID, PIPSOLAR_COMPONENT_SCHEMA, pipsolar_ns DEPENDENCIES = ["uart"] @@ -29,14 +24,8 @@ TYPES = { PipsolarSwitch = pipsolar_ns.class_("PipsolarSwitch", switch.Switch, cg.Component) -PIPSWITCH_SCHEMA = switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(PipsolarSwitch), - cv.Optional(CONF_INVERTED): cv.invalid( - "Pipsolar switches do not support inverted mode!" - ), - cv.Optional(CONF_ICON, default=ICON_POWER): switch.icon, - } +PIPSWITCH_SCHEMA = switch.switch_schema( + PipsolarSwitch, icon=ICON_POWER, block_inverted=True ).extend(cv.COMPONENT_SCHEMA) CONFIG_SCHEMA = PIPSOLAR_COMPONENT_SCHEMA.extend( @@ -50,9 +39,8 @@ async def to_code(config): for type, (on, off) in TYPES.items(): if type in config: conf = config[type] - var = cg.new_Pvariable(conf[CONF_ID]) + var = await switch.new_switch(conf) await cg.register_component(var, conf) - await switch.register_switch(var, conf) cg.add(getattr(paren, f"set_{type}_switch")(var)) cg.add(var.set_parent(paren)) cg.add(var.set_on_command(on)) diff --git a/esphome/components/restart/switch/__init__.py b/esphome/components/restart/switch/__init__.py index de30392b45..89805b4246 100644 --- a/esphome/components/restart/switch/__init__.py +++ b/esphome/components/restart/switch/__init__.py @@ -2,10 +2,6 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import switch from esphome.const import ( - CONF_ENTITY_CATEGORY, - CONF_ID, - CONF_INVERTED, - CONF_ICON, ENTITY_CATEGORY_CONFIG, ICON_RESTART, ) @@ -13,21 +9,14 @@ from esphome.const import ( restart_ns = cg.esphome_ns.namespace("restart") RestartSwitch = restart_ns.class_("RestartSwitch", switch.Switch, cg.Component) -CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(RestartSwitch), - cv.Optional(CONF_INVERTED): cv.invalid( - "Restart switches do not support inverted mode!" - ), - cv.Optional(CONF_ICON, default=ICON_RESTART): switch.icon, - cv.Optional( - CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_CONFIG - ): cv.entity_category, - } +CONFIG_SCHEMA = switch.switch_schema( + RestartSwitch, + icon=ICON_RESTART, + entity_category=ENTITY_CATEGORY_CONFIG, + block_inverted=True, ).extend(cv.COMPONENT_SCHEMA) async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) diff --git a/esphome/components/safe_mode/switch/__init__.py b/esphome/components/safe_mode/switch/__init__.py index b6c3e852f6..a6fcdfbece 100644 --- a/esphome/components/safe_mode/switch/__init__.py +++ b/esphome/components/safe_mode/switch/__init__.py @@ -3,10 +3,6 @@ import esphome.config_validation as cv from esphome.components import switch from esphome.components.ota import OTAComponent from esphome.const import ( - CONF_ENTITY_CATEGORY, - CONF_ID, - CONF_INVERTED, - CONF_ICON, CONF_OTA, ENTITY_CATEGORY_CONFIG, ICON_RESTART_ALERT, @@ -17,25 +13,21 @@ DEPENDENCIES = ["ota"] SafeModeSwitch = safe_mode_ns.class_("SafeModeSwitch", switch.Switch, cg.Component) -CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(SafeModeSwitch), - cv.GenerateID(CONF_OTA): cv.use_id(OTAComponent), - cv.Optional(CONF_INVERTED): cv.invalid( - "Safe Mode Restart switches do not support inverted mode!" - ), - cv.Optional(CONF_ICON, default=ICON_RESTART_ALERT): switch.icon, - cv.Optional( - CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_CONFIG - ): cv.entity_category, - } -).extend(cv.COMPONENT_SCHEMA) +CONFIG_SCHEMA = ( + switch.switch_schema( + SafeModeSwitch, + icon=ICON_RESTART_ALERT, + entity_category=ENTITY_CATEGORY_CONFIG, + block_inverted=True, + ) + .extend({cv.GenerateID(CONF_OTA): cv.use_id(OTAComponent)}) + .extend(cv.COMPONENT_SCHEMA) +) async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) ota = await cg.get_variable(config[CONF_OTA]) cg.add(var.set_ota(ota)) diff --git a/esphome/components/shutdown/switch/__init__.py b/esphome/components/shutdown/switch/__init__.py index 49970b4c2f..5de9f2d189 100644 --- a/esphome/components/shutdown/switch/__init__.py +++ b/esphome/components/shutdown/switch/__init__.py @@ -2,10 +2,6 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import switch from esphome.const import ( - CONF_ENTITY_CATEGORY, - CONF_ID, - CONF_INVERTED, - CONF_ICON, ENTITY_CATEGORY_CONFIG, ICON_POWER, ) @@ -13,21 +9,14 @@ from esphome.const import ( shutdown_ns = cg.esphome_ns.namespace("shutdown") ShutdownSwitch = shutdown_ns.class_("ShutdownSwitch", switch.Switch, cg.Component) -CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(ShutdownSwitch), - cv.Optional(CONF_INVERTED): cv.invalid( - "Shutdown switches do not support inverted mode!" - ), - cv.Optional(CONF_ICON, default=ICON_POWER): switch.icon, - cv.Optional( - CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_CONFIG - ): cv.entity_category, - } +CONFIG_SCHEMA = switch.switch_schema( + ShutdownSwitch, + icon=ICON_POWER, + entity_category=ENTITY_CATEGORY_CONFIG, + block_inverted=True, ).extend(cv.COMPONENT_SCHEMA) async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) diff --git a/esphome/components/sprinkler/__init__.py b/esphome/components/sprinkler/__init__.py index 659eb5b58e..4e80cfa021 100644 --- a/esphome/components/sprinkler/__init__.py +++ b/esphome/components/sprinkler/__init__.py @@ -223,13 +223,7 @@ SPRINKLER_ACTION_QUEUE_VALVE_SCHEMA = cv.Schema( SPRINKLER_VALVE_SCHEMA = cv.Schema( { cv.Optional(CONF_ENABLE_SWITCH): cv.maybe_simple_value( - switch.SWITCH_SCHEMA.extend( - cv.Schema( - { - cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch), - } - ) - ), + switch.switch_schema(SprinklerControllerSwitch), key=CONF_NAME, ), cv.Optional(CONF_PUMP_OFF_SWITCH_ID): cv.use_id(switch.Switch), @@ -237,13 +231,7 @@ SPRINKLER_VALVE_SCHEMA = cv.Schema( cv.Optional(CONF_PUMP_SWITCH_ID): cv.use_id(switch.Switch), cv.Required(CONF_RUN_DURATION): cv.positive_time_period_seconds, cv.Required(CONF_VALVE_SWITCH): cv.maybe_simple_value( - switch.SWITCH_SCHEMA.extend( - cv.Schema( - { - cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch), - } - ) - ), + switch.switch_schema(SprinklerControllerSwitch), key=CONF_NAME, ), cv.Optional(CONF_VALVE_OFF_SWITCH_ID): cv.use_id(switch.Switch), @@ -256,43 +244,19 @@ SPRINKLER_CONTROLLER_SCHEMA = cv.Schema( { cv.GenerateID(): cv.declare_id(Sprinkler), cv.Optional(CONF_AUTO_ADVANCE_SWITCH): cv.maybe_simple_value( - switch.SWITCH_SCHEMA.extend( - cv.Schema( - { - cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch), - } - ) - ), + switch.switch_schema(SprinklerControllerSwitch), key=CONF_NAME, ), cv.Optional(CONF_MAIN_SWITCH): cv.maybe_simple_value( - switch.SWITCH_SCHEMA.extend( - cv.Schema( - { - cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch), - } - ) - ), + switch.switch_schema(SprinklerControllerSwitch), key=CONF_NAME, ), cv.Optional(CONF_QUEUE_ENABLE_SWITCH): cv.maybe_simple_value( - switch.SWITCH_SCHEMA.extend( - cv.Schema( - { - cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch), - } - ) - ), + switch.switch_schema(SprinklerControllerSwitch), key=CONF_NAME, ), cv.Optional(CONF_REVERSE_SWITCH): cv.maybe_simple_value( - switch.SWITCH_SCHEMA.extend( - cv.Schema( - { - cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch), - } - ) - ), + switch.switch_schema(SprinklerControllerSwitch), key=CONF_NAME, ), cv.Optional(CONF_MANUAL_SELECTION_DELAY): cv.positive_time_period_seconds, diff --git a/esphome/components/switch/__init__.py b/esphome/components/switch/__init__.py index 54ad2b852e..336c7d38d6 100644 --- a/esphome/components/switch/__init__.py +++ b/esphome/components/switch/__init__.py @@ -6,6 +6,7 @@ from esphome.components import mqtt from esphome.const import ( CONF_DEVICE_CLASS, CONF_ENTITY_CATEGORY, + CONF_ICON, CONF_ID, CONF_INVERTED, CONF_MQTT_ID, @@ -45,7 +46,6 @@ SwitchTurnOffTrigger = switch_ns.class_( "SwitchTurnOffTrigger", automation.Trigger.template() ) -icon = cv.icon validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True) @@ -76,6 +76,8 @@ def switch_schema( *, entity_category: str = _UNDEF, device_class: str = _UNDEF, + icon: str = _UNDEF, + block_inverted: bool = False, ): schema = SWITCH_SCHEMA if class_ is not _UNDEF: @@ -96,6 +98,16 @@ def switch_schema( ): validate_device_class } ) + if icon is not _UNDEF: + schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon}) + if block_inverted: + schema = schema.extend( + { + cv.Optional(CONF_INVERTED): cv.invalid( + "Inverted is not supported for this platform!" + ) + } + ) return schema diff --git a/esphome/components/template/switch/__init__.py b/esphome/components/template/switch/__init__.py index 6095a7c561..e002c4e3d8 100644 --- a/esphome/components/template/switch/__init__.py +++ b/esphome/components/template/switch/__init__.py @@ -31,9 +31,9 @@ def validate(config): CONFIG_SCHEMA = cv.All( - switch.SWITCH_SCHEMA.extend( + switch.switch_schema(TemplateSwitch) + .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, @@ -45,15 +45,15 @@ CONFIG_SCHEMA = cv.All( ), cv.Optional(CONF_RESTORE_STATE, default=False): cv.boolean, } - ).extend(cv.COMPONENT_SCHEMA), + ) + .extend(cv.COMPONENT_SCHEMA), validate, ) async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) if CONF_LAMBDA in config: template_ = await cg.process_lambda( diff --git a/esphome/components/tuya/switch/__init__.py b/esphome/components/tuya/switch/__init__.py index 4df6bba713..b4ec53ec59 100644 --- a/esphome/components/tuya/switch/__init__.py +++ b/esphome/components/tuya/switch/__init__.py @@ -1,7 +1,7 @@ from esphome.components import switch import esphome.config_validation as cv import esphome.codegen as cg -from esphome.const import CONF_ID, CONF_SWITCH_DATAPOINT +from esphome.const import CONF_SWITCH_DATAPOINT from .. import tuya_ns, CONF_TUYA_ID, Tuya DEPENDENCIES = ["tuya"] @@ -9,19 +9,21 @@ CODEOWNERS = ["@jesserockz"] TuyaSwitch = tuya_ns.class_("TuyaSwitch", switch.Switch, cg.Component) -CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(TuyaSwitch), - cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya), - cv.Required(CONF_SWITCH_DATAPOINT): cv.uint8_t, - } -).extend(cv.COMPONENT_SCHEMA) +CONFIG_SCHEMA = ( + switch.switch_schema(TuyaSwitch) + .extend( + { + cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya), + cv.Required(CONF_SWITCH_DATAPOINT): cv.uint8_t, + } + ) + .extend(cv.COMPONENT_SCHEMA) +) async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) paren = await cg.get_variable(config[CONF_TUYA_ID]) cg.add(var.set_tuya_parent(paren)) diff --git a/esphome/components/uart/switch/__init__.py b/esphome/components/uart/switch/__init__.py index 9e7f95bd2a..60f5ddaf0d 100644 --- a/esphome/components/uart/switch/__init__.py +++ b/esphome/components/uart/switch/__init__.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import switch, uart -from esphome.const import CONF_DATA, CONF_ID, CONF_INVERTED, CONF_SEND_EVERY +from esphome.const import CONF_DATA, CONF_SEND_EVERY from esphome.core import HexInt from .. import uart_ns, validate_raw_data @@ -11,13 +11,10 @@ UARTSwitch = uart_ns.class_("UARTSwitch", switch.Switch, uart.UARTDevice, cg.Com CONFIG_SCHEMA = ( - switch.SWITCH_SCHEMA.extend( + switch.switch_schema(UARTSwitch, block_inverted=True) + .extend( { - cv.GenerateID(): cv.declare_id(UARTSwitch), cv.Required(CONF_DATA): validate_raw_data, - cv.Optional(CONF_INVERTED): cv.invalid( - "UART switches do not support inverted mode!" - ), cv.Optional(CONF_SEND_EVERY): cv.positive_time_period_milliseconds, } ) @@ -27,9 +24,8 @@ CONFIG_SCHEMA = ( async def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) await uart.register_uart_device(var, config) data = config[CONF_DATA]