#pragma once #include "esphome/core/component.h" #include namespace esphome { class Component; class Scheduler { public: void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function &&func); bool cancel_timeout(Component *component, const std::string &name); void set_interval(Component *component, const std::string &name, uint32_t interval, std::function &&func); bool cancel_interval(Component *component, const std::string &name); optional next_schedule_in(); void call(); void process_to_add(); protected: struct SchedulerItem { Component *component; std::string name; enum Type { TIMEOUT, INTERVAL } type; union { uint32_t interval; uint32_t timeout; }; uint32_t last_execution; std::function f; bool remove; bool operator<(const SchedulerItem &other) const; }; void cleanup_(); bool peek_(); void pop_raw_(); void push_(SchedulerItem *item); bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type); std::vector items_; std::vector to_add_; }; } // namespace esphome