From 50fbbf2d3ba32f696277529f1ea5413da330ede3 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 20 Feb 2023 16:22:37 +1300 Subject: [PATCH] Move remaining SENSOR_SCHEMA to use sensor_schema() (#4471) --- .../components/adc128s102/sensor/__init__.py | 17 +++++----- esphome/components/custom/sensor/__init__.py | 2 +- esphome/components/ezo/sensor.py | 4 +-- esphome/components/integration/sensor.py | 31 ++++++++++--------- .../components/kalman_combinator/sensor.py | 29 +++++++++-------- esphome/components/mcp3008/sensor.py | 19 +++++++----- esphome/components/mcp3204/sensor/__init__.py | 17 +++++----- .../modbus_controller/sensor/__init__.py | 4 +-- esphome/components/tuya/sensor/__init__.py | 17 +++++----- 9 files changed, 79 insertions(+), 61 deletions(-) diff --git a/esphome/components/adc128s102/sensor/__init__.py b/esphome/components/adc128s102/sensor/__init__.py index 3ab6fc4c38..640a1b628e 100644 --- a/esphome/components/adc128s102/sensor/__init__.py +++ b/esphome/components/adc128s102/sensor/__init__.py @@ -16,13 +16,16 @@ ADC128S102Sensor = adc128s102_ns.class_( ) CONF_ADC128S102_ID = "adc128s102_id" -CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(ADC128S102Sensor), - cv.GenerateID(CONF_ADC128S102_ID): cv.use_id(ADC128S102), - cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=7), - } -).extend(cv.polling_component_schema("60s")) +CONFIG_SCHEMA = ( + sensor.sensor_schema(ADC128S102Sensor) + .extend( + { + cv.GenerateID(CONF_ADC128S102_ID): cv.use_id(ADC128S102), + cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=7), + } + ) + .extend(cv.polling_component_schema("60s")) +) async def to_code(config): diff --git a/esphome/components/custom/sensor/__init__.py b/esphome/components/custom/sensor/__init__.py index bf9421e43e..be17d9a334 100644 --- a/esphome/components/custom/sensor/__init__.py +++ b/esphome/components/custom/sensor/__init__.py @@ -10,7 +10,7 @@ CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(): cv.declare_id(CustomSensorConstructor), cv.Required(CONF_LAMBDA): cv.returning_lambda, - cv.Required(CONF_SENSORS): cv.ensure_list(sensor.SENSOR_SCHEMA), + cv.Required(CONF_SENSORS): cv.ensure_list(sensor.sensor_schema()), } ) diff --git a/esphome/components/ezo/sensor.py b/esphome/components/ezo/sensor.py index 049b1bf4d0..486ba0126e 100644 --- a/esphome/components/ezo/sensor.py +++ b/esphome/components/ezo/sensor.py @@ -41,9 +41,9 @@ DeviceInformationTrigger = ezo_ns.class_( LedTrigger = ezo_ns.class_("LedTrigger", automation.Trigger.template(cg.bool_)) CONFIG_SCHEMA = ( - sensor.SENSOR_SCHEMA.extend( + sensor.sensor_schema(EZOSensor) + .extend( { - cv.GenerateID(): cv.declare_id(EZOSensor), cv.Optional(CONF_ON_CUSTOM): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CustomTrigger), diff --git a/esphome/components/integration/sensor.py b/esphome/components/integration/sensor.py index 3d7cf03882..d57f909662 100644 --- a/esphome/components/integration/sensor.py +++ b/esphome/components/integration/sensor.py @@ -48,20 +48,23 @@ def inherit_accuracy_decimals(decimals, config): return decimals + 2 -CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(IntegrationSensor), - cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor), - cv.Required(CONF_TIME_UNIT): cv.enum(INTEGRATION_TIMES, lower=True), - cv.Optional(CONF_INTEGRATION_METHOD, default="trapezoid"): cv.enum( - INTEGRATION_METHODS, lower=True - ), - cv.Optional(CONF_RESTORE, default=False): cv.boolean, - cv.Optional("min_save_interval"): cv.invalid( - "min_save_interval was removed in 2022.8.0. Please use the `preferences` -> `flash_write_interval` to adjust." - ), - } -).extend(cv.COMPONENT_SCHEMA) +CONFIG_SCHEMA = ( + sensor.sensor_schema(IntegrationSensor) + .extend( + { + cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor), + cv.Required(CONF_TIME_UNIT): cv.enum(INTEGRATION_TIMES, lower=True), + cv.Optional(CONF_INTEGRATION_METHOD, default="trapezoid"): cv.enum( + INTEGRATION_METHODS, lower=True + ), + cv.Optional(CONF_RESTORE, default=False): cv.boolean, + cv.Optional("min_save_interval"): cv.invalid( + "min_save_interval was removed in 2022.8.0. Please use the `preferences` -> `flash_write_interval` to adjust." + ), + } + ) + .extend(cv.COMPONENT_SCHEMA) +) FINAL_VALIDATE_SCHEMA = cv.All( diff --git a/esphome/components/kalman_combinator/sensor.py b/esphome/components/kalman_combinator/sensor.py index 9223f883b2..28b96077cc 100644 --- a/esphome/components/kalman_combinator/sensor.py +++ b/esphome/components/kalman_combinator/sensor.py @@ -23,20 +23,23 @@ CONF_PROCESS_STD_DEV = "process_std_dev" CONF_STD_DEV = "std_dev" -CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(cv.COMPONENT_SCHEMA).extend( - { - cv.GenerateID(): cv.declare_id(KalmanCombinatorComponent), - cv.Required(CONF_PROCESS_STD_DEV): cv.positive_float, - cv.Required(CONF_SOURCES): cv.ensure_list( - cv.Schema( - { - cv.Required(CONF_SOURCE): cv.use_id(sensor.Sensor), - cv.Required(CONF_ERROR): cv.templatable(cv.positive_float), - } +CONFIG_SCHEMA = ( + sensor.sensor_schema(KalmanCombinatorComponent) + .extend(cv.COMPONENT_SCHEMA) + .extend( + { + cv.Required(CONF_PROCESS_STD_DEV): cv.positive_float, + cv.Required(CONF_SOURCES): cv.ensure_list( + cv.Schema( + { + cv.Required(CONF_SOURCE): cv.use_id(sensor.Sensor), + cv.Required(CONF_ERROR): cv.templatable(cv.positive_float), + } + ), ), - ), - cv.Optional(CONF_STD_DEV): sensor.SENSOR_SCHEMA, - } + cv.Optional(CONF_STD_DEV): sensor.sensor_schema(), + } + ) ) # Inherit some sensor values from the first source, for both the state and the error value diff --git a/esphome/components/mcp3008/sensor.py b/esphome/components/mcp3008/sensor.py index d4b9e979ce..dd5141484b 100644 --- a/esphome/components/mcp3008/sensor.py +++ b/esphome/components/mcp3008/sensor.py @@ -14,14 +14,17 @@ MCP3008Sensor = mcp3008_ns.class_( CONF_REFERENCE_VOLTAGE = "reference_voltage" CONF_MCP3008_ID = "mcp3008_id" -CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(MCP3008Sensor), - cv.GenerateID(CONF_MCP3008_ID): cv.use_id(MCP3008), - cv.Required(CONF_NUMBER): cv.int_, - cv.Optional(CONF_REFERENCE_VOLTAGE, default="3.3V"): cv.voltage, - } -).extend(cv.polling_component_schema("1s")) +CONFIG_SCHEMA = ( + sensor.sensor_schema(MCP3008Sensor) + .extend( + { + cv.GenerateID(CONF_MCP3008_ID): cv.use_id(MCP3008), + cv.Required(CONF_NUMBER): cv.int_, + cv.Optional(CONF_REFERENCE_VOLTAGE, default="3.3V"): cv.voltage, + } + ) + .extend(cv.polling_component_schema("1s")) +) async def to_code(config): diff --git a/esphome/components/mcp3204/sensor/__init__.py b/esphome/components/mcp3204/sensor/__init__.py index 404880d405..6a81c6ec84 100644 --- a/esphome/components/mcp3204/sensor/__init__.py +++ b/esphome/components/mcp3204/sensor/__init__.py @@ -13,13 +13,16 @@ MCP3204Sensor = mcp3204_ns.class_( ) CONF_MCP3204_ID = "mcp3204_id" -CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(MCP3204Sensor), - cv.GenerateID(CONF_MCP3204_ID): cv.use_id(MCP3204), - cv.Required(CONF_NUMBER): cv.int_range(min=0, max=7), - } -).extend(cv.polling_component_schema("60s")) +CONFIG_SCHEMA = ( + sensor.sensor_schema(MCP3204Sensor) + .extend( + { + cv.GenerateID(CONF_MCP3204_ID): cv.use_id(MCP3204), + cv.Required(CONF_NUMBER): cv.int_range(min=0, max=7), + } + ) + .extend(cv.polling_component_schema("60s")) +) async def to_code(config): diff --git a/esphome/components/modbus_controller/sensor/__init__.py b/esphome/components/modbus_controller/sensor/__init__.py index da7b8928b4..0e4588cfef 100644 --- a/esphome/components/modbus_controller/sensor/__init__.py +++ b/esphome/components/modbus_controller/sensor/__init__.py @@ -32,11 +32,11 @@ ModbusSensor = modbus_controller_ns.class_( ) CONFIG_SCHEMA = cv.All( - sensor.SENSOR_SCHEMA.extend(cv.COMPONENT_SCHEMA) + sensor.sensor_schema(ModbusSensor) + .extend(cv.COMPONENT_SCHEMA) .extend(ModbusItemBaseSchema) .extend( { - cv.GenerateID(): cv.declare_id(ModbusSensor), cv.Optional(CONF_REGISTER_TYPE): cv.enum(MODBUS_REGISTER_TYPE), cv.Optional(CONF_VALUE_TYPE, default="U_WORD"): cv.enum(SENSOR_VALUE_TYPE), cv.Optional(CONF_REGISTER_COUNT, default=0): cv.positive_int, diff --git a/esphome/components/tuya/sensor/__init__.py b/esphome/components/tuya/sensor/__init__.py index 441400fa43..69711204a8 100644 --- a/esphome/components/tuya/sensor/__init__.py +++ b/esphome/components/tuya/sensor/__init__.py @@ -9,13 +9,16 @@ CODEOWNERS = ["@jesserockz"] TuyaSensor = tuya_ns.class_("TuyaSensor", sensor.Sensor, cg.Component) -CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(TuyaSensor), - cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya), - cv.Required(CONF_SENSOR_DATAPOINT): cv.uint8_t, - } -).extend(cv.COMPONENT_SCHEMA) +CONFIG_SCHEMA = ( + sensor.sensor_schema(TuyaSensor) + .extend( + { + cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya), + cv.Required(CONF_SENSOR_DATAPOINT): cv.uint8_t, + } + ) + .extend(cv.COMPONENT_SCHEMA) +) async def to_code(config):