add actions to the MAX7219Component (#6462)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
NP v/d Spek 2025-05-14 23:15:04 +02:00 committed by GitHub
parent e7b6081c5c
commit edb8d187be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 157 additions and 1 deletions

View File

@ -0,0 +1,52 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/helpers.h"
#include "max7219digit.h"
namespace esphome {
namespace max7219digit {
template<typename... Ts> class DisplayInvertAction : public Action<Ts...>, public Parented<MAX7219Component> {
public:
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override {
bool state = this->state_.value(x...);
this->parent_->invert_on_off(state);
}
};
template<typename... Ts> class DisplayVisibilityAction : public Action<Ts...>, public Parented<MAX7219Component> {
public:
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override {
bool state = this->state_.value(x...);
this->parent_->turn_on_off(state);
}
};
template<typename... Ts> class DisplayReverseAction : public Action<Ts...>, public Parented<MAX7219Component> {
public:
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override {
bool state = this->state_.value(x...);
this->parent_->set_reverse(state);
}
};
template<typename... Ts> class DisplayIntensityAction : public Action<Ts...>, public Parented<MAX7219Component> {
public:
TEMPLATABLE_VALUE(uint8_t, state)
void play(Ts... x) override {
uint8_t state = this->state_.value(x...);
this->parent_->set_intensity(state);
}
};
} // namespace max7219digit
} // namespace esphome

View File

@ -1,7 +1,14 @@
from esphome import automation
import esphome.codegen as cg
from esphome.components import display, spi
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INTENSITY, CONF_LAMBDA, CONF_NUM_CHIPS
from esphome.const import (
CONF_ID,
CONF_INTENSITY,
CONF_LAMBDA,
CONF_NUM_CHIPS,
CONF_STATE,
)
CODEOWNERS = ["@rspaargaren"]
DEPENDENCIES = ["spi"]
@ -17,6 +24,7 @@ CONF_REVERSE_ENABLE = "reverse_enable"
CONF_NUM_CHIP_LINES = "num_chip_lines"
CONF_CHIP_LINES_STYLE = "chip_lines_style"
integration_ns = cg.esphome_ns.namespace("max7219digit")
ChipLinesStyle = integration_ns.enum("ChipLinesStyle")
CHIP_LINES_STYLE = {
@ -99,3 +107,87 @@ async def to_code(config):
config[CONF_LAMBDA], [(MAX7219ComponentRef, "it")], return_type=cg.void
)
cg.add(var.set_writer(lambda_))
DisplayInvertAction = max7219_ns.class_("DisplayInvertAction", automation.Action)
DisplayVisibilityAction = max7219_ns.class_(
"DisplayVisibilityAction", automation.Action
)
DisplayReverseAction = max7219_ns.class_("DisplayReverseAction", automation.Action)
DisplayIntensityAction = max7219_ns.class_("DisplayIntensityAction", automation.Action)
MAX7219_OFF_ACTION_SCHEMA = automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(MAX7219Component),
cv.Optional(CONF_STATE, default=False): False,
}
)
MAX7219_ON_ACTION_SCHEMA = automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(MAX7219Component),
cv.Optional(CONF_STATE, default=True): True,
}
)
@automation.register_action(
"max7129digit.invert_off", DisplayInvertAction, MAX7219_OFF_ACTION_SCHEMA
)
@automation.register_action(
"max7129digit.invert_on", DisplayInvertAction, MAX7219_ON_ACTION_SCHEMA
)
async def max7129digit_invert_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
cg.add(var.set_state(config[CONF_STATE]))
return var
@automation.register_action(
"max7129digit.turn_off", DisplayVisibilityAction, MAX7219_OFF_ACTION_SCHEMA
)
@automation.register_action(
"max7129digit.turn_on", DisplayVisibilityAction, MAX7219_ON_ACTION_SCHEMA
)
async def max7129digit_visible_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
cg.add(var.set_state(config[CONF_STATE]))
return var
@automation.register_action(
"max7129digit.reverse_off", DisplayReverseAction, MAX7219_OFF_ACTION_SCHEMA
)
@automation.register_action(
"max7129digit.reverse_on", DisplayReverseAction, MAX7219_ON_ACTION_SCHEMA
)
async def max7129digit_reverse_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
cg.add(var.set_state(config[CONF_STATE]))
return var
MAX7219_INTENSITY_SCHEMA = cv.maybe_simple_value(
{
cv.GenerateID(): cv.use_id(MAX7219Component),
cv.Optional(CONF_INTENSITY, default=15): cv.templatable(
cv.int_range(min=0, max=15)
),
},
key=CONF_INTENSITY,
)
@automation.register_action(
"max7129digit.intensity", DisplayIntensityAction, MAX7219_INTENSITY_SCHEMA
)
async def max7129digit_intensity_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
template_ = await cg.templatable(config[CONF_INTENSITY], args, cg.uint8)
cg.add(var.set_state(template_))
return var

View File

@ -14,3 +14,15 @@ display:
id: my_matrix
lambda: |-
it.printdigit("hello");
esphome:
on_boot:
- priority: 100
then:
- max7129digit.invert_off:
- max7129digit.invert_on:
- max7129digit.turn_on:
- max7129digit.turn_off:
- max7129digit.reverse_on:
- max7129digit.reverse_off:
- max7129digit.intensity: 10