From 5aeab5f361ea853efd2803a8fb3cde9a4539ecb8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 25 Sep 2026 20:40:42 +0100 Subject: [PATCH] [hdc302x] Use register_apply_action for heater_on and heater_off (#19673) --- esphome/components/hdc302x/hdc302x.h | 18 ---------- esphome/components/hdc302x/sensor.py | 34 ++++--------------- tests/components/hdc302x/common-actions.yaml | 12 +++++++ .../hdc302x/test-actions.esp8266-ard.yaml | 4 +++ 4 files changed, 22 insertions(+), 46 deletions(-) create mode 100644 tests/components/hdc302x/common-actions.yaml create mode 100644 tests/components/hdc302x/test-actions.esp8266-ard.yaml diff --git a/esphome/components/hdc302x/hdc302x.h b/esphome/components/hdc302x/hdc302x.h index cc5343ee89..0cf8b7a386 100644 --- a/esphome/components/hdc302x/hdc302x.h +++ b/esphome/components/hdc302x/hdc302x.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/i2c/i2c.h" @@ -48,21 +47,4 @@ class HDC302XComponent final : public PollingComponent, public i2c::I2CDevice { uint32_t conversion_delay_ms_(); }; -template class HeaterOnAction final : public Action, public Parented { - public: - TEMPLATABLE_VALUE(uint16_t, power) - TEMPLATABLE_VALUE(uint32_t, duration) - - void play(const Ts &...x) override { - auto power_val = this->power_.value(x...); - auto duration_val = this->duration_.value(x...); - this->parent_->start_heater(power_val, duration_val); - } -}; - -template class HeaterOffAction final : public Action, public Parented { - public: - void play(const Ts &...x) override { this->parent_->stop_heater(); } -}; - } // namespace esphome::hdc302x diff --git a/esphome/components/hdc302x/sensor.py b/esphome/components/hdc302x/sensor.py index 31671c5b1e..2686ba5322 100644 --- a/esphome/components/hdc302x/sensor.py +++ b/esphome/components/hdc302x/sensor.py @@ -18,8 +18,6 @@ from esphome.const import ( UNIT_CELSIUS, UNIT_PERCENT, ) -from esphome.core import ID -from esphome.cpp_generator import MockObj, TemplateArgsType from esphome.types import ConfigType DEPENDENCIES = ["i2c"] @@ -37,10 +35,6 @@ POWER_MODE_OPTIONS = { "ULTRA_LOW_POWER": HDC302XPowerMode.ULTRA_LOW_POWER, } -# Actions -HeaterOnAction = hdc302x_ns.class_("HeaterOnAction", automation.Action) -HeaterOffAction = hdc302x_ns.class_("HeaterOffAction", automation.Action) - CONFIG_SCHEMA = ( cv.Schema( { @@ -118,30 +112,14 @@ HDC302X_HEATER_ON_ACTION_SCHEMA = maybe_simple_id( ) -@automation.register_action( +automation.register_apply_action( "hdc302x.heater_on", - HeaterOnAction, HDC302X_HEATER_ON_ACTION_SCHEMA, - synchronous=True, + automation.ApplyCall( + "start_heater({}, {})", ((CONF_POWER, cg.uint16), (CONF_DURATION, cg.uint32)) + ), ) -async def hdc302x_heater_on_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_POWER], args, cg.uint16) - cg.add(var.set_power(template_)) - template_ = await cg.templatable(config[CONF_DURATION], args, cg.uint32) - cg.add(var.set_duration(template_)) - return var - -automation.register_parented_action( - "hdc302x.heater_off", - HeaterOffAction, - HDC302X_ACTION_SCHEMA, - synchronous=True, +automation.register_apply_action( + "hdc302x.heater_off", HDC302X_ACTION_SCHEMA, automation.ApplyCall("stop_heater()") ) diff --git a/tests/components/hdc302x/common-actions.yaml b/tests/components/hdc302x/common-actions.yaml new file mode 100644 index 0000000000..9bc3c13312 --- /dev/null +++ b/tests/components/hdc302x/common-actions.yaml @@ -0,0 +1,12 @@ +button: + - platform: template + name: HDC302x Actions + on_press: + - hdc302x.heater_on: + id: hdc302x_sensor + power: 0x03FF + duration: !lambda return 2000; + - hdc302x.heater_on: + id: hdc302x_sensor + power: !lambda return 0x3FFF; + - hdc302x.heater_off: hdc302x_sensor diff --git a/tests/components/hdc302x/test-actions.esp8266-ard.yaml b/tests/components/hdc302x/test-actions.esp8266-ard.yaml new file mode 100644 index 0000000000..01bf8fac7d --- /dev/null +++ b/tests/components/hdc302x/test-actions.esp8266-ard.yaml @@ -0,0 +1,4 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml + hdc302x: !include common.yaml + actions: !include common-actions.yaml