mirror of
https://github.com/esphome/esphome.git
synced 2026-05-11 06:27:50 +08:00
[globals] Fix TemplatableFn deprecation warning for globals.set (#15733)
This commit is contained in:
committed by
Jesse Hills
parent
311812c8cc
commit
089a2c99e2
@@ -108,8 +108,13 @@ 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])
|
full_id, paren = await cg.get_variable_with_full_id(config[CONF_ID])
|
||||||
template_arg = cg.TemplateArguments(full_id.type, *template_arg)
|
template_arg = cg.TemplateArguments(full_id.type, *template_arg)
|
||||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||||
|
# Use the global's value_type alias as the lambda return type so
|
||||||
|
# TemplatableFn stores a direct function pointer instead of going through
|
||||||
|
# the deprecated converting trampoline when the value expression deduces
|
||||||
|
# to a different type (e.g. int literal assigned to a float global).
|
||||||
|
value_type = cg.RawExpression(f"{full_id.type}::value_type")
|
||||||
templ = await cg.templatable(
|
templ = await cg.templatable(
|
||||||
config[CONF_VALUE], args, None, to_exp=cg.RawExpression, wrap_constant=True
|
config[CONF_VALUE], args, value_type, to_exp=cg.RawExpression
|
||||||
)
|
)
|
||||||
cg.add(var.set_value(templ))
|
cg.add(var.set_value(templ))
|
||||||
return var
|
return var
|
||||||
|
|||||||
@@ -4,6 +4,14 @@ esphome:
|
|||||||
- globals.set:
|
- globals.set:
|
||||||
id: glob_int
|
id: glob_int
|
||||||
value: "10"
|
value: "10"
|
||||||
|
# Set a float global with an integer literal - must emit the correct
|
||||||
|
# return type so TemplatableFn stores a direct function pointer.
|
||||||
|
- globals.set:
|
||||||
|
id: glob_float
|
||||||
|
value: "102"
|
||||||
|
- globals.set:
|
||||||
|
id: glob_float
|
||||||
|
value: !lambda "return 42;"
|
||||||
|
|
||||||
globals:
|
globals:
|
||||||
- id: glob_int
|
- id: glob_int
|
||||||
|
|||||||
Reference in New Issue
Block a user