Tidy up switch schemas (#3754)

This commit is contained in:
Jesse Hills 2022-08-31 13:43:46 +12:00 committed by GitHub
parent bd6bc283b6
commit 4d66fab360
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 118 additions and 207 deletions

View File

@ -1,7 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import switch, ble_client 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 from .. import ble_client_ns
BLEClientSwitch = ble_client_ns.class_( BLEClientSwitch = ble_client_ns.class_(
@ -9,22 +9,13 @@ BLEClientSwitch = ble_client_ns.class_(
) )
CONFIG_SCHEMA = ( CONFIG_SCHEMA = (
switch.SWITCH_SCHEMA.extend( switch.switch_schema(BLEClientSwitch, icon=ICON_BLUETOOTH, block_inverted=True)
{
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,
}
)
.extend(ble_client.BLE_CLIENT_SCHEMA) .extend(ble_client.BLE_CLIENT_SCHEMA)
.extend(cv.COMPONENT_SCHEMA) .extend(cv.COMPONENT_SCHEMA)
) )
async def to_code(config): 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 cg.register_component(var, config)
await switch.register_switch(var, config)
await ble_client.register_ble_node(var, config) await ble_client.register_ble_node(var, config)

View File

@ -5,7 +5,6 @@ from esphome.const import (
CONF_DEVICE_CLASS, CONF_DEVICE_CLASS,
CONF_ENTITY_CATEGORY, CONF_ENTITY_CATEGORY,
CONF_ICON, CONF_ICON,
CONF_ID,
CONF_SOURCE_ID, CONF_SOURCE_ID,
) )
from esphome.core.entity_helpers import inherit_property_from 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) CopySwitch = copy_ns.class_("CopySwitch", switch.Switch, cg.Component)
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( CONFIG_SCHEMA = (
{ switch.switch_schema(CopySwitch)
cv.GenerateID(): cv.declare_id(CopySwitch), .extend(
cv.Required(CONF_SOURCE_ID): cv.use_id(switch.Switch), {
} cv.Required(CONF_SOURCE_ID): cv.use_id(switch.Switch),
).extend(cv.COMPONENT_SCHEMA) }
)
.extend(cv.COMPONENT_SCHEMA)
)
FINAL_VALIDATE_SCHEMA = cv.All( FINAL_VALIDATE_SCHEMA = cv.All(
inherit_property_from(CONF_ICON, CONF_SOURCE_ID), inherit_property_from(CONF_ICON, CONF_SOURCE_ID),
@ -30,8 +32,7 @@ FINAL_VALIDATE_SCHEMA = cv.All(
async def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = await switch.new_switch(config)
await switch.register_switch(var, config)
await cg.register_component(var, config) await cg.register_component(var, config)
source = await cg.get_variable(config[CONF_SOURCE_ID]) source = await cg.get_variable(config[CONF_SOURCE_ID])

View File

@ -10,13 +10,7 @@ CONFIG_SCHEMA = cv.Schema(
{ {
cv.GenerateID(): cv.declare_id(CustomSwitchConstructor), cv.GenerateID(): cv.declare_id(CustomSwitchConstructor),
cv.Required(CONF_LAMBDA): cv.returning_lambda, cv.Required(CONF_LAMBDA): cv.returning_lambda,
cv.Required(CONF_SWITCHES): cv.ensure_list( cv.Required(CONF_SWITCHES): cv.ensure_list(switch.switch_schema(switch.Switch)),
switch.SWITCH_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(switch.Switch),
}
)
),
} }
) )

View File

