[max6956] Use register_apply_action for set_brightness_global and set_brightness_mode (#19672)

This commit is contained in:
J. Nick Koston
2026-09-25 19:40:48 +00:00
committed by GitHub
parent 5aeab5f361
commit aef3a586de
4 changed files with 27 additions and 75 deletions
+9 -37
View File
@@ -11,8 +11,7 @@ from esphome.const import (
CONF_OUTPUT,
CONF_PULLUP,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType
CODEOWNERS = ["@looping40"]
@@ -29,10 +28,6 @@ max6956_ns = cg.esphome_ns.namespace("max6956")
MAX6956 = max6956_ns.class_("MAX6956", cg.Component, i2c.I2CDevice)
MAX6956GPIOPin = max6956_ns.class_("MAX6956GPIOPin", cg.GPIOPin)
# Actions
SetCurrentGlobalAction = max6956_ns.class_("SetCurrentGlobalAction", automation.Action)
SetCurrentModeAction = max6956_ns.class_("SetCurrentModeAction", automation.Action)
MAX6956_CURRENTMODE = max6956_ns.enum("MAX6956CURRENTMODE")
CURRENT_MODES = {
"global": MAX6956_CURRENTMODE.GLOBAL,
@@ -103,9 +98,8 @@ async def max6956_pin_to_code(config: ConfigType) -> MockObj:
return var
@automation.register_action(
automation.register_apply_action(
"max6956.set_brightness_global",
SetCurrentGlobalAction,
cv.maybe_simple_value(
{
cv.GenerateID(CONF_ID): cv.use_id(MAX6956),
@@ -115,24 +109,12 @@ async def max6956_pin_to_code(config: ConfigType) -> MockObj:
},
key=CONF_BRIGHTNESS_GLOBAL,
),
synchronous=True,
automation.ApplyField(CONF_BRIGHTNESS_GLOBAL, "set_brightness_global", cg.uint8),
automation.ApplyCall("write_brightness_global()"),
)
async def max6956_set_brightness_global_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_BRIGHTNESS_GLOBAL], args, cg.uint8)
cg.add(var.set_brightness_global(template_))
return var
@automation.register_action(
automation.register_apply_action(
"max6956.set_brightness_mode",
SetCurrentModeAction,
cv.maybe_simple_value(
{
cv.Required(CONF_ID): cv.use_id(MAX6956),
@@ -142,18 +124,8 @@ async def max6956_set_brightness_global_to_code(
},
key=CONF_BRIGHTNESS_MODE,
),
synchronous=True,
automation.ApplyField(
CONF_BRIGHTNESS_MODE, "set_brightness_mode", MAX6956_CURRENTMODE
),
automation.ApplyCall("write_brightness_mode()"),
)
async def max6956_set_brightness_mode_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(
config[CONF_BRIGHTNESS_MODE], args, MAX6956_CURRENTMODE
)
cg.add(var.set_brightness_mode(template_))
return var
-38
View File
@@ -1,38 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/max6956/max6956.h"
namespace esphome::max6956 {
template<typename... Ts> class SetCurrentGlobalAction final : public Action<Ts...> {
public:
SetCurrentGlobalAction(MAX6956 *max6956) : max6956_(max6956) {}
TEMPLATABLE_VALUE(uint8_t, brightness_global)
void play(const Ts &...x) override {
this->max6956_->set_brightness_global(this->brightness_global_.value(x...));
this->max6956_->write_brightness_global();
}
protected:
MAX6956 *max6956_;
};
template<typename... Ts> class SetCurrentModeAction final : public Action<Ts...> {
public:
SetCurrentModeAction(MAX6956 *max6956) : max6956_(max6956) {}
TEMPLATABLE_VALUE(max6956::MAX6956CURRENTMODE, brightness_mode)
void play(const Ts &...x) override {
this->max6956_->set_brightness_mode(this->brightness_mode_.value(x...));
this->max6956_->write_brightness_mode();
}
protected:
MAX6956 *max6956_;
};
} // namespace esphome::max6956
@@ -0,0 +1,14 @@
button:
- platform: template
name: MAX6956 Actions
on_press:
- max6956.set_brightness_global: 10
- max6956.set_brightness_global:
id: max6956_1
brightness_global: !lambda return 5;
- max6956.set_brightness_mode:
id: max6956_1
brightness_mode: segment
- max6956.set_brightness_mode:
id: max6956_1
brightness_mode: !lambda return max6956::GLOBAL;
@@ -0,0 +1,4 @@
packages:
i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml
max6956: !include common.yaml
actions: !include common-actions.yaml