mirror of
https://github.com/esphome/esphome.git
synced 2026-09-27 23:10:21 +00:00
[event] Use register_apply_action for event.trigger (#19508)
Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
This commit is contained in:
co-authored by
Jonathan Swoboda
parent
281c54bcb8
commit
c1d82168d8
@@ -16,14 +16,14 @@ from esphome.const import (
|
||||
DEVICE_CLASS_EMPTY,
|
||||
DEVICE_CLASS_MOTION,
|
||||
)
|
||||
from esphome.core import CORE, ID, CoroPriority, coroutine_with_priority
|
||||
from esphome.core import CORE, CoroPriority, coroutine_with_priority
|
||||
from esphome.core.entity_helpers import (
|
||||
entity_duplicate_validator,
|
||||
queue_entity_register,
|
||||
setup_device_class,
|
||||
setup_entity,
|
||||
)
|
||||
from esphome.cpp_generator import MockObj, MockObjClass, TemplateArgsType
|
||||
from esphome.cpp_generator import MockObj, MockObjClass
|
||||
from esphome.types import ConfigType
|
||||
|
||||
CODEOWNERS = ["@nohat"]
|
||||
@@ -40,8 +40,6 @@ event_ns = cg.esphome_ns.namespace("event")
|
||||
Event = event_ns.class_("Event", cg.EntityBase)
|
||||
EventPtr = Event.operator("ptr")
|
||||
|
||||
TriggerEventAction = event_ns.class_("TriggerEventAction", automation.Action)
|
||||
|
||||
validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True, space="_")
|
||||
|
||||
_EVENT_SCHEMA = (
|
||||
@@ -135,20 +133,11 @@ TRIGGER_EVENT_SCHEMA = cv.Schema(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"event.trigger", TriggerEventAction, TRIGGER_EVENT_SCHEMA, synchronous=True
|
||||
automation.register_apply_action(
|
||||
"event.trigger",
|
||||
TRIGGER_EVENT_SCHEMA,
|
||||
automation.ApplyField(CONF_EVENT_TYPE, "trigger", cg.std_string),
|
||||
)
|
||||
async def event_fire_to_code(
|
||||
config: ConfigType,
|
||||
action_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
templ = await cg.templatable(config[CONF_EVENT_TYPE], args, cg.std_string)
|
||||
cg.add(var.set_event_type(templ))
|
||||
return var
|
||||
|
||||
|
||||
@coroutine_with_priority(CoroPriority.CORE)
|
||||
|
||||
@@ -6,13 +6,6 @@
|
||||
|
||||
namespace esphome::event {
|
||||
|
||||
template<typename... Ts> class TriggerEventAction final : public Action<Ts...>, public Parented<Event> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(std::string, event_type)
|
||||
|
||||
void play(const Ts &...x) override { this->parent_->trigger(this->event_type_.value(x...)); }
|
||||
};
|
||||
|
||||
class EventTrigger final : public Trigger<StringRef> {
|
||||
public:
|
||||
EventTrigger(Event *event) {
|
||||
|
||||
@@ -18,3 +18,15 @@ event:
|
||||
// Log using %.*s format for StringRef
|
||||
ESP_LOGD("test", "Event type: %.*s", (int) event_type.size(), event_type.c_str());
|
||||
}
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
name: Trigger Event
|
||||
on_press:
|
||||
- event.trigger:
|
||||
id: some_event
|
||||
event_type: template_event_type1
|
||||
- event.trigger:
|
||||
id: some_event
|
||||
event_type: !lambda |-
|
||||
return id(some_event).has_event() ? "template_event_type2" : "template_event_type1";
|
||||
|
||||
Reference in New Issue
Block a user