[mcp4461] Use register_apply_action for the four wiper actions (#19669)

This commit is contained in:
J. Nick Koston
2026-09-25 15:41:54 -04:00
committed by GitHub
parent d8696c4f28
commit 447a30da0f
5 changed files with 37 additions and 90 deletions
+15 -34
View File
@@ -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]
)
@@ -1,56 +0,0 @@
#pragma once
#include "esphome/core/automation.h"
#include "mcp4461_output.h"
namespace esphome::mcp4461 {
template<typename... Ts> class WiperIncreaseAction : public Action<Ts...> {
public:
explicit WiperIncreaseAction(Mcp4461Wiper *wiper) : wiper_(wiper) {}
void play(Ts... x) override { this->wiper_->increase_wiper(); }
protected:
Mcp4461Wiper *wiper_;
};
template<typename... Ts> class WiperDecreaseAction : public Action<Ts...> {
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<typename... Ts> class WiperStoreNonvolatileAction : public Action<Ts...> {
public:
explicit WiperStoreNonvolatileAction(Mcp4461Wiper *wiper) : wiper_(wiper) {}
void play(Ts... x) override { this->wiper_->store_nonvolatile(); }
protected:
Mcp4461Wiper *wiper_;
};
template<typename... Ts> class WiperSetTerminalAction : public Action<Ts...> {
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
@@ -33,6 +33,14 @@ class Mcp4461Wiper final : public output::FloatOutput, public Parented<Mcp4461Co
/// @brief Disable given terminal
/// @param[in] terminal single char parameter defining desired terminal to disable, one of { 'a', 'b', 'w', 'h' }
void disable_terminal(char terminal);
/// @brief Enable or disable given terminal
void set_terminal(char terminal, bool enable) {
if (enable) {
this->enable_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();
@@ -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
@@ -0,0 +1,4 @@
packages:
i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml
mcp4461: !include common.yaml
actions: !include common-actions.yaml