[graphical_display_menu] Build on_redraw with build_callback_automation (#20452)

This commit is contained in:
J. Nick Koston
2026-10-09 15:36:57 -10:00
committed by GitHub
parent 1b49e79bf8
commit 5d2433f69a
5 changed files with 35 additions and 26 deletions
@@ -13,7 +13,6 @@ from esphome.const import (
CONF_FONT,
CONF_FOREGROUND_COLOR,
CONF_ID,
CONF_TRIGGER_ID,
)
from esphome.types import ConfigType
@@ -29,9 +28,6 @@ MenuItemValueArguments = graphical_display_menu_ns.struct("MenuItemValueArgument
MenuItemValueArgumentsConstPtr = MenuItemValueArguments.operator("ptr").operator(
"const"
)
GraphicalDisplayMenuOnRedrawTrigger = graphical_display_menu_ns.class_(
"GraphicalDisplayMenuOnRedrawTrigger", automation.Trigger
)
CODEOWNERS = ["@MrMDavidson"]
DOMAIN = "graphical_display_menu"
@@ -49,13 +45,7 @@ CONFIG_SCHEMA = DISPLAY_MENU_BASE_SCHEMA.extend(
cv.Optional(CONF_MENU_ITEM_VALUE): cv.templatable(cv.string),
cv.Optional(CONF_FOREGROUND_COLOR): cv.use_id(color.ColorStruct),
cv.Optional(CONF_BACKGROUND_COLOR): cv.use_id(color.ColorStruct),
cv.Optional(CONF_ON_REDRAW): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
GraphicalDisplayMenuOnRedrawTrigger
)
}
),
cv.Optional(CONF_ON_REDRAW): automation.validate_automation({}),
}
)
)
@@ -92,9 +82,8 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_background_color(background_color))
for conf in config.get(CONF_ON_REDRAW, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger, [(GraphicalDisplayMenuConstPtr, "it")], conf
await automation.build_callback_automation(
var, "add_on_redraw_callback", [(GraphicalDisplayMenuConstPtr, "it")], conf
)
await display_menu_to_code(var, config)
@@ -241,6 +241,6 @@ void GraphicalDisplayMenu::draw_item(const display_menu_base::MenuItem *item, co
"draw_item should be called.");
}
void GraphicalDisplayMenu::update() { this->on_redraw_callbacks_.call(); }
void GraphicalDisplayMenu::update() { this->on_redraw_callbacks_.call(this); }
} // namespace esphome::graphical_display_menu
@@ -70,17 +70,7 @@ class GraphicalDisplayMenu final : public display_menu_base::DisplayMenuComponen
Color foreground_color_{COLOR_ON};
Color background_color_{COLOR_OFF};
CallbackManager<void()> on_redraw_callbacks_{};
};
class GraphicalDisplayMenuOnRedrawTrigger final : public Trigger<const GraphicalDisplayMenu *> {
public:
explicit GraphicalDisplayMenuOnRedrawTrigger(GraphicalDisplayMenu *parent) : parent_(parent) {
parent->add_on_redraw_callback([this]() { this->trigger(this->parent_); });
}
protected:
GraphicalDisplayMenu *parent_;
LazyCallbackManager<void(const GraphicalDisplayMenu *)> on_redraw_callbacks_{};
};
} // namespace graphical_display_menu
@@ -0,0 +1,24 @@
display:
- platform: ssd1306_i2c
i2c_id: i2c_bus
id: graphical_display_menu_triggers_display
model: SSD1306_128X64
reset_pin: ${reset_pin}
lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height());
font:
- file: "gfonts://Roboto"
id: graphical_display_menu_triggers_font
size: 20
graphical_display_menu:
id: graphical_display_menu_triggers_menu
display: graphical_display_menu_triggers_display
font: graphical_display_menu_triggers_font
on_redraw:
then:
lambda: 'ESP_LOGI("graphical_display_menu", "redraw, active: %d", it->is_active());'
items:
- type: label
text: "Label"
@@ -0,0 +1,6 @@
substitutions:
reset_pin: GPIO15
packages:
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
graphical_display_menu: !include common-triggers.yaml