diff --git a/esphome/components/audio/audio_decoder.cpp b/esphome/components/audio/audio_decoder.cpp index 60489d7d78..c74b028c4b 100644 --- a/esphome/components/audio/audio_decoder.cpp +++ b/esphome/components/audio/audio_decoder.cpp @@ -171,7 +171,7 @@ AudioDecoderState AudioDecoder::decode(bool stop_gracefully) { bytes_available_before_processing = this->input_transfer_buffer_->available(); - if ((this->potentially_failed_count_ > 10) && (bytes_read == 0)) { + if ((this->potentially_failed_count_ > 0) && (bytes_read == 0)) { // Failed to decode in last attempt and there is no new data if ((this->input_transfer_buffer_->free() == 0) && first_loop_iteration) { diff --git a/esphome/components/dfrobot_sen0395/switch/__init__.py b/esphome/components/dfrobot_sen0395/switch/__init__.py index f854d08398..8e492080de 100644 --- a/esphome/components/dfrobot_sen0395/switch/__init__.py +++ b/esphome/components/dfrobot_sen0395/switch/__init__.py @@ -2,6 +2,7 @@ import esphome.codegen as cg from esphome.components import switch import esphome.config_validation as cv from esphome.const import CONF_TYPE, ENTITY_CATEGORY_CONFIG +from esphome.cpp_generator import MockObjClass from .. import CONF_DFROBOT_SEN0395_ID, DfrobotSen0395Component @@ -26,32 +27,30 @@ Sen0395StartAfterBootSwitch = dfrobot_sen0395_ns.class_( "Sen0395StartAfterBootSwitch", DfrobotSen0395Switch ) -_SWITCH_SCHEMA = ( - switch.switch_schema( - entity_category=ENTITY_CATEGORY_CONFIG, + +def _switch_schema(class_: MockObjClass) -> cv.Schema: + return ( + switch.switch_schema( + class_, + entity_category=ENTITY_CATEGORY_CONFIG, + ) + .extend( + { + cv.GenerateID(CONF_DFROBOT_SEN0395_ID): cv.use_id( + DfrobotSen0395Component + ), + } + ) + .extend(cv.COMPONENT_SCHEMA) ) - .extend( - { - cv.GenerateID(CONF_DFROBOT_SEN0395_ID): cv.use_id(DfrobotSen0395Component), - } - ) - .extend(cv.COMPONENT_SCHEMA) -) + CONFIG_SCHEMA = cv.typed_schema( { - "sensor_active": _SWITCH_SCHEMA.extend( - {cv.GenerateID(): cv.declare_id(Sen0395PowerSwitch)} - ), - "turn_on_led": _SWITCH_SCHEMA.extend( - {cv.GenerateID(): cv.declare_id(Sen0395LedSwitch)} - ), - "presence_via_uart": _SWITCH_SCHEMA.extend( - {cv.GenerateID(): cv.declare_id(Sen0395UartPresenceSwitch)} - ), - "start_after_boot": _SWITCH_SCHEMA.extend( - {cv.GenerateID(): cv.declare_id(Sen0395StartAfterBootSwitch)} - ), + "sensor_active": _switch_schema(Sen0395PowerSwitch), + "turn_on_led": _switch_schema(Sen0395LedSwitch), + "presence_via_uart": _switch_schema(Sen0395UartPresenceSwitch), + "start_after_boot": _switch_schema(Sen0395StartAfterBootSwitch), } ) diff --git a/esphome/components/esp32/gpio.py b/esphome/components/esp32/gpio.py index df01769a66..2bb10ce6ec 100644 --- a/esphome/components/esp32/gpio.py +++ b/esphome/components/esp32/gpio.py @@ -1,6 +1,6 @@ from dataclasses import dataclass import logging -from typing import Any +from typing import Any, Callable from esphome import pins import esphome.codegen as cg @@ -64,8 +64,7 @@ def _lookup_pin(value): def _translate_pin(value): if isinstance(value, dict) or value is None: raise cv.Invalid( - "This variable only supports pin numbers, not full pin schemas " - "(with inverted and mode)." + "This variable only supports pin numbers, not full pin schemas (with inverted and mode)." ) if isinstance(value, int) and not isinstance(value, bool): return value @@ -82,30 +81,22 @@ def _translate_pin(value): @dataclass class ESP32ValidationFunctions: - pin_validation: Any - usage_validation: Any + pin_validation: Callable[[Any], Any] + usage_validation: Callable[[Any], Any] _esp32_validations = { VARIANT_ESP32: ESP32ValidationFunctions( pin_validation=esp32_validate_gpio_pin, usage_validation=esp32_validate_supports ), - VARIANT_ESP32S2: ESP32ValidationFunctions( - pin_validation=esp32_s2_validate_gpio_pin, - usage_validation=esp32_s2_validate_supports, + VARIANT_ESP32C2: ESP32ValidationFunctions( + pin_validation=esp32_c2_validate_gpio_pin, + usage_validation=esp32_c2_validate_supports, ), VARIANT_ESP32C3: ESP32ValidationFunctions( pin_validation=esp32_c3_validate_gpio_pin, usage_validation=esp32_c3_validate_supports, ), - VARIANT_ESP32S3: ESP32ValidationFunctions( - pin_validation=esp32_s3_validate_gpio_pin, - usage_validation=esp32_s3_validate_supports, - ), - VARIANT_ESP32C2: ESP32ValidationFunctions( - pin_validation=esp32_c2_validate_gpio_pin, - usage_validation=esp32_c2_validate_supports, - ), VARIANT_ESP32C6: ESP32ValidationFunctions( pin_validation=esp32_c6_validate_gpio_pin, usage_validation=esp32_c6_validate_supports, @@ -114,6 +105,14 @@ _esp32_validations = { pin_validation=esp32_h2_validate_gpio_pin, usage_validation=esp32_h2_validate_supports, ), + VARIANT_ESP32S2: ESP32ValidationFunctions( + pin_validation=esp32_s2_validate_gpio_pin, + usage_validation=esp32_s2_validate_supports, + ), + VARIANT_ESP32S3: ESP32ValidationFunctions( + pin_validation=esp32_s3_validate_gpio_pin, + usage_validation=esp32_s3_validate_supports, + ), } diff --git a/esphome/components/esp32/gpio_esp32.py b/esphome/components/esp32/gpio_esp32.py index e4d3b6aaf3..973d2dc0ef 100644 --- a/esphome/components/esp32/gpio_esp32.py +++ b/esphome/components/esp32/gpio_esp32.py @@ -31,8 +31,7 @@ def esp32_validate_gpio_pin(value): ) if 9 <= value <= 10: _LOGGER.warning( - "Pin %s (9-10) might already be used by the " - "flash interface in QUAD IO flash mode.", + "Pin %s (9-10) might already be used by the flash interface in QUAD IO flash mode.", value, ) if value in (24, 28, 29, 30, 31): diff --git a/esphome/components/esp32/gpio_esp32_c2.py b/esphome/components/esp32/gpio_esp32_c2.py index abdcb1b655..32a24050ca 100644 --- a/esphome/components/esp32/gpio_esp32_c2.py +++ b/esphome/components/esp32/gpio_esp32_c2.py @@ -22,7 +22,7 @@ def esp32_c2_validate_supports(value): is_input = mode[CONF_INPUT] if num < 0 or num > 20: - raise cv.Invalid(f"Invalid pin number: {value} (must be 0-20)") + raise cv.Invalid(f"Invalid pin number: {num} (must be 0-20)") if is_input: # All ESP32 pins support input mode diff --git a/esphome/components/esp32/gpio_esp32_c3.py b/esphome/components/esp32/gpio_esp32_c3.py index 5b9ec0ebd9..c1427cc02a 100644 --- a/esphome/components/esp32/gpio_esp32_c3.py +++ b/esphome/components/esp32/gpio_esp32_c3.py @@ -35,7 +35,7 @@ def esp32_c3_validate_supports(value): is_input = mode[CONF_INPUT] if num < 0 or num > 21: - raise cv.Invalid(f"Invalid pin number: {value} (must be 0-21)") + raise cv.Invalid(f"Invalid pin number: {num} (must be 0-21)") if is_input: # All ESP32 pins support input mode diff --git a/esphome/components/esp32/gpio_esp32_c6.py b/esphome/components/esp32/gpio_esp32_c6.py index bc735f85c4..d466adb994 100644 --- a/esphome/components/esp32/gpio_esp32_c6.py +++ b/esphome/components/esp32/gpio_esp32_c6.py @@ -36,7 +36,7 @@ def esp32_c6_validate_supports(value): is_input = mode[CONF_INPUT] if num < 0 or num > 23: - raise cv.Invalid(f"Invalid pin number: {value} (must be 0-23)") + raise cv.Invalid(f"Invalid pin number: {num} (must be 0-23)") if is_input: # All ESP32 pins support input mode pass diff --git a/esphome/components/esp32/gpio_esp32_h2.py b/esphome/components/esp32/gpio_esp32_h2.py index 7413bf4db5..7c3a658b17 100644 --- a/esphome/components/esp32/gpio_esp32_h2.py +++ b/esphome/components/esp32/gpio_esp32_h2.py @@ -45,7 +45,7 @@ def esp32_h2_validate_supports(value): is_input = mode[CONF_INPUT] if num < 0 or num > 27: - raise cv.Invalid(f"Invalid pin number: {value} (must be 0-27)") + raise cv.Invalid(f"Invalid pin number: {num} (must be 0-27)") if is_input: # All ESP32 pins support input mode pass diff --git a/esphome/components/switch/__init__.py b/esphome/components/switch/__init__.py index 1c65aa8dfc..e7445051e0 100644 --- a/esphome/components/switch/__init__.py +++ b/esphome/components/switch/__init__.py @@ -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")) diff --git a/esphome/components/text_sensor/__init__.py b/esphome/components/text_sensor/__init__.py index 92b08aa6d0..888b65745f 100644 --- a/esphome/components/text_sensor/__init__.py +++ b/esphome/components/text_sensor/__init__.py @@ -156,32 +156,24 @@ _TEXT_SENSOR_SCHEMA = ( def text_sensor_schema( class_: MockObjClass = cv.UNDEFINED, *, - icon: str = cv.UNDEFINED, - entity_category: str = cv.UNDEFINED, device_class: str = cv.UNDEFINED, + entity_category: str = cv.UNDEFINED, + icon: str = cv.UNDEFINED, ) -> cv.Schema: - schema = _TEXT_SENSOR_SCHEMA + schema = {} + if class_ is not cv.UNDEFINED: - schema = schema.extend({cv.GenerateID(): cv.declare_id(class_)}) - if icon is not cv.UNDEFINED: - schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon}) - if device_class is not cv.UNDEFINED: - schema = schema.extend( - { - cv.Optional( - CONF_DEVICE_CLASS, default=device_class - ): validate_device_class - } - ) - if entity_category is not cv.UNDEFINED: - schema = schema.extend( - { - cv.Optional( - CONF_ENTITY_CATEGORY, default=entity_category - ): cv.entity_category - } - ) - return schema + schema[cv.GenerateID()] = cv.declare_id(class_) + + for key, default, validator in [ + (CONF_ICON, icon, cv.icon), + (CONF_DEVICE_CLASS, device_class, validate_device_class), + (CONF_ENTITY_CATEGORY, entity_category, cv.entity_category), + ]: + if default is not cv.UNDEFINED: + schema[cv.Optional(key, default=default)] = validator + + return _TEXT_SENSOR_SCHEMA.extend(schema) # Remove before 2025.11.0 diff --git a/tests/components/dfrobot_sen0395/common.yaml b/tests/components/dfrobot_sen0395/common.yaml index 69bcebf182..8c349911d3 100644 --- a/tests/components/dfrobot_sen0395/common.yaml +++ b/tests/components/dfrobot_sen0395/common.yaml @@ -26,3 +26,17 @@ dfrobot_sen0395: binary_sensor: - platform: dfrobot_sen0395 id: mmwave_detected + +switch: + - platform: dfrobot_sen0395 + type: sensor_active + id: mmwave_sensor_active + - platform: dfrobot_sen0395 + type: turn_on_led + id: mmwave_turn_on_led + - platform: dfrobot_sen0395 + type: presence_via_uart + id: mmwave_presence_via_uart + - platform: dfrobot_sen0395 + type: start_after_boot + id: mmwave_start_after_boot