From 447a30da0f486d75c3704b43936d3d05be4f3621 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 25 Sep 2026 20:41:54 +0100 Subject: [PATCH] [mcp4461] Use register_apply_action for the four wiper actions (#19669) --- esphome/components/mcp4461/output/__init__.py | 49 +++++----------- .../components/mcp4461/output/automation.h | 56 ------------------- .../mcp4461/output/mcp4461_output.h | 8 +++ tests/components/mcp4461/common-actions.yaml | 10 ++++ .../mcp4461/test-actions.esp8266-ard.yaml | 4 ++ 5 files changed, 37 insertions(+), 90 deletions(-) delete mode 100644 esphome/components/mcp4461/output/automation.h create mode 100644 tests/components/mcp4461/common-actions.yaml create mode 100644 tests/components/mcp4461/test-actions.esp8266-ard.yaml diff --git a/esphome/components/mcp4461/output/__init__.py b/esphome/components/mcp4461/output/__init__.py index d2b4b32174..b95a9e1510 100644 --- a/esphome/components/mcp4461/output/__init__.py +++ b/esphome/components/mcp4461/output/__init__.py @@ -3,8 +3,6 @@ import esphome.codegen as cg from esphome.components import output import esphome.config_validation as cv from esphome.const import CONF_CHANNEL, CONF_ID, CONF_INITIAL_VALUE -from esphome.core import ID -from esphome.cpp_generator import MockObj, TemplateArgsType from esphome.types import ConfigType from .. import CONF_MCP4461_ID, Mcp4461Component, mcp4461_ns @@ -121,13 +119,6 @@ async def to_code(config: ConfigType) -> None: # ---- Actions ---- -WiperIncreaseAction = mcp4461_ns.class_("WiperIncreaseAction", automation.Action) -WiperDecreaseAction = mcp4461_ns.class_("WiperDecreaseAction", automation.Action) -WiperStoreNonvolatileAction = mcp4461_ns.class_( - "WiperStoreNonvolatileAction", automation.Action -) -WiperSetTerminalAction = mcp4461_ns.class_("WiperSetTerminalAction", automation.Action) - WIPER_ACTION_SCHEMA = automation.maybe_simple_id( {cv.Required(CONF_ID): cv.use_id(Mcp4461Wiper)} ) @@ -144,43 +135,33 @@ TERMINAL_ACTION_SCHEMA = cv.Schema( ) -automation.register_simple_action( +def _char_literal(config: ConfigType, value: str) -> str: + return f"'{value}'" + + +automation.register_apply_action( "mcp4461.wiper.increase", - WiperIncreaseAction, WIPER_ACTION_SCHEMA, - synchronous=True, + automation.ApplyCall("increase_wiper()"), ) - -automation.register_simple_action( +automation.register_apply_action( "mcp4461.wiper.decrease", - WiperDecreaseAction, WIPER_ACTION_SCHEMA, - synchronous=True, + automation.ApplyCall("decrease_wiper()"), ) - -automation.register_simple_action( +automation.register_apply_action( "mcp4461.wiper.store_nonvolatile", - WiperStoreNonvolatileAction, WIPER_ACTION_SCHEMA, - synchronous=True, + automation.ApplyCall("store_nonvolatile()"), ) - -@automation.register_action( +automation.register_apply_action( "mcp4461.wiper.set_terminal", - WiperSetTerminalAction, TERMINAL_ACTION_SCHEMA, - synchronous=True, + automation.ApplyCall( + "set_terminal({}, {})", + ((CONF_TERMINAL, cg.char, _char_literal), (CONF_ENABLE, cg.bool_)), + ), ) -async def mcp4461_wiper_terminal_to_code( - config: ConfigType, - action_id: ID, - template_arg: cg.TemplateArguments, - args: TemplateArgsType, -) -> MockObj: - wiper = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable( - action_id, template_arg, wiper, ord(config[CONF_TERMINAL]), config[CONF_ENABLE] - ) diff --git a/esphome/components/mcp4461/output/automation.h b/esphome/components/mcp4461/output/automation.h deleted file mode 100644 index 4be317b2f8..0000000000 --- a/esphome/components/mcp4461/output/automation.h +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once - -#include "esphome/core/automation.h" -#include "mcp4461_output.h" - -namespace esphome::mcp4461 { - -template class WiperIncreaseAction : public Action { - public: - explicit WiperIncreaseAction(Mcp4461Wiper *wiper) : wiper_(wiper) {} - void play(Ts... x) override { this->wiper_->increase_wiper(); } - - protected: - Mcp4461Wiper *wiper_; -}; - -template class WiperDecreaseAction : public Action { - public: - explicit WiperDecreaseAction(Mcp4461Wiper *wiper) : wiper_(wiper) {} - void play(Ts... x) override { this->wiper_->decrease_wiper(); } - - protected: - Mcp4461Wiper *wiper_; -}; - -// Persist the current level to the chip's nonvolatile register immediately — useful with -// nonvolatile: false to persist only at deliberate moments (e.g. on a button press), or to -// bypass the stability delay of the automatic mirroring. -template class WiperStoreNonvolatileAction : public Action { - public: - explicit WiperStoreNonvolatileAction(Mcp4461Wiper *wiper) : wiper_(wiper) {} - void play(Ts... x) override { this->wiper_->store_nonvolatile(); } - - protected: - Mcp4461Wiper *wiper_; -}; - -template class WiperSetTerminalAction : public Action { - public: - WiperSetTerminalAction(Mcp4461Wiper *wiper, char terminal, bool enable) - : wiper_(wiper), terminal_(terminal), enable_(enable) {} - void play(Ts... x) override { - if (this->enable_) { - this->wiper_->enable_terminal(this->terminal_); - } else { - this->wiper_->disable_terminal(this->terminal_); - } - } - - protected: - Mcp4461Wiper *wiper_; - char terminal_; - bool enable_; -}; - -} // namespace esphome::mcp4461 diff --git a/esphome/components/mcp4461/output/mcp4461_output.h b/esphome/components/mcp4461/output/mcp4461_output.h index 1052369a74..f0af6037e7 100644 --- a/esphome/components/mcp4461/output/mcp4461_output.h +++ b/esphome/components/mcp4461/output/mcp4461_output.h @@ -33,6 +33,14 @@ class Mcp4461Wiper final : public output::FloatOutput, public Parentedenable_terminal(terminal); + } else { + this->disable_terminal(terminal); + } + } /// @brief Immediately persist the current wiper level to the chip's nonvolatile register /// (independent of the deferred nonvolatile mirroring / its stability delay) void store_nonvolatile(); diff --git a/tests/components/mcp4461/common-actions.yaml b/tests/components/mcp4461/common-actions.yaml new file mode 100644 index 0000000000..09dbccd554 --- /dev/null +++ b/tests/components/mcp4461/common-actions.yaml @@ -0,0 +1,10 @@ +button: + - platform: template + name: MCP4461 Actions + on_press: + - mcp4461.wiper.increase: + id: digipot_wiper_2 + - mcp4461.wiper.set_terminal: + id: digipot_wiper_2 + terminal: w + enable: true diff --git a/tests/components/mcp4461/test-actions.esp8266-ard.yaml b/tests/components/mcp4461/test-actions.esp8266-ard.yaml new file mode 100644 index 0000000000..d489a2feac --- /dev/null +++ b/tests/components/mcp4461/test-actions.esp8266-ard.yaml @@ -0,0 +1,4 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml + mcp4461: !include common.yaml + actions: !include common-actions.yaml