mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-02 02:58:26 +02:00
Make string globals persist-able using fixed size allocations (#5296)
Co-authored-by: Daniel Dunn <dannydunn@eternityforest.com>
This commit is contained in:
@@ -15,8 +15,14 @@ CODEOWNERS = ["@esphome/core"]
|
||||
globals_ns = cg.esphome_ns.namespace("globals")
|
||||
GlobalsComponent = globals_ns.class_("GlobalsComponent", cg.Component)
|
||||
RestoringGlobalsComponent = globals_ns.class_("RestoringGlobalsComponent", cg.Component)
|
||||
RestoringGlobalStringComponent = globals_ns.class_(
|
||||
"RestoringGlobalStringComponent", cg.Component
|
||||
)
|
||||
GlobalVarSetAction = globals_ns.class_("GlobalVarSetAction", automation.Action)
|
||||
|
||||
CONF_MAX_RESTORE_DATA_LENGTH = "max_restore_data_length"
|
||||
|
||||
|
||||
MULTI_CONF = True
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
@@ -24,6 +30,7 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
cv.Required(CONF_TYPE): cv.string_strict,
|
||||
cv.Optional(CONF_INITIAL_VALUE): cv.string_strict,
|
||||
cv.Optional(CONF_RESTORE_VALUE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_MAX_RESTORE_DATA_LENGTH): cv.int_range(0, 254),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
@@ -32,12 +39,19 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
@coroutine_with_priority(-100.0)
|
||||
async def to_code(config):
|
||||
type_ = cg.RawExpression(config[CONF_TYPE])
|
||||
template_args = cg.TemplateArguments(type_)
|
||||
restore = config[CONF_RESTORE_VALUE]
|
||||
|
||||
type = RestoringGlobalsComponent if restore else GlobalsComponent
|
||||
res_type = type.template(template_args)
|
||||
# Special casing the strings to their own class with a different save/restore mechanism
|
||||
if str(type_) == "std::string" and restore:
|
||||
template_args = cg.TemplateArguments(
|
||||
type_, config.get(CONF_MAX_RESTORE_DATA_LENGTH, 63) + 1
|
||||
)
|
||||
type = RestoringGlobalStringComponent
|
||||
else:
|
||||
template_args = cg.TemplateArguments(type_)
|
||||
type = RestoringGlobalsComponent if restore else GlobalsComponent
|
||||
|
||||
res_type = type.template(template_args)
|
||||
initial_value = None
|
||||
if CONF_INITIAL_VALUE in config:
|
||||
initial_value = cg.RawExpression(config[CONF_INITIAL_VALUE])
|
||||
|
||||
Reference in New Issue
Block a user