From 1c72fd4674f769af5f7d23ebc78035dab7416732 Mon Sep 17 00:00:00 2001 From: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> Date: Wed, 9 Apr 2025 09:00:39 +1000 Subject: [PATCH] [lvgl] add on_boot trigger (#8498) --- esphome/components/lvgl/__init__.py | 4 +++- esphome/components/lvgl/schemas.py | 28 ++++++++++++++++++------- esphome/components/lvgl/trigger.py | 11 +++++++++- tests/components/lvgl/lvgl-package.yaml | 6 ++++++ 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/esphome/components/lvgl/__init__.py b/esphome/components/lvgl/__init__.py index 22571c2550..a3e4ee83fa 100644 --- a/esphome/components/lvgl/__init__.py +++ b/esphome/components/lvgl/__init__.py @@ -10,6 +10,7 @@ from esphome.const import ( CONF_GROUP, CONF_ID, CONF_LAMBDA, + CONF_ON_BOOT, CONF_ON_IDLE, CONF_PAGES, CONF_TIMEOUT, @@ -50,7 +51,7 @@ from .schemas import ( ) from .styles import add_top_layer, styles_to_code, theme_to_code from .touchscreens import touchscreen_schema, touchscreens_to_code -from .trigger import generate_triggers +from .trigger import add_on_boot_triggers, generate_triggers from .types import ( FontEngine, IdleTrigger, @@ -365,6 +366,7 @@ async def to_code(configs): conf[CONF_TRIGGER_ID], lv_component, False ) await build_automation(resume_trigger, [], conf) + await add_on_boot_triggers(config.get(CONF_ON_BOOT, ())) # This must be done after all widgets are created for comp in helpers.lvgl_components_required: diff --git a/esphome/components/lvgl/schemas.py b/esphome/components/lvgl/schemas.py index ae50d5b2e1..6321ae276f 100644 --- a/esphome/components/lvgl/schemas.py +++ b/esphome/components/lvgl/schemas.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_FORMAT, CONF_GROUP, CONF_ID, + CONF_ON_BOOT, CONF_ON_VALUE, CONF_STATE, CONF_TEXT, @@ -14,6 +15,7 @@ from esphome.const import ( CONF_TYPE, ) from esphome.core import TimePeriod +from esphome.core.config import StartupTrigger from esphome.schema_extractors import SCHEMA_EXTRACT from . import defines as df, lv_validation as lvalid @@ -216,14 +218,24 @@ def automation_schema(typ: LvType): events = events + (CONF_ON_VALUE,) args = typ.get_arg_type() if isinstance(typ, LvType) else [] args.append(lv_event_t_ptr) - return { - cv.Optional(event): validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(Trigger.template(*args)), - } - ) - for event in events - } + return cv.Schema( + { + cv.Optional(event): validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id( + Trigger.template(*args) + ), + } + ) + for event in events + } + ).extend( + { + cv.Optional(CONF_ON_BOOT): validate_automation( + {cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(StartupTrigger)} + ) + } + ) def base_update_schema(widget_type, parts): diff --git a/esphome/components/lvgl/trigger.py b/esphome/components/lvgl/trigger.py index b76f90fecd..283c9a5e56 100644 --- a/esphome/components/lvgl/trigger.py +++ b/esphome/components/lvgl/trigger.py @@ -1,6 +1,6 @@ from esphome import automation import esphome.codegen as cg -from esphome.const import CONF_ID, CONF_ON_VALUE, CONF_TRIGGER_ID +from esphome.const import CONF_ID, CONF_ON_BOOT, CONF_ON_VALUE, CONF_TRIGGER_ID from .defines import ( CONF_ALIGN, @@ -28,6 +28,13 @@ from .types import LV_EVENT from .widgets import LvScrActType, get_scr_act, widget_map +async def add_on_boot_triggers(triggers): + for conf in triggers: + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], 390) + await cg.register_component(trigger, conf) + await automation.build_automation(trigger, [], conf) + + async def generate_triggers(): """ Generate LVGL triggers for all defined widgets @@ -75,6 +82,8 @@ async def generate_triggers(): UPDATE_EVENT, ) + await add_on_boot_triggers(w.config.get(CONF_ON_BOOT, ())) + # Generate align to directives while we're here if align_to := w.config.get(CONF_ALIGN_TO): target = widget_map[align_to[CONF_ID]].obj diff --git a/tests/components/lvgl/lvgl-package.yaml b/tests/components/lvgl/lvgl-package.yaml index c527f51b1e..3dde14194e 100644 --- a/tests/components/lvgl/lvgl-package.yaml +++ b/tests/components/lvgl/lvgl-package.yaml @@ -24,6 +24,8 @@ lvgl: logger.log: LVGL is Paused on_resume: logger.log: LVGL has resumed + on_boot: + logger.log: LVGL has started bg_color: light_blue disp_bg_color: color_id disp_bg_image: cat_image @@ -210,6 +212,10 @@ lvgl: src: !lambda "return {dog_image, cat_image};" duration: 2s - label: + on_boot: + lvgl.label.update: + id: hello_label + text: Goodbye Cruel World id: hello_label text: Hello world text_color: 0xFF8000