From 15d756d559b2c8b437855089123eb01fd53bd635 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 25 Sep 2026 19:12:35 +0100 Subject: [PATCH] [sps30] Use register_apply_action for the three sps30 actions (#19644) --- esphome/components/sps30/automation.h | 24 ------------- esphome/components/sps30/sensor.py | 35 +++++-------------- tests/components/sps30/common-actions.yaml | 7 ++++ tests/components/sps30/common.yaml | 1 + .../sps30/test-actions.esp8266-ard.yaml | 4 +++ 5 files changed, 20 insertions(+), 51 deletions(-) delete mode 100644 esphome/components/sps30/automation.h create mode 100644 tests/components/sps30/common-actions.yaml create mode 100644 tests/components/sps30/test-actions.esp8266-ard.yaml diff --git a/esphome/components/sps30/automation.h b/esphome/components/sps30/automation.h deleted file mode 100644 index ba978e7770..0000000000 --- a/esphome/components/sps30/automation.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include "esphome/core/automation.h" -#include "esphome/core/helpers.h" -#include "sps30.h" - -namespace esphome::sps30 { - -template class StartFanAction final : public Action, public Parented { - public: - void play(const Ts &...x) override { this->parent_->start_fan_cleaning(); } -}; - -template class StartMeasurementAction final : public Action, public Parented { - public: - void play(const Ts &...x) override { this->parent_->start_measurement(); } -}; - -template class StopMeasurementAction final : public Action, public Parented { - public: - void play(const Ts &...x) override { this->parent_->stop_measurement(); } -}; - -} // namespace esphome::sps30 diff --git a/esphome/components/sps30/sensor.py b/esphome/components/sps30/sensor.py index a310993d82..0026e38979 100644 --- a/esphome/components/sps30/sensor.py +++ b/esphome/components/sps30/sensor.py @@ -37,11 +37,6 @@ SPS30Component = sps30_ns.class_( "SPS30Component", cg.PollingComponent, sensirion_common.SensirionI2CDevice ) -# Actions -StartFanAction = sps30_ns.class_("StartFanAction", automation.Action) -StartMeasurementAction = sps30_ns.class_("StartMeasurementAction", automation.Action) -StopMeasurementAction = sps30_ns.class_("StopMeasurementAction", automation.Action) - CONF_AUTO_CLEANING_INTERVAL = "auto_cleaning_interval" CONF_IDLE_INTERVAL = "idle_interval" @@ -180,25 +175,11 @@ SPS30_ACTION_SCHEMA = maybe_simple_id( ) -automation.register_parented_action( - "sps30.start_fan_autoclean", - StartFanAction, - SPS30_ACTION_SCHEMA, - synchronous=True, -) - - -automation.register_parented_action( - "sps30.start_measurement", - StartMeasurementAction, - SPS30_ACTION_SCHEMA, - synchronous=True, -) - - -automation.register_parented_action( - "sps30.stop_measurement", - StopMeasurementAction, - SPS30_ACTION_SCHEMA, - synchronous=True, -) +for _name, _call in ( + ("sps30.start_fan_autoclean", "start_fan_cleaning()"), + ("sps30.start_measurement", "start_measurement()"), + ("sps30.stop_measurement", "stop_measurement()"), +): + automation.register_apply_action( + _name, SPS30_ACTION_SCHEMA, automation.ApplyCall(_call) + ) diff --git a/tests/components/sps30/common-actions.yaml b/tests/components/sps30/common-actions.yaml new file mode 100644 index 0000000000..00b85ca767 --- /dev/null +++ b/tests/components/sps30/common-actions.yaml @@ -0,0 +1,7 @@ +button: + - platform: template + name: SPS30 Actions + on_press: + - sps30.start_fan_autoclean: workshop_sps30 + - sps30.stop_measurement: workshop_sps30 + - sps30.start_measurement: workshop_sps30 diff --git a/tests/components/sps30/common.yaml b/tests/components/sps30/common.yaml index a83477b764..47811f7544 100644 --- a/tests/components/sps30/common.yaml +++ b/tests/components/sps30/common.yaml @@ -1,5 +1,6 @@ sensor: - platform: sps30 + id: workshop_sps30 i2c_id: i2c_bus pm_1_0: name: Workshop PM <1µm Weight concentration diff --git a/tests/components/sps30/test-actions.esp8266-ard.yaml b/tests/components/sps30/test-actions.esp8266-ard.yaml new file mode 100644 index 0000000000..1d4f15d887 --- /dev/null +++ b/tests/components/sps30/test-actions.esp8266-ard.yaml @@ -0,0 +1,4 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml + sps30: !include common.yaml + actions: !include common-actions.yaml