From c561c6bbe8c9c5959edec59e8b9858fd8795d618 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 25 Sep 2026 18:02:58 +0100 Subject: [PATCH] [scd4x] Use register_apply_action for perform_forced_calibration and factory_reset (#19630) --- esphome/components/scd4x/automation.h | 31 ------------------- esphome/components/scd4x/sensor.py | 29 +++-------------- tests/components/scd4x/common-actions.yaml | 11 +++++++ .../scd4x/test-actions.esp8266-ard.yaml | 4 +++ 4 files changed, 19 insertions(+), 56 deletions(-) delete mode 100644 esphome/components/scd4x/automation.h create mode 100644 tests/components/scd4x/common-actions.yaml create mode 100644 tests/components/scd4x/test-actions.esp8266-ard.yaml diff --git a/esphome/components/scd4x/automation.h b/esphome/components/scd4x/automation.h deleted file mode 100644 index e0cc04e2cb..0000000000 --- a/esphome/components/scd4x/automation.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include "esphome/core/component.h" -#include "esphome/core/automation.h" -#include "scd4x.h" - -namespace esphome::scd4x { - -template -class PerformForcedCalibrationAction final : public Action, public Parented { - public: - // User provided, not "= default": `new(p) PerformForcedCalibrationAction()` would zero-fill .bss that is already - // zero. - PerformForcedCalibrationAction() {} - - void play(const Ts &...x) override { - if (this->value_.has_value()) { - this->parent_->perform_forced_calibration(this->value_.value(x...)); - } - } - - protected: - TEMPLATABLE_VALUE(uint16_t, value) -}; - -template class FactoryResetAction final : public Action, public Parented { - public: - void play(const Ts &...x) override { this->parent_->factory_reset(); } -}; - -} // namespace esphome::scd4x diff --git a/esphome/components/scd4x/sensor.py b/esphome/components/scd4x/sensor.py index fcae9e670e..71be6e43b1 100644 --- a/esphome/components/scd4x/sensor.py +++ b/esphome/components/scd4x/sensor.py @@ -26,8 +26,6 @@ from esphome.const import ( UNIT_PARTS_PER_MILLION, UNIT_PERCENT, ) -from esphome.core import ID -from esphome.cpp_generator import MockObj, TemplateArgsType from esphome.types import ConfigType CODEOWNERS = ["@sjtrny", "@martgras"] @@ -47,12 +45,6 @@ MEASUREMENT_MODE_OPTIONS = { } -# Actions -PerformForcedCalibrationAction = scd4x_ns.class_( - "PerformForcedCalibrationAction", automation.Action -) -FactoryResetAction = scd4x_ns.class_("FactoryResetAction", automation.Action) - CONFIG_SCHEMA = ( cv.Schema( { @@ -140,23 +132,11 @@ SCD4X_ACTION_SCHEMA = maybe_simple_id( ) -@automation.register_action( +automation.register_apply_action( "scd4x.perform_forced_calibration", - PerformForcedCalibrationAction, SCD4X_ACTION_SCHEMA, - synchronous=True, + automation.ApplyField(CONF_VALUE, "perform_forced_calibration", cg.uint16), ) -async def scd4x_frc_to_code( - config: ConfigType, - action_id: ID, - template_arg: cg.TemplateArguments, - args: TemplateArgsType, -) -> MockObj: - var = cg.new_Pvariable(action_id, template_arg) - await cg.register_parented(var, config[CONF_ID]) - template_ = await cg.templatable(config[CONF_VALUE], args, cg.uint16) - cg.add(var.set_value(template_)) - return var SCD4X_RESET_ACTION_SCHEMA = maybe_simple_id( @@ -166,9 +146,8 @@ SCD4X_RESET_ACTION_SCHEMA = maybe_simple_id( ) -automation.register_parented_action( +automation.register_apply_action( "scd4x.factory_reset", - FactoryResetAction, SCD4X_RESET_ACTION_SCHEMA, - synchronous=True, + automation.ApplyCall("factory_reset()"), ) diff --git a/tests/components/scd4x/common-actions.yaml b/tests/components/scd4x/common-actions.yaml new file mode 100644 index 0000000000..20c30e2cad --- /dev/null +++ b/tests/components/scd4x/common-actions.yaml @@ -0,0 +1,11 @@ +button: + - platform: template + name: SCD4x Actions + on_press: + - scd4x.perform_forced_calibration: + id: scd40 + value: 419 + - scd4x.perform_forced_calibration: + id: scd40 + value: !lambda return 420; + - scd4x.factory_reset: scd40 diff --git a/tests/components/scd4x/test-actions.esp8266-ard.yaml b/tests/components/scd4x/test-actions.esp8266-ard.yaml new file mode 100644 index 0000000000..c5bf817cb8 --- /dev/null +++ b/tests/components/scd4x/test-actions.esp8266-ard.yaml @@ -0,0 +1,4 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml + scd4x: !include common.yaml + actions: !include common-actions.yaml