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.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)

View file

@ -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])

View file

@ -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)),
}
)

View file

@ -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)

View file

@ -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))

View file

@ -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,

View file

@ -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),

View file

@ -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_))

View file

@ -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))

View file

@ -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)

View file

@ -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))

View file

@ -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)

View file

@ -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,

View file

@ -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

View file

@ -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(

View file

@ -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))

View file

@ -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]