From 3e85af29c60e8067d02ac565bbd6b00188a7cd07 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Sep 2026 01:29:04 +0100 Subject: [PATCH] [hc8] Use register_apply_action for hc8.calibrate (#19681) --- esphome/components/hc8/hc8.h | 8 -------- esphome/components/hc8/sensor.py | 19 ++----------------- tests/components/hc8/common-actions.yaml | 7 +++++++ .../hc8/test-actions.esp8266-ard.yaml | 8 ++++++++ 4 files changed, 17 insertions(+), 25 deletions(-) create mode 100644 tests/components/hc8/common-actions.yaml create mode 100644 tests/components/hc8/test-actions.esp8266-ard.yaml diff --git a/esphome/components/hc8/hc8.h b/esphome/components/hc8/hc8.h index 681dffe4f6..d3399c8a2d 100644 --- a/esphome/components/hc8/hc8.h +++ b/esphome/components/hc8/hc8.h @@ -1,7 +1,6 @@ #pragma once #include "esphome/core/component.h" -#include "esphome/core/automation.h" #include "esphome/components/sensor/sensor.h" #include "esphome/components/uart/uart.h" @@ -26,11 +25,4 @@ class HC8Component final : public PollingComponent, public uart::UARTDevice { bool warmup_complete_{false}; }; -template class HC8CalibrateAction final : public Action, public Parented { - public: - TEMPLATABLE_VALUE(uint16_t, baseline) - - void play(const Ts &...x) override { this->parent_->calibrate(this->baseline_.value(x...)); } -}; - } // namespace esphome::hc8 diff --git a/esphome/components/hc8/sensor.py b/esphome/components/hc8/sensor.py index 8a19cce8d1..05d8e233ef 100644 --- a/esphome/components/hc8/sensor.py +++ b/esphome/components/hc8/sensor.py @@ -12,15 +12,12 @@ from esphome.const import ( STATE_CLASS_MEASUREMENT, UNIT_PARTS_PER_MILLION, ) -from esphome.core import ID -from esphome.cpp_generator import MockObj, TemplateArgsType from esphome.types import ConfigType DEPENDENCIES = ["uart"] hc8_ns = cg.esphome_ns.namespace("hc8") HC8Component = hc8_ns.class_("HC8Component", cg.PollingComponent, uart.UARTDevice) -HC8CalibrateAction = hc8_ns.class_("HC8CalibrateAction", automation.Action) CONFIG_SCHEMA = ( cv.Schema( @@ -73,20 +70,8 @@ CALIBRATION_ACTION_SCHEMA = cv.Schema( ) -@automation.register_action( +automation.register_apply_action( "hc8.calibrate", - HC8CalibrateAction, CALIBRATION_ACTION_SCHEMA, - synchronous=True, + automation.ApplyField(CONF_BASELINE, "calibrate", cg.uint16), ) -async def hc8_calibration_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_BASELINE], args, cg.uint16) - cg.add(var.set_baseline(template_)) - return var diff --git a/tests/components/hc8/common-actions.yaml b/tests/components/hc8/common-actions.yaml new file mode 100644 index 0000000000..798ec4cbe6 --- /dev/null +++ b/tests/components/hc8/common-actions.yaml @@ -0,0 +1,7 @@ +button: + - platform: template + name: HC8 Calibrate + on_press: + - hc8.calibrate: + id: hc8_sensor + baseline: !lambda return 400; diff --git a/tests/components/hc8/test-actions.esp8266-ard.yaml b/tests/components/hc8/test-actions.esp8266-ard.yaml new file mode 100644 index 0000000000..c19ee22eb3 --- /dev/null +++ b/tests/components/hc8/test-actions.esp8266-ard.yaml @@ -0,0 +1,8 @@ +substitutions: + tx_pin: GPIO0 + rx_pin: GPIO2 + +packages: + uart: !include ../../test_build_components/common/uart/esp8266-ard.yaml + hc8: !include common.yaml + actions: !include common-actions.yaml