2019-04-17 12:06:00 +02:00
|
|
|
import hashlib
|
|
|
|
|
2019-04-24 23:49:02 +02:00
|
|
|
from esphome import config_validation as cv, automation
|
2019-04-17 12:06:00 +02:00
|
|
|
from esphome import codegen as cg
|
2021-03-07 16:03:16 -03:00
|
|
|
from esphome.const import (
|
|
|
|
CONF_ID,
|
|
|
|
CONF_INITIAL_VALUE,
|
|
|
|
CONF_RESTORE_VALUE,
|
|
|
|
CONF_TYPE,
|
|
|
|
CONF_VALUE,
|
|
|
|
)
|
2019-04-24 23:49:02 +02:00
|
|
|
from esphome.core import coroutine_with_priority
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-03-07 16:03:16 -03:00
|
|
|
CODEOWNERS = ["@esphome/core"]
|
|
|
|
globals_ns = cg.esphome_ns.namespace("globals")
|
|
|
|
GlobalsComponent = globals_ns.class_("GlobalsComponent", cg.Component)
|
2021-09-20 11:47:51 +02:00
|
|
|
RestoringGlobalsComponent = globals_ns.class_("RestoringGlobalsComponent", cg.Component)
|
2021-03-07 16:03:16 -03:00
|
|
|
GlobalVarSetAction = globals_ns.class_("GlobalVarSetAction", automation.Action)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
MULTI_CONF = True
|
2021-03-07 16:03:16 -03:00
|
|
|
CONFIG_SCHEMA = cv.Schema(
|
|
|
|
{
|
|
|
|
cv.Required(CONF_ID): cv.declare_id(GlobalsComponent),
|
|
|
|
cv.Required(CONF_TYPE): cv.string_strict,
|
|
|
|
cv.Optional(CONF_INITIAL_VALUE): cv.string_strict,
|
|
|
|
cv.Optional(CONF_RESTORE_VALUE, default=False): cv.boolean,
|
|
|
|
}
|
|
|
|
).extend(cv.COMPONENT_SCHEMA)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
2019-04-24 23:49:02 +02:00
|
|
|
# Run with low priority so that namespaces are registered first
|
|
|
|
@coroutine_with_priority(-100.0)
|
2021-05-23 22:10:30 +02:00
|
|
|
async def to_code(config):
|
2019-04-17 12:06:00 +02:00
|
|
|
type_ = cg.RawExpression(config[CONF_TYPE])
|
|
|
|
template_args = cg.TemplateArguments(type_)
|
2021-09-20 11:47:51 +02:00
|
|
|
restore = config[CONF_RESTORE_VALUE]
|
|
|
|
|
|
|
|
type = RestoringGlobalsComponent if restore else GlobalsComponent
|
|
|
|
res_type = type.template(template_args)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
initial_value = None
|
|
|
|
if CONF_INITIAL_VALUE in config:
|
|
|
|
initial_value = cg.RawExpression(config[CONF_INITIAL_VALUE])
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
rhs = type.new(template_args, initial_value)
|
2020-04-20 10:05:58 +10:00
|
|
|
glob = cg.Pvariable(config[CONF_ID], rhs, res_type)
|
2021-05-23 22:10:30 +02:00
|
|
|
await cg.register_component(glob, config)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
if restore:
|
2019-04-17 12:06:00 +02:00
|
|
|
value = config[CONF_ID].id
|
2019-12-07 18:28:55 +01:00
|
|
|
if isinstance(value, str):
|
2019-04-17 12:06:00 +02:00
|
|
|
value = value.encode()
|
|
|
|
hash_ = int(hashlib.md5(value).hexdigest()[:8], 16)
|
2021-09-20 11:47:51 +02:00
|
|
|
cg.add(glob.set_name_hash(hash_))
|
2019-04-24 23:49:02 +02:00
|
|
|
|
|
|
|
|
2021-03-07 16:03:16 -03:00
|
|
|
@automation.register_action(
|
|
|
|
"globals.set",
|
|
|
|
GlobalVarSetAction,
|
|
|
|
cv.Schema(
|
|
|
|
{
|
|
|
|
cv.Required(CONF_ID): cv.use_id(GlobalsComponent),
|
|
|
|
cv.Required(CONF_VALUE): cv.templatable(cv.string_strict),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
2021-05-23 22:10:30 +02:00
|
|
|
async def globals_set_to_code(config, action_id, template_arg, args):
|
|
|
|
full_id, paren = await cg.get_variable_with_full_id(config[CONF_ID])
|
2019-04-24 23:49:02 +02:00
|
|
|
template_arg = cg.TemplateArguments(full_id.type, *template_arg)
|
|
|
|
var = cg.new_Pvariable(action_id, template_arg, paren)
|
2021-05-23 22:10:30 +02:00
|
|
|
templ = await cg.templatable(
|
2021-03-07 16:03:16 -03:00
|
|
|
config[CONF_VALUE], args, None, to_exp=cg.RawExpression
|
|
|
|
)
|
2019-04-24 23:49:02 +02:00
|
|
|
cg.add(var.set_value(templ))
|
2021-05-23 22:10:30 +02:00
|
|
|
return var
|