From edb8d187bef60080749689b57c89ea154e7f68a2 Mon Sep 17 00:00:00 2001 From: NP v/d Spek Date: Wed, 14 May 2025 23:15:04 +0200 Subject: [PATCH] add actions to the MAX7219Component (#6462) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/max7219digit/automation.h | 52 +++++++++++ esphome/components/max7219digit/display.py | 94 +++++++++++++++++++- tests/components/max7219digit/common.yaml | 12 +++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 esphome/components/max7219digit/automation.h diff --git a/esphome/components/max7219digit/automation.h b/esphome/components/max7219digit/automation.h new file mode 100644 index 0000000000..02acebb109 --- /dev/null +++ b/esphome/components/max7219digit/automation.h @@ -0,0 +1,52 @@ +#pragma once + +#include "esphome/core/automation.h" +#include "esphome/core/helpers.h" + +#include "max7219digit.h" + +namespace esphome { +namespace max7219digit { + +template class DisplayInvertAction : public Action, public Parented { + public: + TEMPLATABLE_VALUE(bool, state) + + void play(Ts... x) override { + bool state = this->state_.value(x...); + this->parent_->invert_on_off(state); + } +}; + +template class DisplayVisibilityAction : public Action, public Parented { + public: + TEMPLATABLE_VALUE(bool, state) + + void play(Ts... x) override { + bool state = this->state_.value(x...); + this->parent_->turn_on_off(state); + } +}; + +template class DisplayReverseAction : public Action, public Parented { + public: + TEMPLATABLE_VALUE(bool, state) + + void play(Ts... x) override { + bool state = this->state_.value(x...); + this->parent_->set_reverse(state); + } +}; + +template class DisplayIntensityAction : public Action, public Parented { + 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 diff --git a/esphome/components/max7219digit/display.py b/esphome/components/max7219digit/display.py index 582d11bf4f..f195078c1a 100644 --- a/esphome/components/max7219digit/display.py +++ b/esphome/components/max7219digit/display.py @@ -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 diff --git a/tests/components/max7219digit/common.yaml b/tests/components/max7219digit/common.yaml index a5a3bd57fb..84edc7eb3d 100644 --- a/tests/components/max7219digit/common.yaml +++ b/tests/components/max7219digit/common.yaml @@ -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