21#ifdef USE_SOCKET_SELECT_SUPPORT
22#include <sys/select.h>
25#ifdef USE_BINARY_SENSOR
55#ifdef USE_DATETIME_DATE
58#ifdef USE_DATETIME_TIME
61#ifdef USE_DATETIME_DATETIME
76#ifdef USE_MEDIA_PLAYER
79#ifdef USE_ALARM_CONTROL_PANEL
95static const uint32_t TEARDOWN_TIMEOUT_REBOOT_MS = 1000;
99 void pre_setup(
const std::string &name,
const std::string &friendly_name,
const char *comment,
100 const char *compilation_time,
bool name_add_mac_suffix) {
103 if (name_add_mac_suffix) {
105 if (friendly_name.empty()) {
128#ifdef USE_BINARY_SENSOR
146#ifdef USE_TEXT_SENSOR
170#ifdef USE_DATETIME_DATE
174#ifdef USE_DATETIME_TIME
178#ifdef USE_DATETIME_DATETIME
198#ifdef USE_MEDIA_PLAYER
202#ifdef USE_ALARM_CONTROL_PANEL
219#ifdef USE_BINARY_SENSOR
231#ifdef USE_TEXT_SENSOR
249#ifdef USE_DATETIME_DATE
252#ifdef USE_DATETIME_TIME
255#ifdef USE_DATETIME_DATETIME
270#ifdef USE_MEDIA_PLAYER
273#ifdef USE_ALARM_CONTROL_PANEL
291 static_assert(std::is_base_of<Component, C>::value,
"Only Component subclasses can be registered");
312 if (!this->
areas_.empty() && this->areas_[0] !=
nullptr) {
313 return this->
areas_[0]->get_name();
346 this->
loop_interval_ = std::min(loop_interval,
static_cast<uint32_t
>(std::numeric_limits<uint16_t>::max()));
373#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
374 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
375 for (auto *obj : this->entities_member##_) { \
376 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
388#ifdef USE_BINARY_SENSOR
393 const std::vector<switch_::Switch *> &get_switches() {
return this->
switches_; }
397 const std::vector<button::Button *> &get_buttons() {
return this->
buttons_; }
401 const std::vector<sensor::Sensor *> &get_sensors() {
return this->
sensors_; }
404#ifdef USE_TEXT_SENSOR
405 const std::vector<text_sensor::TextSensor *> &get_text_sensors() {
return this->
text_sensors_; }
409 const std::vector<fan::Fan *> &get_fans() {
return this->
fans_; }
413 const std::vector<cover::Cover *> &get_covers() {
return this->
covers_; }
417 const std::vector<light::LightState *> &get_lights() {
return this->
lights_; }
421 const std::vector<climate::Climate *> &get_climates() {
return this->
climates_; }
425 const std::vector<number::Number *> &get_numbers() {
return this->
numbers_; }
428#ifdef USE_DATETIME_DATE
429 const std::vector<datetime::DateEntity *> &get_dates() {
return this->
dates_; }
432#ifdef USE_DATETIME_TIME
433 const std::vector<datetime::TimeEntity *> &get_times() {
return this->
times_; }
436#ifdef USE_DATETIME_DATETIME
437 const std::vector<datetime::DateTimeEntity *> &get_datetimes() {
return this->
datetimes_; }
441 const std::vector<text::Text *> &get_texts() {
return this->
texts_; }
445 const std::vector<select::Select *> &get_selects() {
return this->
selects_; }
449 const std::vector<lock::Lock *> &get_locks() {
return this->
locks_; }
453 const std::vector<valve::Valve *> &get_valves() {
return this->
valves_; }
456#ifdef USE_MEDIA_PLAYER
457 const std::vector<media_player::MediaPlayer *> &get_media_players() {
return this->
media_players_; }
461#ifdef USE_ALARM_CONTROL_PANEL
462 const std::vector<alarm_control_panel::AlarmControlPanel *> &get_alarm_control_panels() {
469 const std::vector<event::Event *> &get_events() {
return this->
events_; }
474 const std::vector<update::UpdateEntity *> &get_updates() {
return this->
updates_; }
481#ifdef USE_SOCKET_SELECT_SUPPORT
550#ifdef USE_BINARY_SENSOR
565#ifdef USE_TEXT_SENSOR
583#ifdef USE_DATETIME_DATE
584 std::vector<datetime::DateEntity *>
dates_{};
586#ifdef USE_DATETIME_TIME
587 std::vector<datetime::TimeEntity *>
times_{};
589#ifdef USE_DATETIME_DATETIME
604#ifdef USE_MEDIA_PLAYER
607#ifdef USE_ALARM_CONTROL_PANEL
614#ifdef USE_SOCKET_SELECT_SUPPORT
626#ifdef USE_SOCKET_SELECT_SUPPORT
641#ifdef USE_SOCKET_SELECT_SUPPORT
651extern Application
App;
void enable_pending_loops_()
void reserve_datetime(size_t count)
void setup()
Set up all the registered components. Call this at the end of your setup() function.
uint32_t get_loop_interval() const
std::string get_comment() const
Get the comment of this Application set by pre_setup().
void register_fan(fan::Fan *state)
void register_button(button::Button *button)
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
GET_ENTITY_METHOD(datetime::DateEntity, date, dates) const std
void register_light(light::LightState *light)
void register_binary_sensor(binary_sensor::BinarySensor *binary_sensor)
void reserve_number(size_t count)
void pre_setup(const std::string &name, const std::string &friendly_name, const char *comment, const char *compilation_time, bool name_add_mac_suffix)
GET_ENTITY_METHOD(climate::Climate, climate, climates) const std
void reserve_components(size_t count)
Reserve space for components to avoid memory fragmentation.
GET_ENTITY_METHOD(number::Number, number, numbers) const std
uint16_t looping_components_active_end_
void reserve_button(size_t count)
void set_current_component(Component *component)
void reserve_lock(size_t count)
void reserve_cover(size_t count)
Component * get_current_component()
std::vector< text::Text * > texts_
bool is_socket_ready(int fd) const
Check if there's data available on a socket without blocking This function is thread-safe for reading...
void register_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)
GET_ENTITY_METHOD(button::Button, button, buttons) const std
GET_ENTITY_METHOD(media_player::MediaPlayer, media_player, media_players) const std
void reserve_event(size_t count)
void reserve_text_sensor(size_t count)
std::vector< update::UpdateEntity * > updates_
void register_update(update::UpdateEntity *update)
void register_media_player(media_player::MediaPlayer *media_player)
GET_ENTITY_METHOD(valve::Valve, valve, valves) const std
void reserve_valve(size_t count)
void reserve_alarm_control_panel(size_t count)
GET_ENTITY_METHOD(sensor::Sensor, sensor, sensors) const std
std::vector< media_player::MediaPlayer * > media_players_
std::vector< int > socket_fds_
GET_ENTITY_METHOD(event::Event, event, events) const std
std::vector< event::Event * > events_
void register_number(number::Number *number)
std::vector< sensor::Sensor * > sensors_
void reserve_select(size_t count)
const std::vector< Area * > & get_areas()
GET_ENTITY_METHOD(update::UpdateEntity, update, updates) Scheduler scheduler
std::vector< Component * > components_
void set_loop_interval(uint32_t loop_interval)
Set the target interval with which to run the loop() calls.
void register_climate(climate::Climate *climate)
GET_ENTITY_METHOD(cover::Cover, cover, covers) const std
void reserve_climate(size_t count)
GET_ENTITY_METHOD(switch_::Switch, switch, switches) const std
void register_cover(cover::Cover *cover)
std::vector< text_sensor::TextSensor * > text_sensors_
void reserve_time(size_t count)
void reserve_switch(size_t count)
std::string get_compilation_time() const
void register_area(Area *area)
Component * current_component_
GET_ENTITY_METHOD(binary_sensor::BinarySensor, binary_sensor, binary_sensors) const std
std::vector< datetime::TimeEntity * > times_
std::vector< cover::Cover * > covers_
void register_datetime(datetime::DateTimeEntity *datetime)
std::vector< Device * > devices_
void register_time(datetime::TimeEntity *time)
void enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
void reserve_binary_sensor(size_t count)
std::vector< binary_sensor::BinarySensor * > binary_sensors_
void disable_component_loop_(Component *component)
const char * get_area() const
Get the area of this Application set by pre_setup().
bool is_name_add_mac_suffix_enabled() const
void activate_looping_component_(uint16_t index)
void reserve_fan(size_t count)
GET_ENTITY_METHOD(select::Select, select, selects) const std
std::vector< alarm_control_panel::AlarmControlPanel * > alarm_control_panels_
std::string friendly_name_
void register_switch(switch_::Switch *a_switch)
std::vector< lock::Lock * > locks_
const std::string & get_name() const
Get the name of this Application set by pre_setup().
GET_ENTITY_METHOD(text_sensor::TextSensor, text_sensor, text_sensors) const std
void reserve_sensor(size_t count)
void register_lock(lock::Lock *a_lock)
std::vector< datetime::DateEntity * > dates_
void reserve_media_player(size_t count)
void teardown_components(uint32_t timeout_ms)
Teardown all components with a timeout.
void register_event(event::Event *event)
void register_valve(valve::Valve *valve)
void reserve_light(size_t count)
GET_ENTITY_METHOD(text::Text, text, texts) const std
bool name_add_mac_suffix_
void register_sensor(sensor::Sensor *sensor)
volatile bool has_pending_enable_loop_requests_
std::vector< climate::Climate * > climates_
GET_ENTITY_METHOD(datetime::DateTimeEntity, datetime, datetimes) const std
void reserve_device(size_t count)
const char * compilation_time_
std::vector< Component * > looping_components_
void schedule_dump_config()
void reserve_update(size_t count)
const std::vector< Device * > & get_devices()
void register_text_sensor(text_sensor::TextSensor *sensor)
void run_safe_shutdown_hooks()
const std::vector< binary_sensor::BinarySensor * > & get_binary_sensors()
uint16_t current_loop_index_
std::vector< switch_::Switch * > switches_
std::vector< Area * > areas_
void reserve_text(size_t count)
std::vector< fan::Fan * > fans_
std::vector< number::Number * > numbers_
GET_ENTITY_METHOD(alarm_control_panel::AlarmControlPanel, alarm_control_panel, alarm_control_panels) const std
GET_ENTITY_METHOD(fan::Fan, fan, fans) const std
void register_select(select::Select *select)
void feed_wdt(uint32_t time=0)
C * register_component(C *c)
Register the component in this Application instance.
std::vector< valve::Valve * > valves_
void loop()
Make a loop iteration. Call this in your loop() function.
GET_ENTITY_METHOD(lock::Lock, lock, locks) const std
void register_text(text::Text *text)
std::vector< button::Button * > buttons_
std::vector< light::LightState * > lights_
void reserve_date(size_t count)
void reserve_area(size_t count)
void register_device(Device *device)
void unregister_socket_fd(int fd)
std::vector< select::Select * > selects_
std::vector< datetime::DateTimeEntity * > datetimes_
GET_ENTITY_METHOD(light::LightState, light, lights) const std
bool register_socket_fd(int fd)
Register/unregister a socket file descriptor to be monitored for read events.
void calculate_looping_components_()
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void yield_with_select_(uint32_t delay_ms)
Perform a delay while also monitoring socket file descriptors for readiness.
uint8_t get_app_state() const
void run_powerdown_hooks()
GET_ENTITY_METHOD(datetime::TimeEntity, time, times) const std
void register_component_(Component *comp)
void register_date(datetime::DateEntity *date)
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Base class for all cover devices.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Base class for all locks.
Base-class for all numbers.
Base-class for all selects.
Base-class for all sensors.
Base class for all switches.
Base-class for all text inputs.
Base class for all valve devices.
Providing packet encoding functions for exchanging data with a remote host.
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Application App
Global storage of Application pointer - only one Application can exist.