[scd4x] Use register_apply_action for perform_forced_calibration and factory_reset (#19630)

This commit is contained in:
J. Nick Koston
2026-09-25 13:02:58 -04:00
committed by GitHub
parent 7294da9f49
commit c561c6bbe8
4 changed files with 19 additions and 56 deletions
-31
View File
@@ -1,31 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "scd4x.h"
namespace esphome::scd4x {
template<typename... Ts>
class PerformForcedCalibrationAction final : public Action<Ts...>, public Parented<SCD4XComponent> {
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<typename... Ts> class FactoryResetAction final : public Action<Ts...>, public Parented<SCD4XComponent> {
public:
void play(const Ts &...x) override { this->parent_->factory_reset(); }
};
} // namespace esphome::scd4x
+4 -25
View File
@@ -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()"),
)
@@ -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
@@ -0,0 +1,4 @@
packages:
i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml
scd4x: !include common.yaml
actions: !include common-actions.yaml