@ -349,13 +349,7 @@ CONFIG_SCHEMA = cv.Schema(
CONF_ICON: ICON_BLUETOOTH, CONF_ICON: ICON_BLUETOOTH,
}, },
], ],
): [ ): [switch.switch_schema(DemoSwitch).extend(cv.COMPONENT_SCHEMA)],
switch.SWITCH_SCHEMA.extend(cv.COMPONENT_SCHEMA).extend(
{
cv.GenerateID(): cv.declare_id(DemoSwitch),
}
)
],
cv.Optional( cv.Optional(
CONF_TEXT_SENSORS, CONF_TEXT_SENSORS,
default=[ default=[
@ -422,9 +416,8 @@ async def to_code(config):
await cg.register_component(var, conf) await cg.register_component(var, conf)
for conf in config[CONF_SWITCHES]: 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 cg.register_component(var, conf)
await switch.register_switch(var, conf)
for conf in config[CONF_TEXT_SENSORS]: for conf in config[CONF_TEXT_SENSORS]:
var = await text_sensor.new_text_sensor(conf) var = await text_sensor.new_text_sensor(conf)

View File

@ -2,7 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import pins from esphome import pins
from esphome.components import switch 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 from .. import gpio_ns
GPIOSwitch = gpio_ns.class_("GPIOSwitch", switch.Switch, cg.Component) GPIOSwitch = gpio_ns.class_("GPIOSwitch", switch.Switch, cg.Component)
@ -18,25 +18,27 @@ RESTORE_MODES = {
} }
CONF_INTERLOCK_WAIT_TIME = "interlock_wait_time" CONF_INTERLOCK_WAIT_TIME = "interlock_wait_time"
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( CONFIG_SCHEMA = (
{ switch.switch_schema(GPIOSwitch)
cv.GenerateID(): cv.declare_id(GPIOSwitch), .extend(
cv.Required(CONF_PIN): pins.gpio_output_pin_schema, {
cv.Optional(CONF_RESTORE_MODE, default="RESTORE_DEFAULT_OFF"): cv.enum( cv.Required(CONF_PIN): pins.gpio_output_pin_schema,
RESTORE_MODES, upper=True, space="_" 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( cv.Optional(CONF_INTERLOCK): cv.ensure_list(cv.use_id(switch.Switch)),
CONF_INTERLOCK_WAIT_TIME, default="0ms" cv.Optional(
): cv.positive_time_period_milliseconds, CONF_INTERLOCK_WAIT_TIME, default="0ms"
} ): cv.positive_time_period_milliseconds,
).extend(cv.COMPONENT_SCHEMA) }
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config): 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 cg.register_component(var, config)
await switch.register_switch(var, config)
pin = await cg.gpio_pin_expression(config[CONF_PIN]) pin = await cg.gpio_pin_expression(config[CONF_PIN])
cg.add(var.set_pin(pin)) cg.add(var.set_pin(pin))

View File

@ -32,11 +32,11 @@ ModbusSwitch = modbus_controller_ns.class_(
) )
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
switch.SWITCH_SCHEMA.extend(cv.COMPONENT_SCHEMA) switch.switch_schema(ModbusSwitch)
.extend(cv.COMPONENT_SCHEMA)
.extend(ModbusItemBaseSchema) .extend(ModbusItemBaseSchema)
.extend( .extend(
{ {
cv.GenerateID(): cv.declare_id(ModbusSwitch),
cv.Optional(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE), cv.Optional(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE),
cv.Optional(CONF_USE_WRITE_MULTIPLE, default=False): cv.boolean, cv.Optional(CONF_USE_WRITE_MULTIPLE, default=False): cv.boolean,
cv.Optional(CONF_WRITE_LAMBDA): cv.returning_lambda, cv.Optional(CONF_WRITE_LAMBDA): cv.returning_lambda,

View File

@ -17,11 +17,7 @@ CODEOWNERS = ["@senexcrenshaw"]
NextionSwitch = nextion_ns.class_("NextionSwitch", switch.Switch, cg.PollingComponent) NextionSwitch = nextion_ns.class_("NextionSwitch", switch.Switch, cg.PollingComponent)
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
switch.SWITCH_SCHEMA.extend( switch.switch_schema(NextionSwitch)
{
cv.GenerateID(): cv.declare_id(NextionSwitch),
}
)
.extend(CONFIG_SWITCH_COMPONENT_SCHEMA) .extend(CONFIG_SWITCH_COMPONENT_SCHEMA)
.extend(cv.polling_component_schema("never")), .extend(cv.polling_component_schema("never")),
cv.has_exactly_one_key(CONF_COMPONENT_NAME, CONF_VARIABLE_NAME), cv.has_exactly_one_key(CONF_COMPONENT_NAME, CONF_VARIABLE_NAME),

View File

@ -1,7 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import output, switch 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 from .. import output_ns
OutputSwitch = output_ns.class_("OutputSwitch", switch.Switch, cg.Component) 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, "RESTORE_INVERTED_DEFAULT_ON": OutputSwitchRestoreMode.OUTPUT_SWITCH_RESTORE_INVERTED_DEFAULT_ON,
} }
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( CONFIG_SCHEMA = (
{ switch.switch_schema(OutputSwitch)
cv.GenerateID(): cv.declare_id(OutputSwitch), .extend(
cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput), {
cv.Optional(CONF_RESTORE_MODE, default="RESTORE_DEFAULT_OFF"): cv.enum( cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput),
RESTORE_MODES, upper=True, space="_" cv.Optional(CONF_RESTORE_MODE, default="RESTORE_DEFAULT_OFF"): cv.enum(
), RESTORE_MODES, upper=True, space="_"
} ),
).extend(cv.COMPONENT_SCHEMA) }
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config): 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 cg.register_component(var, config)
await switch.register_switch(var, config)
output_ = await cg.get_variable(config[CONF_OUTPUT]) output_ = await cg.get_variable(config[CONF_OUTPUT])
cg.add(var.set_output(output_)) cg.add(var.set_output(output_))

View File

@ -1,12 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import switch from esphome.components import switch
from esphome.const import ( from esphome.const import ICON_POWER
CONF_ID,
CONF_INVERTED,
CONF_ICON,
ICON_POWER,
)
from .. import CONF_PIPSOLAR_ID, PIPSOLAR_COMPONENT_SCHEMA, pipsolar_ns from .. import CONF_PIPSOLAR_ID, PIPSOLAR_COMPONENT_SCHEMA, pipsolar_ns
DEPENDENCIES = ["uart"] DEPENDENCIES = ["uart"]
@ -29,14 +24,8 @@ TYPES = {
PipsolarSwitch = pipsolar_ns.class_("PipsolarSwitch", switch.Switch, cg.Component) PipsolarSwitch = pipsolar_ns.class_("PipsolarSwitch", switch.Switch, cg.Component)
PIPSWITCH_SCHEMA = switch.SWITCH_SCHEMA.extend( PIPSWITCH_SCHEMA = switch.switch_schema(
{ PipsolarSwitch, icon=ICON_POWER, block_inverted=True
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,
}
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
CONFIG_SCHEMA = PIPSOLAR_COMPONENT_SCHEMA.extend( CONFIG_SCHEMA = PIPSOLAR_COMPONENT_SCHEMA.extend(
@ -50,9 +39,8 @@ async def to_code(config):
for type, (on, off) in TYPES.items(): for type, (on, off) in TYPES.items():
if type in config: if type in config:
conf = config[type] conf = config[type]
var = cg.new_Pvariable(conf[CONF_ID]) var = await switch.new_switch(conf)
await cg.register_component(var, conf) await cg.register_component(var, conf)
await switch.register_switch(var, conf)
cg.add(getattr(paren, f"set_{type}_switch")(var)) cg.add(getattr(paren, f"set_{type}_switch")(var))
cg.add(var.set_parent(paren)) cg.add(var.set_parent(paren))
cg.add(var.set_on_command(on)) cg.add(var.set_on_command(on))

View File

@ -2,10 +2,6 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import switch from esphome.components import switch
from esphome.const import ( from esphome.const import (
CONF_ENTITY_CATEGORY,
CONF_ID,
CONF_INVERTED,
CONF_ICON,
ENTITY_CATEGORY_CONFIG, ENTITY_CATEGORY_CONFIG,
ICON_RESTART, ICON_RESTART,
) )
@ -13,21 +9,14 @@ from esphome.const import (
restart_ns = cg.esphome_ns.namespace("restart") restart_ns = cg.esphome_ns.namespace("restart")
RestartSwitch = restart_ns.class_("RestartSwitch", switch.Switch, cg.Component) RestartSwitch = restart_ns.class_("RestartSwitch", switch.Switch, cg.Component)
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( CONFIG_SCHEMA = switch.switch_schema(
{ RestartSwitch,
cv.GenerateID(): cv.declare_id(RestartSwitch), icon=ICON_RESTART,
cv.Optional(CONF_INVERTED): cv.invalid( entity_category=ENTITY_CATEGORY_CONFIG,
"Restart switches do not support inverted mode!" block_inverted=True,
),
cv.Optional(CONF_ICON, default=ICON_RESTART): switch.icon,
cv.Optional(
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_CONFIG
): cv.entity_category,
}
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
async def to_code(config): 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 cg.register_component(var, config)
await switch.register_switch(var, config)

View File

@ -3,10 +3,6 @@ import esphome.config_validation as cv
from esphome.components import switch from esphome.components import switch
from esphome.components.ota import OTAComponent from esphome.components.ota import OTAComponent
from esphome.const import ( from esphome.const import (
CONF_ENTITY_CATEGORY,
CONF_ID,
CONF_INVERTED,
CONF_ICON,
CONF_OTA, CONF_OTA,
ENTITY_CATEGORY_CONFIG, ENTITY_CATEGORY_CONFIG,
ICON_RESTART_ALERT, ICON_RESTART_ALERT,
@ -17,25 +13,21 @@ DEPENDENCIES = ["ota"]
SafeModeSwitch = safe_mode_ns.class_("SafeModeSwitch", switch.Switch, cg.Component) SafeModeSwitch = safe_mode_ns.class_("SafeModeSwitch", switch.Switch, cg.Component)
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( CONFIG_SCHEMA = (
{ switch.switch_schema(
cv.GenerateID(): cv.declare_id(SafeModeSwitch), SafeModeSwitch,
cv.GenerateID(CONF_OTA): cv.use_id(OTAComponent), icon=ICON_RESTART_ALERT,
cv.Optional(CONF_INVERTED): cv.invalid( entity_category=ENTITY_CATEGORY_CONFIG,
"Safe Mode Restart switches do not support inverted mode!" block_inverted=True,
), )
cv.Optional(CONF_ICON, default=ICON_RESTART_ALERT): switch.icon, .extend({cv.GenerateID(CONF_OTA): cv.use_id(OTAComponent)})
cv.Optional( .extend(cv.COMPONENT_SCHEMA)
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_CONFIG )
): cv.entity_category,
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config): 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 cg.register_component(var, config)
await switch.register_switch(var, config)
ota = await cg.get_variable(config[CONF_OTA]) ota = await cg.get_variable(config[CONF_OTA])
cg.add(var.set_ota(ota)) cg.add(var.set_ota(ota))

View File

@ -2,10 +2,6 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import switch from esphome.components import switch
from esphome.const import ( from esphome.const import (
CONF_ENTITY_CATEGORY,
CONF_ID,
CONF_INVERTED,
CONF_ICON,
ENTITY_CATEGORY_CONFIG, ENTITY_CATEGORY_CONFIG,
ICON_POWER, ICON_POWER,
) )
@ -13,21 +9,14 @@ from esphome.const import (
shutdown_ns = cg.esphome_ns.namespace("shutdown") shutdown_ns = cg.esphome_ns.namespace("shutdown")
ShutdownSwitch = shutdown_ns.class_("ShutdownSwitch", switch.Switch, cg.Component) ShutdownSwitch = shutdown_ns.class_("ShutdownSwitch", switch.Switch, cg.Component)
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( CONFIG_SCHEMA = switch.switch_schema(
{ ShutdownSwitch,
cv.GenerateID(): cv.declare_id(ShutdownSwitch), icon=ICON_POWER,
cv.Optional(CONF_INVERTED): cv.invalid( entity_category=ENTITY_CATEGORY_CONFIG,
"Shutdown switches do not support inverted mode!" block_inverted=True,
),
cv.Optional(CONF_ICON, default=ICON_POWER): switch.icon,
cv.Optional(
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_CONFIG
): cv.entity_category,
}
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
async def to_code(config): 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 cg.register_component(var, config)
await switch.register_switch(var, config)

View File

@ -223,13 +223,7 @@ SPRINKLER_ACTION_QUEUE_VALVE_SCHEMA = cv.Schema(
SPRINKLER_VALVE_SCHEMA = cv.Schema( SPRINKLER_VALVE_SCHEMA = cv.Schema(
{ {
cv.Optional(CONF_ENABLE_SWITCH): cv.maybe_simple_value( cv.Optional(CONF_ENABLE_SWITCH): cv.maybe_simple_value(
switch.SWITCH_SCHEMA.extend( switch.switch_schema(SprinklerControllerSwitch),
cv.Schema(
{
cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch),
}
)
),
key=CONF_NAME, key=CONF_NAME,
), ),
cv.Optional(CONF_PUMP_OFF_SWITCH_ID): cv.use_id(switch.Switch), 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.Optional(CONF_PUMP_SWITCH_ID): cv.use_id(switch.Switch),
cv.Required(CONF_RUN_DURATION): cv.positive_time_period_seconds, cv.Required(CONF_RUN_DURATION): cv.positive_time_period_seconds,
cv.Required(CONF_VALVE_SWITCH): cv.maybe_simple_value( cv.Required(CONF_VALVE_SWITCH): cv.maybe_simple_value(
switch.SWITCH_SCHEMA.extend( switch.switch_schema(SprinklerControllerSwitch),
cv.Schema(
{
cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch),
}
)
),
key=CONF_NAME, key=CONF_NAME,
), ),
cv.Optional(CONF_VALVE_OFF_SWITCH_ID): cv.use_id(switch.Switch), 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.GenerateID(): cv.declare_id(Sprinkler),
cv.Optional(CONF_AUTO_ADVANCE_SWITCH): cv.maybe_simple_value( cv.Optional(CONF_AUTO_ADVANCE_SWITCH): cv.maybe_simple_value(
switch.SWITCH_SCHEMA.extend( switch.switch_schema(SprinklerControllerSwitch),
cv.Schema(
{
cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch),
}
)
),
key=CONF_NAME, key=CONF_NAME,
), ),
cv.Optional(CONF_MAIN_SWITCH): cv.maybe_simple_value( cv.Optional(CONF_MAIN_SWITCH): cv.maybe_simple_value(
switch.SWITCH_SCHEMA.extend( switch.switch_schema(SprinklerControllerSwitch),
cv.Schema(
{
cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch),
}
)
),
key=CONF_NAME, key=CONF_NAME,
), ),
cv.Optional(CONF_QUEUE_ENABLE_SWITCH): cv.maybe_simple_value( cv.Optional(CONF_QUEUE_ENABLE_SWITCH): cv.maybe_simple_value(
switch.SWITCH_SCHEMA.extend( switch.switch_schema(SprinklerControllerSwitch),
cv.Schema(
{
cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch),
}
)
),
key=CONF_NAME, key=CONF_NAME,
), ),
cv.Optional(CONF_REVERSE_SWITCH): cv.maybe_simple_value( cv.Optional(CONF_REVERSE_SWITCH): cv.maybe_simple_value(
switch.SWITCH_SCHEMA.extend( switch.switch_schema(SprinklerControllerSwitch),
cv.Schema(
{
cv.GenerateID(): cv.declare_id(SprinklerControllerSwitch),
}
)
),
key=CONF_NAME, key=CONF_NAME,
), ),
cv.Optional(CONF_MANUAL_SELECTION_DELAY): cv.positive_time_period_seconds, cv.Optional(CONF_MANUAL_SELECTION_DELAY): cv.positive_time_period_seconds,

View File

@ -6,6 +6,7 @@ from esphome.components import mqtt
from esphome.const import ( from esphome.const import (
CONF_DEVICE_CLASS, CONF_DEVICE_CLASS,
CONF_ENTITY_CATEGORY, CONF_ENTITY_CATEGORY,
CONF_ICON,
CONF_ID, CONF_ID,
CONF_INVERTED, CONF_INVERTED,
CONF_MQTT_ID, CONF_MQTT_ID,
@ -45,7 +46,6 @@ SwitchTurnOffTrigger = switch_ns.class_(
"SwitchTurnOffTrigger", automation.Trigger.template() "SwitchTurnOffTrigger", automation.Trigger.template()
) )
icon = cv.icon
validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True) validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True)
@ -76,6 +76,8 @@ def switch_schema(
*, *,
entity_category: str = _UNDEF, entity_category: str = _UNDEF,
device_class: str = _UNDEF, device_class: str = _UNDEF,
icon: str = _UNDEF,
block_inverted: bool = False,
): ):
schema = SWITCH_SCHEMA schema = SWITCH_SCHEMA
if class_ is not _UNDEF: if class_ is not _UNDEF:
@ -96,6 +98,16 @@ def switch_schema(
): validate_device_class ): 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 return schema

View File

@ -31,9 +31,9 @@ def validate(config):
CONFIG_SCHEMA = cv.All( 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_LAMBDA): cv.returning_lambda,
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean, cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
cv.Optional(CONF_ASSUMED_STATE, 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, cv.Optional(CONF_RESTORE_STATE, default=False): cv.boolean,
} }
).extend(cv.COMPONENT_SCHEMA), )
.extend(cv.COMPONENT_SCHEMA),
validate, validate,
) )
async def to_code(config): 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 cg.register_component(var, config)
await switch.register_switch(var, config)
if CONF_LAMBDA in config: if CONF_LAMBDA in config:
template_ = await cg.process_lambda( template_ = await cg.process_lambda(

View File

@ -1,7 +1,7 @@
from esphome.components import switch from esphome.components import switch
import esphome.config_validation as cv import esphome.config_validation as cv
import esphome.codegen as cg 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 from .. import tuya_ns, CONF_TUYA_ID, Tuya
DEPENDENCIES = ["tuya"] DEPENDENCIES = ["tuya"]
@ -9,19 +9,21 @@ CODEOWNERS = ["@jesserockz"]
TuyaSwitch = tuya_ns.class_("TuyaSwitch", switch.Switch, cg.Component) TuyaSwitch = tuya_ns.class_("TuyaSwitch", switch.Switch, cg.Component)
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( CONFIG_SCHEMA = (
{ switch.switch_schema(TuyaSwitch)
cv.GenerateID(): cv.declare_id(TuyaSwitch), .extend(
cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya), {
cv.Required(CONF_SWITCH_DATAPOINT): cv.uint8_t, cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya),
} cv.Required(CONF_SWITCH_DATAPOINT): cv.uint8_t,
).extend(cv.COMPONENT_SCHEMA) }
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config): 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 cg.register_component(var, config)
await switch.register_switch(var, config)
paren = await cg.get_variable(config[CONF_TUYA_ID]) paren = await cg.get_variable(config[CONF_TUYA_ID])
cg.add(var.set_tuya_parent(paren)) cg.add(var.set_tuya_parent(paren))

View File

@ -1,7 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import switch, uart 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 esphome.core import HexInt
from .. import uart_ns, validate_raw_data from .. import uart_ns, validate_raw_data
@ -11,13 +11,10 @@ UARTSwitch = uart_ns.class_("UARTSwitch", switch.Switch, uart.UARTDevice, cg.Com
CONFIG_SCHEMA = ( 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.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, cv.Optional(CONF_SEND_EVERY): cv.positive_time_period_milliseconds,
} }
) )
@ -27,9 +24,8 @@ CONFIG_SCHEMA = (
async def to_code(config): 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 cg.register_component(var, config)
await switch.register_switch(var, config)
await uart.register_uart_device(var, config) await uart.register_uart_device(var, config)
data = config[CONF_DATA] data = config[CONF_DATA]