Lint/clang fixes

This commit is contained in:
Michael Davidson 2023-12-17 21:34:09 +11:00
parent 25b2897268
commit 8cb27b5b78
No known key found for this signature in database
GPG key ID: B8D1A99712B8B0EB
13 changed files with 42 additions and 54 deletions

View file

@ -1,10 +1,10 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
import esphome.components.graphical_layout.horizontal_stack as horizontal_stack
import esphome.components.graphical_layout.vertical_stack as vertical_stack
import esphome.components.graphical_layout.text_panel as text_panel
from esphome.components import font, color from esphome.components import font, color
from esphome.const import CONF_ID from esphome.const import CONF_ID
from esphome.components.graphical_layout import horizontal_stack
from esphome.components.graphical_layout import vertical_stack
from esphome.components.graphical_layout import text_panel
graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout") graphical_layout_ns = cg.esphome_ns.namespace("graphical_layout")
RootLayoutComponent = graphical_layout_ns.class_("RootLayoutComponent", cg.Component) RootLayoutComponent = graphical_layout_ns.class_("RootLayoutComponent", cg.Component)
@ -21,42 +21,48 @@ CONF_ITEMS = "items"
CONF_LAYOUT = "layout" CONF_LAYOUT = "layout"
CONF_ITEM_TYPE = "type" CONF_ITEM_TYPE = "type"
BASE_ITEM_SCHEMA = cv.Schema( BASE_ITEM_SCHEMA = cv.Schema({})
{
}
)
def item_type_schema(value): def item_type_schema(value):
return ITEM_TYPE_SCHEMA(value) return ITEM_TYPE_SCHEMA(value)
ITEM_TYPE_SCHEMA = cv.typed_schema( ITEM_TYPE_SCHEMA = cv.typed_schema(
{ {
text_panel.CONF_TYPE: cv.Schema( text_panel.CONF_TYPE: BASE_ITEM_SCHEMA.extend(
{ {
cv.GenerateID(): cv.declare_id(text_panel.TextPanel), cv.GenerateID(): cv.declare_id(text_panel.TextPanel),
cv.Optional(text_panel.CONF_ITEM_PADDING, default=0): cv.templatable(cv.int_), 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.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_FOREGROUND_COLOR): cv.use_id(
cv.Optional(text_panel.CONF_BACKGROUND_COLOR): cv.use_id(color.ColorStruct), color.ColorStruct
),
cv.Optional(text_panel.CONF_BACKGROUND_COLOR): cv.use_id(
color.ColorStruct
),
cv.Required(text_panel.CONF_TEXT): cv.templatable(cv.string), cv.Required(text_panel.CONF_TEXT): cv.templatable(cv.string),
} }
), ),
horizontal_stack.CONF_TYPE: BASE_ITEM_SCHEMA.extend( horizontal_stack.CONF_TYPE: BASE_ITEM_SCHEMA.extend(
{ {
cv.GenerateID(): cv.declare_id(horizontal_stack.HorizontalStack), cv.GenerateID(): cv.declare_id(horizontal_stack.HorizontalStack),
cv.Optional(horizontal_stack.CONF_ITEM_PADDING, default=0): cv.templatable(cv.int_), cv.Optional(
horizontal_stack.CONF_ITEM_PADDING, default=0
): cv.templatable(cv.int_),
cv.Required(CONF_ITEMS): cv.All( cv.Required(CONF_ITEMS): cv.All(
cv.ensure_list(item_type_schema), cv.Length(min=1) cv.ensure_list(item_type_schema), cv.Length(min=1)
) ),
} }
), ),
vertical_stack.CONF_TYPE: BASE_ITEM_SCHEMA.extend( vertical_stack.CONF_TYPE: BASE_ITEM_SCHEMA.extend(
{ {
cv.GenerateID(): cv.declare_id(vertical_stack.VerticalStack), cv.GenerateID(): cv.declare_id(vertical_stack.VerticalStack),
cv.Optional(vertical_stack.CONF_ITEM_PADDING, default=0): cv.templatable(cv.int_), cv.Optional(
vertical_stack.CONF_ITEM_PADDING, default=0
): cv.templatable(cv.int_),
cv.Required(CONF_ITEMS): cv.All( cv.Required(CONF_ITEMS): cv.All(
cv.ensure_list(item_type_schema), cv.Length(min=1) cv.ensure_list(item_type_schema), cv.Length(min=1)
) ),
} }
) )
} }
@ -65,13 +71,13 @@ ITEM_TYPE_SCHEMA = cv.typed_schema(
CODE_GENERATORS = { CODE_GENERATORS = {
text_panel.CONF_TYPE: text_panel.config_to_layout_item, text_panel.CONF_TYPE: text_panel.config_to_layout_item,
horizontal_stack.CONF_TYPE: horizontal_stack.config_to_layout_item, horizontal_stack.CONF_TYPE: horizontal_stack.config_to_layout_item,
vertical_stack.CONF_TYPE: vertical_stack.config_to_layout_item vertical_stack.CONF_TYPE: vertical_stack.config_to_layout_item,
} }
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(
{ {
cv.GenerateID(): cv.declare_id(RootLayoutComponent), cv.GenerateID(): cv.declare_id(RootLayoutComponent),
cv.Required(CONF_LAYOUT): ITEM_TYPE_SCHEMA cv.Required(CONF_LAYOUT): ITEM_TYPE_SCHEMA,
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)

View file

@ -26,4 +26,4 @@ class ContainerLayoutItem : public LayoutItem {
}; };
} }
} }

