mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 07:20:24 +00:00
[globals] Use register_apply_action for globals.set (#19481)
This commit is contained in:
@@ -8,8 +8,7 @@ from esphome.const import (
|
||||
CONF_TYPE,
|
||||
CONF_VALUE,
|
||||
)
|
||||
from esphome.core import ID, CoroPriority, coroutine_with_priority
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.core import CoroPriority, coroutine_with_priority
|
||||
from esphome.types import ConfigType
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
@@ -21,7 +20,6 @@ RestoringGlobalsComponent = globals_ns.class_(
|
||||
RestoringGlobalStringComponent = globals_ns.class_(
|
||||
"RestoringGlobalStringComponent", cg.PollingComponent
|
||||
)
|
||||
GlobalVarSetAction = globals_ns.class_("GlobalVarSetAction", automation.Action)
|
||||
|
||||
CONF_MAX_RESTORE_DATA_LENGTH = "max_restore_data_length"
|
||||
|
||||
@@ -94,33 +92,19 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add(glob.set_name_hash(hash_))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
# A lambda returns the global's own type so `return 42;` reduces to a cast; the value
|
||||
# itself is raw C++ text.
|
||||
_VALUE_TYPE = "std::remove_reference_t<decltype({parent}->value())>"
|
||||
|
||||
automation.register_apply_action(
|
||||
"globals.set",
|
||||
GlobalVarSetAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(GlobalsComponent),
|
||||
cv.Required(CONF_VALUE): cv.templatable(cv.string_strict),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
automation.ApplyField(
|
||||
CONF_VALUE, "value() = {}", _VALUE_TYPE, const_fn=lambda _, value: value
|
||||
),
|
||||
)
|
||||
async def globals_set_to_code(
|
||||
config: ConfigType,
|
||||
action_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
full_id, paren = await cg.get_variable_with_full_id(config[CONF_ID])
|
||||
template_arg = cg.TemplateArguments(full_id.type, *template_arg)
|
||||
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(
|
||||
config[CONF_VALUE], args, value_type, to_exp=cg.RawExpression
|
||||
)
|
||||
cg.add(var.set_value(templ))
|
||||
return var
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
|
||||
namespace esphome::globals {
|
||||
@@ -127,20 +129,6 @@ template<typename T, uint8_t SZ> class RestoringGlobalStringComponent : public P
|
||||
ESPPreferenceObject rtc_;
|
||||
};
|
||||
|
||||
template<class C, typename... Ts> class GlobalVarSetAction final : public Action<Ts...> {
|
||||
public:
|
||||
explicit GlobalVarSetAction(C *parent) : parent_(parent) {}
|
||||
|
||||
using T = typename C::value_type;
|
||||
|
||||
TEMPLATABLE_VALUE(T, value);
|
||||
|
||||
void play(const Ts &...x) override { this->parent_->value() = this->value_.value(x...); }
|
||||
|
||||
protected:
|
||||
C *parent_;
|
||||
};
|
||||
|
||||
template<typename T> T &id(GlobalsComponent<T> *value) { return value->value(); }
|
||||
template<typename T> T &id(RestoringGlobalsComponent<T> *value) { return value->value(); }
|
||||
template<typename T, uint8_t SZ> T &id(RestoringGlobalStringComponent<T, SZ> *value) { return value->value(); }
|
||||
|
||||
@@ -4,8 +4,8 @@ esphome:
|
||||
- globals.set:
|
||||
id: glob_int
|
||||
value: "10"
|
||||
# Set a float global with an integer literal - must emit the correct
|
||||
# return type so TemplatableFn stores a direct function pointer.
|
||||
# Set a float global with an integer literal; the lambda must return the
|
||||
# global's own type so the assignment needs no conversion.
|
||||
- globals.set:
|
||||
id: glob_float
|
||||
value: "102"
|
||||
|
||||
Reference in New Issue
Block a user