mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-04 20:09:35 +02:00
Event entity support (#6451)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -63,6 +63,9 @@
|
||||
#ifdef USE_ALARM_CONTROL_PANEL
|
||||
#include "esphome/components/alarm_control_panel/alarm_control_panel.h"
|
||||
#endif
|
||||
#ifdef USE_EVENT
|
||||
#include "esphome/components/event/event.h"
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
|
||||
@@ -164,6 +167,10 @@ class Application {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_EVENT
|
||||
void register_event(event::Event *event) { this->events_.push_back(event); }
|
||||
#endif
|
||||
|
||||
/// Register the component in this Application instance.
|
||||
template<class C> C *register_component(C *c) {
|
||||
static_assert(std::is_base_of<Component, C>::value, "Only Component subclasses can be registered");
|
||||
@@ -386,6 +393,16 @@ class Application {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_EVENT
|
||||
const std::vector<event::Event *> &get_events() { return this->events_; }
|
||||
event::Event *get_event_by_key(uint32_t key, bool include_internal = false) {
|
||||
for (auto *obj : this->events_)
|
||||
if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
|
||||
return obj;
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
Scheduler scheduler;
|
||||
|
||||
protected:
|
||||
@@ -409,6 +426,9 @@ class Application {
|
||||
#ifdef USE_BUTTON
|
||||
std::vector<button::Button *> buttons_{};
|
||||
#endif
|
||||
#ifdef USE_EVENT
|
||||
std::vector<event::Event *> events_{};
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
std::vector<sensor::Sensor *> sensors_{};
|
||||
#endif
|
||||
|
||||
@@ -321,6 +321,21 @@ void ComponentIterator::advance() {
|
||||
}
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#ifdef USE_EVENT
|
||||
case IteratorState::EVENT:
|
||||
if (this->at_ >= App.get_events().size()) {
|
||||
advance_platform = true;
|
||||
} else {
|
||||
auto *event = App.get_events()[this->at_];
|
||||
if (event->is_internal() && !this->include_internal_) {
|
||||
success = true;
|
||||
break;
|
||||
} else {
|
||||
success = this->on_event(event);
|
||||
}
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case IteratorState::MAX:
|
||||
if (this->on_end()) {
|
||||
|
||||
@@ -80,6 +80,9 @@ class ComponentIterator {
|
||||
#endif
|
||||
#ifdef USE_ALARM_CONTROL_PANEL
|
||||
virtual bool on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) = 0;
|
||||
#endif
|
||||
#ifdef USE_EVENT
|
||||
virtual bool on_event(event::Event *event) = 0;
|
||||
#endif
|
||||
virtual bool on_end();
|
||||
|
||||
@@ -146,6 +149,9 @@ class ComponentIterator {
|
||||
#endif
|
||||
#ifdef USE_ALARM_CONTROL_PANEL
|
||||
ALARM_CONTROL_PANEL,
|
||||
#endif
|
||||
#ifdef USE_EVENT
|
||||
EVENT,
|
||||
#endif
|
||||
MAX,
|
||||
} state_{IteratorState::NONE};
|
||||
|
||||
@@ -109,6 +109,12 @@ void Controller::setup_controller(bool include_internal) {
|
||||
obj->add_on_state_callback([this, obj]() { this->on_alarm_control_panel_update(obj); });
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_EVENT
|
||||
for (auto *obj : App.get_events()) {
|
||||
if (include_internal || !obj->is_internal())
|
||||
obj->add_on_event_callback([this, obj](const std::string &event_type) { this->on_event(obj, event_type); });
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
@@ -55,6 +55,9 @@
|
||||
#ifdef USE_ALARM_CONTROL_PANEL
|
||||
#include "esphome/components/alarm_control_panel/alarm_control_panel.h"
|
||||
#endif
|
||||
#ifdef USE_EVENT
|
||||
#include "esphome/components/event/event.h"
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
|
||||
@@ -112,6 +115,9 @@ class Controller {
|
||||
#ifdef USE_ALARM_CONTROL_PANEL
|
||||
virtual void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj){};
|
||||
#endif
|
||||
#ifdef USE_EVENT
|
||||
virtual void on_event(event::Event *obj, const std::string &event_type){};
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#define USE_CLIMATE
|
||||
#define USE_COVER
|
||||
#define USE_DEEP_SLEEP
|
||||
#define USE_EVENT
|
||||
#define USE_FAN
|
||||
#define USE_GRAPH
|
||||
#define USE_HOMEASSISTANT_TIME
|
||||
|
||||
Reference in New Issue
Block a user