View file

@ -48,4 +48,4 @@ void HorizontalStack::render(display::Display *display, display::Rect bounds) {
} }
} }
} }

View file

@ -22,4 +22,4 @@ class HorizontalStack : public ContainerLayoutItem {
}; };
} }
} }

View file

@ -1,5 +1,4 @@
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 +9,7 @@ CONF_TYPE = "horizontal_stack"
CONF_ITEMS = "items" CONF_ITEMS = "items"
CONF_ITEM_TYPE = "type" CONF_ITEM_TYPE = "type"
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])
@ -19,7 +19,9 @@ async def config_to_layout_item(item_config, child_item_builder):
for child_item_config in item_config[CONF_ITEMS]: for child_item_config in item_config[CONF_ITEMS]:
child_item_type = child_item_config[CONF_ITEM_TYPE] child_item_type = child_item_config[CONF_ITEM_TYPE]
if child_item_type in child_item_builder: if child_item_type in child_item_builder:
child_item_var = await child_item_builder[child_item_type](child_item_config, child_item_builder) child_item_var = await child_item_builder[child_item_type](
child_item_config, child_item_builder
)
cg.add(var.add_item(child_item_var)) cg.add(var.add_item(child_item_var))
else: else:
raise f"Do not know how to build type {child_item_type}" raise f"Do not know how to build type {child_item_type}"

View file

@ -34,4 +34,4 @@ class LayoutItem {
}; };
} }
} }

View file

@ -1,9 +0,0 @@
import esphome.config_validation as cv
from typing import Awaitable, Any, Callable, Optional
class LayoutImport:
def __init__(self, name : str, schema_builder_func: Callable[[cv.Schema, cv.Schema, cv.Schema], cv.Schema], builder_func : Awaitable[Any], parent_schema_builder_func : Optional[Callable[[], cv.Schema]] = None):
self.name = name
self.schema_builder_func = schema_builder_func
self.builder_func = builder_func
self.parent_schema_builder_func = parent_schema_builder_func

View file

@ -29,4 +29,4 @@ void TextPanel::render(display::Display *display, display::Rect bounds) {
} }
} }
} }

View file

@ -33,4 +33,4 @@ class TextPanel : public LayoutItem {
}; };
} }
} }

View file

@ -1,5 +1,4 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import font, color from esphome.components import font, color
from esphome.const import CONF_ID from esphome.const import CONF_ID
@ -13,17 +12,6 @@ CONF_FOREGROUND_COLOR = "foreground_color"
CONF_BACKGROUND_COLOR = "background_color" CONF_BACKGROUND_COLOR = "background_color"
CONF_TEXT = "text" CONF_TEXT = "text"
LAYOUT_ITEM_SCHEMA = cv.Schema(
{
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])
@ -31,8 +19,8 @@ async def config_to_layout_item(item_config, child_item_builder):
if item_padding_config := item_config[CONF_ITEM_PADDING]: if item_padding_config := item_config[CONF_ITEM_PADDING]:
cg.add(var.set_item_padding(item_padding_config)) cg.add(var.set_item_padding(item_padding_config))
font = await cg.get_variable(item_config[CONF_FONT]) panel_font = await cg.get_variable(item_config[CONF_FONT])
cg.add(var.set_font(font)) cg.add(var.set_font(panel_font))
if foreground_color_config := item_config.get(CONF_FOREGROUND_COLOR): if foreground_color_config := item_config.get(CONF_FOREGROUND_COLOR):
foreground_color = await cg.get_variable(foreground_color_config) foreground_color = await cg.get_variable(foreground_color_config)
@ -42,7 +30,7 @@ async def config_to_layout_item(item_config, child_item_builder):
background_color = await cg.get_variable(background_color_config) background_color = await cg.get_variable(background_color_config)
cg.add(var.set_background_color(background_color)) cg.add(var.set_background_color(background_color))
text = await cg.templatable(item_config[CONF_TEXT], args = [], output_type = str) text = await cg.templatable(item_config[CONF_TEXT], args=[], output_type=str)
cg.add(var.set_text(text)) cg.add(var.set_text(text))
return var return var

View file

@ -47,4 +47,4 @@ void VerticalStack::render(display::Display *display, display::Rect bounds) {
} }
} }
} }

View file

@ -22,4 +22,4 @@ class VerticalStack : public ContainerLayoutItem {
}; };
} }
} }

View file

@ -1,5 +1,4 @@
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")
@ -20,9 +19,11 @@ async def config_to_layout_item(item_config, child_item_builder):
for child_item_config in item_config[CONF_ITEMS]: for child_item_config in item_config[CONF_ITEMS]:
child_item_type = child_item_config[CONF_ITEM_TYPE] child_item_type = child_item_config[CONF_ITEM_TYPE]
if child_item_type in child_item_builder: if child_item_type in child_item_builder:
child_item_var = await child_item_builder[child_item_type](child_item_config, child_item_builder) child_item_var = await child_item_builder[child_item_type](
child_item_config, child_item_builder
)
cg.add(var.add_item(child_item_var)) cg.add(var.add_item(child_item_var))
else: else:
raise f"Do not know how to build type {child_item_type}" raise f"Do not know how to build type {child_item_type}"
return var return var