Files
esphome/tests/components/event/common.yaml
T

57 lines
1.9 KiB
YAML

event:
- platform: template
name: Event
id: some_event
event_types:
- template_event_type1
- template_event_type2
on_event:
- logger.log: Event fired
- lambda: |-
// Test get_last_event_type() returns StringRef
if (id(some_event).has_event()) {
auto event_type = id(some_event).get_last_event_type();
// Compare with string literal using ==
if (event_type == "template_event_type1") {
ESP_LOGD("test", "Event type is template_event_type1");
}
// Log using %.*s format for StringRef
ESP_LOGD("test", "Event type: %.*s", (int) event_type.size(), event_type.c_str());
}
- platform: template
name: Other Event
id: other_event
event_types:
- template_event_type1
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";
- platform: template
name: Event Types Lambdas
on_press:
- lambda: |-
// How external components and configs read and set event types
const auto &types = id(some_event).get_event_types();
for (const char *type : types)
ESP_LOGD("test", "%s", type);
if (types.empty()) {
ESP_LOGD("test", "no types");
} else if (types.size() > 1) {
ESP_LOGD("test", "%s %s", types[0], types.at(1));
const auto *it = std::find(types.begin(), types.end(), types[1]);
ESP_LOGD("test", "Index %d", (int) (it - types.begin()));
}
id(other_event).set_event_types(id(some_event).get_event_types());
id(other_event).set_event_types({"other_a", "other_b"});