Move layout schemas into the layout items - this keeps them local to the config generation code that uses them

This commit is contained in:
Michael Davidson 2023-12-20 22:43:36 +11:00
parent af81251a7c
commit 64ff886806
No known key found for this signature in database
GPG key ID: B8D1A99712B8B0EB
5 changed files with 66 additions and 53 deletions

View file

@ -1,7 +1,6 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import font, color from esphome.const import CONF_ID, CONF_TYPE
from esphome.const import CONF_ID
from . import horizontal_stack from . import horizontal_stack
from . import vertical_stack from . import vertical_stack
from . import text_panel from . import text_panel
@ -18,9 +17,7 @@ AUTO_LOAD = ["display"]
MULTI_CONF = True MULTI_CONF = True
CONF_ITEMS = "items"
CONF_LAYOUT = "layout" CONF_LAYOUT = "layout"
CONF_ITEM_TYPE = "type"
BASE_ITEM_SCHEMA = cv.Schema({}) BASE_ITEM_SCHEMA = cv.Schema({})
@ -31,57 +28,17 @@ def item_type_schema(value):
ITEM_TYPE_SCHEMA = cv.typed_schema( ITEM_TYPE_SCHEMA = cv.typed_schema(
{ {
text_panel.CONF_TYPE: BASE_ITEM_SCHEMA.extend( text_panel.CONF_TYPE: text_panel.get_config_schema(
{ BASE_ITEM_SCHEMA, item_type_schema
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), horizontal_stack.CONF_TYPE: horizontal_stack.get_config_schema(
cv.Optional(text_panel.CONF_FOREGROUND_COLOR): cv.use_id( BASE_ITEM_SCHEMA, item_type_schema
color.ColorStruct
), ),
cv.Optional(text_panel.CONF_BACKGROUND_COLOR): cv.use_id( vertical_stack.CONF_TYPE: vertical_stack.get_config_schema(
color.ColorStruct BASE_ITEM_SCHEMA, item_type_schema
), ),
cv.Required(text_panel.CONF_TEXT): cv.templatable(cv.string), display_rendering_panel.CONF_TYPE: display_rendering_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)
),
}
),
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)
),
}
),
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_,
}
), ),
} }
) )
@ -106,7 +63,7 @@ async def to_code(config):
await cg.register_component(var, config) await cg.register_component(var, config)
layout_config = config[CONF_LAYOUT] layout_config = config[CONF_LAYOUT]
layout_type = layout_config[CONF_ITEM_TYPE] layout_type = layout_config[CONF_TYPE]
if layout_type in CODE_GENERATORS: if layout_type in CODE_GENERATORS:
layout_var = await CODE_GENERATORS[layout_type](layout_config, CODE_GENERATORS) layout_var = await CODE_GENERATORS[layout_type](layout_config, CODE_GENERATORS)
cg.add(var.set_layout_root(layout_var)) cg.add(var.set_layout_root(layout_var))

View file

@ -1,4 +1,5 @@
import esphome.codegen as cg 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.const import CONF_ID, CONF_WIDTH, CONF_HEIGHT, CONF_LAMBDA
from esphome.components.display import DisplayRef from esphome.components.display import DisplayRef
@ -7,6 +8,18 @@ DisplayRenderingPanel = graphical_layout_ns.class_("DisplayRenderingPanel")
CONF_TYPE = "display_rendering_panel" 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): async def config_to_layout_item(item_config, child_item_builder):
var = cg.new_Pvariable(item_config[CONF_ID]) var = cg.new_Pvariable(item_config[CONF_ID])

View file

@ -1,4 +1,5 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ID from esphome.const import CONF_ID
graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout") graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout")
@ -10,6 +11,18 @@ CONF_ITEMS = "items"
CONF_TYPE_KEY = "type" 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): async def config_to_layout_item(item_config, child_item_builder):
var = cg.new_Pvariable(item_config[CONF_ID]) var = cg.new_Pvariable(item_config[CONF_ID])

View file

@ -1,4 +1,6 @@
import esphome.codegen as cg 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
graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout") graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout")
@ -12,6 +14,21 @@ CONF_BACKGROUND_COLOR = "background_color"
CONF_TEXT = "text" 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): async def config_to_layout_item(item_config, child_item_builder):
var = cg.new_Pvariable(item_config[CONF_ID]) var = cg.new_Pvariable(item_config[CONF_ID])

View file

@ -1,4 +1,5 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ID from esphome.const import CONF_ID
graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout") graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout")
@ -10,6 +11,18 @@ CONF_ITEMS = "items"
CONF_TYPE_KEY = "type" 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): async def config_to_layout_item(item_config, child_item_builder):
var = cg.new_Pvariable(item_config[CONF_ID]) var = cg.new_Pvariable(item_config[CONF_ID])