From 64ff886806fdecc22ba39b78889b7dfac71ce219 Mon Sep 17 00:00:00 2001 From: Michael Davidson Date: Wed, 20 Dec 2023 22:43:36 +1100 Subject: [PATCH] Move layout schemas into the layout items - this keeps them local to the config generation code that uses them --- .../components/graphical_layout/__init__.py | 63 +++---------------- .../display_rendering_panel.py | 13 ++++ .../graphical_layout/horizontal_stack.py | 13 ++++ .../components/graphical_layout/text_panel.py | 17 +++++ .../graphical_layout/vertical_stack.py | 13 ++++ 5 files changed, 66 insertions(+), 53 deletions(-) diff --git a/esphome/components/graphical_layout/__init__.py b/esphome/components/graphical_layout/__init__.py index 4e4e56aec8..30cd3f0b72 100644 --- a/esphome/components/graphical_layout/__init__.py +++ b/esphome/components/graphical_layout/__init__.py @@ -1,7 +1,6 @@ import esphome.codegen as cg import esphome.config_validation as cv -from esphome.components import font, color -from esphome.const import CONF_ID +from esphome.const import CONF_ID, CONF_TYPE from . import horizontal_stack from . import vertical_stack from . import text_panel @@ -18,9 +17,7 @@ AUTO_LOAD = ["display"] MULTI_CONF = True -CONF_ITEMS = "items" CONF_LAYOUT = "layout" -CONF_ITEM_TYPE = "type" BASE_ITEM_SCHEMA = cv.Schema({}) @@ -31,57 +28,17 @@ def item_type_schema(value): ITEM_TYPE_SCHEMA = cv.typed_schema( { - text_panel.CONF_TYPE: BASE_ITEM_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(text_panel.TextPanel), - cv.Optional(text_panel.CONF_ITEM_PADDING, default=0): cv.templatable( - cv.int_ - ), - cv.Required(text_panel.CONF_FONT): cv.use_id(font.Font), - cv.Optional(text_panel.CONF_FOREGROUND_COLOR): cv.use_id( - color.ColorStruct - ), - cv.Optional(text_panel.CONF_BACKGROUND_COLOR): cv.use_id( - color.ColorStruct - ), - cv.Required(text_panel.CONF_TEXT): cv.templatable(cv.string), - } + text_panel.CONF_TYPE: text_panel.get_config_schema( + BASE_ITEM_SCHEMA, item_type_schema ), - horizontal_stack.CONF_TYPE: BASE_ITEM_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(horizontal_stack.HorizontalStack), - cv.Optional( - horizontal_stack.CONF_ITEM_PADDING, default=0 - ): cv.templatable(cv.int_), - cv.Required(CONF_ITEMS): cv.All( - cv.ensure_list(item_type_schema), cv.Length(min=1) - ), - } + horizontal_stack.CONF_TYPE: horizontal_stack.get_config_schema( + BASE_ITEM_SCHEMA, item_type_schema ), - vertical_stack.CONF_TYPE: BASE_ITEM_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id(vertical_stack.VerticalStack), - cv.Optional( - vertical_stack.CONF_ITEM_PADDING, default=0 - ): cv.templatable(cv.int_), - cv.Required(CONF_ITEMS): cv.All( - cv.ensure_list(item_type_schema), cv.Length(min=1) - ), - } + vertical_stack.CONF_TYPE: vertical_stack.get_config_schema( + BASE_ITEM_SCHEMA, item_type_schema ), - display_rendering_panel.CONF_TYPE: BASE_ITEM_SCHEMA.extend( - { - cv.GenerateID(): cv.declare_id( - display_rendering_panel.DisplayRenderingPanel - ), - cv.Required(display_rendering_panel.CONF_WIDTH): cv.templatable( - cv.int_range(min=1) - ), - cv.Required(display_rendering_panel.CONF_HEIGHT): cv.templatable( - cv.int_range(min=1) - ), - cv.Required(display_rendering_panel.CONF_LAMBDA): cv.lambda_, - } + display_rendering_panel.CONF_TYPE: display_rendering_panel.get_config_schema( + BASE_ITEM_SCHEMA, item_type_schema ), } ) @@ -106,7 +63,7 @@ async def to_code(config): await cg.register_component(var, config) layout_config = config[CONF_LAYOUT] - layout_type = layout_config[CONF_ITEM_TYPE] + layout_type = layout_config[CONF_TYPE] if layout_type in CODE_GENERATORS: layout_var = await CODE_GENERATORS[layout_type](layout_config, CODE_GENERATORS) cg.add(var.set_layout_root(layout_var)) diff --git a/esphome/components/graphical_layout/display_rendering_panel.py b/esphome/components/graphical_layout/display_rendering_panel.py index 84dcd56353..1ec4dba884 100644 --- a/esphome/components/graphical_layout/display_rendering_panel.py +++ b/esphome/components/graphical_layout/display_rendering_panel.py @@ -1,4 +1,5 @@ import esphome.codegen as cg +import esphome.config_validation as cv from esphome.const import CONF_ID, CONF_WIDTH, CONF_HEIGHT, CONF_LAMBDA from esphome.components.display import DisplayRef @@ -7,6 +8,18 @@ DisplayRenderingPanel = graphical_layout_ns.class_("DisplayRenderingPanel") CONF_TYPE = "display_rendering_panel" + +def get_config_schema(base_item_schema, item_type_schema): + return base_item_schema.extend( + { + cv.GenerateID(): cv.declare_id(DisplayRenderingPanel), + cv.Required(CONF_WIDTH): cv.templatable(cv.int_range(min=1)), + cv.Required(CONF_HEIGHT): cv.templatable(cv.int_range(min=1)), + cv.Required(CONF_LAMBDA): cv.lambda_, + } + ) + + async def config_to_layout_item(item_config, child_item_builder): var = cg.new_Pvariable(item_config[CONF_ID]) diff --git a/esphome/components/graphical_layout/horizontal_stack.py b/esphome/components/graphical_layout/horizontal_stack.py index 53a3b9e510..584ce640f8 100644 --- a/esphome/components/graphical_layout/horizontal_stack.py +++ b/esphome/components/graphical_layout/horizontal_stack.py @@ -1,4 +1,5 @@ import esphome.codegen as cg +import esphome.config_validation as cv from esphome.const import CONF_ID graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout") @@ -10,6 +11,18 @@ CONF_ITEMS = "items" CONF_TYPE_KEY = "type" +def get_config_schema(base_item_schema, item_type_schema): + return base_item_schema.extend( + { + cv.GenerateID(): cv.declare_id(HorizontalStack), + cv.Optional(CONF_ITEM_PADDING, default=0): cv.templatable(cv.int_), + cv.Required(CONF_ITEMS): cv.All( + cv.ensure_list(item_type_schema), cv.Length(min=1) + ), + } + ) + + async def config_to_layout_item(item_config, child_item_builder): var = cg.new_Pvariable(item_config[CONF_ID]) diff --git a/esphome/components/graphical_layout/text_panel.py b/esphome/components/graphical_layout/text_panel.py index b74dc74def..e2b9ffd85d 100644 --- a/esphome/components/graphical_layout/text_panel.py +++ b/esphome/components/graphical_layout/text_panel.py @@ -1,4 +1,6 @@ import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import font, color from esphome.const import CONF_ID graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout") @@ -12,6 +14,21 @@ CONF_BACKGROUND_COLOR = "background_color" CONF_TEXT = "text" +def get_config_schema(base_item_schema, item_type_schema): + return base_item_schema.extend( + { + cv.GenerateID(): cv.declare_id(TextPanel), + cv.Optional(CONF_ITEM_PADDING, default=0): cv.templatable( + cv.int_ + ), + cv.Required(CONF_FONT): cv.use_id(font.Font), + cv.Optional(CONF_FOREGROUND_COLOR): cv.use_id(color.ColorStruct), + cv.Optional(CONF_BACKGROUND_COLOR): cv.use_id(color.ColorStruct), + cv.Required(CONF_TEXT): cv.templatable(cv.string), + } + ) + + async def config_to_layout_item(item_config, child_item_builder): var = cg.new_Pvariable(item_config[CONF_ID]) diff --git a/esphome/components/graphical_layout/vertical_stack.py b/esphome/components/graphical_layout/vertical_stack.py index a292404110..1406269a4f 100644 --- a/esphome/components/graphical_layout/vertical_stack.py +++ b/esphome/components/graphical_layout/vertical_stack.py @@ -1,4 +1,5 @@ import esphome.codegen as cg +import esphome.config_validation as cv from esphome.const import CONF_ID graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout") @@ -10,6 +11,18 @@ CONF_ITEMS = "items" CONF_TYPE_KEY = "type" +def get_config_schema(base_item_schema, item_type_schema): + return base_item_schema.extend( + { + cv.GenerateID(): cv.declare_id(VerticalStack), + cv.Optional(CONF_ITEM_PADDING, default=0): cv.templatable(cv.int_), + cv.Required(CONF_ITEMS): cv.All( + cv.ensure_list(item_type_schema), cv.Length(min=1) + ), + } + ) + + async def config_to_layout_item(item_config, child_item_builder): var = cg.new_Pvariable(item_config[CONF_ID])