From 34d435c99643280c297e0741a14a58574e7c879e Mon Sep 17 00:00:00 2001 From: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> Date: Mon, 12 Aug 2024 13:56:54 +1000 Subject: [PATCH] [lvgl] Implement default group for encoders (#7242) Co-authored-by: clydeps --- esphome/components/lvgl/__init__.py | 6 ++++-- esphome/components/lvgl/defines.py | 1 + esphome/components/lvgl/encoders.py | 10 +++++++--- esphome/components/lvgl/touchscreens.py | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/esphome/components/lvgl/__init__.py b/esphome/components/lvgl/__init__.py index 87fbcab4dc..6bf6e287f8 100644 --- a/esphome/components/lvgl/__init__.py +++ b/esphome/components/lvgl/__init__.py @@ -47,6 +47,7 @@ from .types import ( IdleTrigger, ObjUpdateAction, lv_font_t, + lv_group_t, lv_style_t, lvgl_ns, ) @@ -335,8 +336,9 @@ CONFIG_SCHEMA = ( cv.Optional(df.CONF_THEME): cv.Schema( {cv.Optional(name): obj_schema(w) for name, w in WIDGET_TYPES.items()} ), - cv.GenerateID(df.CONF_TOUCHSCREENS): touchscreen_schema, - cv.GenerateID(df.CONF_ENCODERS): ENCODERS_CONFIG, + cv.Optional(df.CONF_TOUCHSCREENS, default=None): touchscreen_schema, + cv.Optional(df.CONF_ENCODERS, default=None): ENCODERS_CONFIG, + cv.GenerateID(df.CONF_DEFAULT_GROUP): cv.declare_id(lv_group_t), } ) .extend(DISP_BG_SCHEMA) diff --git a/esphome/components/lvgl/defines.py b/esphome/components/lvgl/defines.py index d0047b59f7..1c6fd2678c 100644 --- a/esphome/components/lvgl/defines.py +++ b/esphome/components/lvgl/defines.py @@ -386,6 +386,7 @@ CONF_COLOR_DEPTH = "color_depth" CONF_CONTROL = "control" CONF_DEFAULT = "default" CONF_DEFAULT_FONT = "default_font" +CONF_DEFAULT_GROUP = "default_group" CONF_DIR = "dir" CONF_DISPLAYS = "displays" CONF_ENCODERS = "encoders" diff --git a/esphome/components/lvgl/encoders.py b/esphome/components/lvgl/encoders.py index caddc2e47f..cfd0e42996 100644 --- a/esphome/components/lvgl/encoders.py +++ b/esphome/components/lvgl/encoders.py @@ -5,6 +5,7 @@ import esphome.config_validation as cv from esphome.const import CONF_GROUP, CONF_ID, CONF_SENSOR from .defines import ( + CONF_DEFAULT_GROUP, CONF_ENCODERS, CONF_ENTER_BUTTON, CONF_LEFT_BUTTON, @@ -38,7 +39,10 @@ ENCODERS_CONFIG = cv.ensure_list( async def encoders_to_code(var, config): - for enc_conf in config.get(CONF_ENCODERS, ()): + default_group = lv_Pvariable(lv_group_t, config[CONF_DEFAULT_GROUP]) + lv_assign(default_group, lv_expr.group_create()) + lv.group_set_default(default_group) + for enc_conf in config[CONF_ENCODERS]: lvgl_components_required.add("KEY_LISTENER") lpt = enc_conf[CONF_LONG_PRESS_TIME].total_milliseconds lprt = enc_conf[CONF_LONG_PRESS_REPEAT_TIME].total_milliseconds @@ -60,6 +64,6 @@ async def encoders_to_code(var, config): if group := enc_conf.get(CONF_GROUP): group = lv_Pvariable(lv_group_t, group) lv_assign(group, lv_expr.group_create()) - lv.indev_set_group(lv_expr.indev_drv_register(listener.get_drv()), group) else: - lv.indev_drv_register(listener.get_drv()) + group = default_group + lv.indev_set_group(lv_expr.indev_drv_register(listener.get_drv()), group) diff --git a/esphome/components/lvgl/touchscreens.py b/esphome/components/lvgl/touchscreens.py index 292b0873f3..4d430a428e 100644 --- a/esphome/components/lvgl/touchscreens.py +++ b/esphome/components/lvgl/touchscreens.py @@ -34,7 +34,7 @@ def touchscreen_schema(config): async def touchscreens_to_code(var, config): - for tconf in config.get(CONF_TOUCHSCREENS, ()): + for tconf in config[CONF_TOUCHSCREENS]: lvgl_components_required.add(CONF_TOUCHSCREEN) touchscreen = await cg.get_variable(tconf[CONF_TOUCHSCREEN_ID]) lpt = tconf[CONF_LONG_PRESS_TIME].total_milliseconds