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 this->
name_ = name +
"-" + mac_suffix;
106 this->
friendly_name_ = friendly_name.empty() ?
"" : friendly_name +
" " + mac_suffix;
125#ifdef USE_BINARY_SENSOR
143#ifdef USE_TEXT_SENSOR
167#ifdef USE_DATETIME_DATE
171#ifdef USE_DATETIME_TIME
175#ifdef USE_DATETIME_DATETIME
195#ifdef USE_MEDIA_PLAYER
199#ifdef USE_ALARM_CONTROL_PANEL
217 static_assert(std::is_base_of<Component, C>::value,
"Only Component subclasses can be registered");
238 if (!this->
areas_.empty() && this->areas_[0] !=
nullptr) {
239 return this->
areas_[0]->get_name();
272 this->
loop_interval_ = std::min(loop_interval,
static_cast<uint32_t
>(std::numeric_limits<uint16_t>::max()));
299#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
300 entity_type *get_##entity_name##_by_key(uint32_t key, uint32_t device_id, bool include_internal = false) { \
301 for (auto *obj : this->entities_member##_) { \
302 if (obj->get_object_id_hash() == key && obj->get_device_id() == device_id && \
303 (include_internal || !obj->is_internal())) \
310#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
311 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
312 for (auto *obj : this->entities_member##_) { \
313 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
322#ifdef USE_BINARY_SENSOR
327 auto &get_switches()
const {
return this->
switches_; }
331 auto &get_buttons()
const {
return this->
buttons_; }
335 auto &get_sensors()
const {
return this->
sensors_; }
338#ifdef USE_TEXT_SENSOR
339 auto &get_text_sensors()
const {
return this->
text_sensors_; }
343 auto &get_fans()
const {
return this->
fans_; }
347 auto &get_covers()
const {
return this->
covers_; }
351 auto &get_lights()
const {
return this->
lights_; }
355 auto &get_climates()
const {
return this->
climates_; }
359 auto &get_numbers()
const {
return this->
numbers_; }
362#ifdef USE_DATETIME_DATE
363 auto &get_dates()
const {
return this->
dates_; }
366#ifdef USE_DATETIME_TIME
367 auto &get_times()
const {
return this->
times_; }
370#ifdef USE_DATETIME_DATETIME
371 auto &get_datetimes()
const {
return this->
datetimes_; }
375 auto &get_texts()
const {
return this->
texts_; }
379 auto &get_selects()
const {
return this->
selects_; }
383 auto &get_locks()
const {
return this->
locks_; }
387 auto &get_valves()
const {
return this->
valves_; }
390#ifdef USE_MEDIA_PLAYER
395#ifdef USE_ALARM_CONTROL_PANEL
401 auto &get_events()
const {
return this->
events_; }
406 auto &get_updates()
const {
return this->
updates_; }
413#ifdef USE_SOCKET_SELECT_SUPPORT
472#ifdef USE_SOCKET_SELECT_SUPPORT
487#ifdef USE_SOCKET_SELECT_SUPPORT
502#ifdef USE_SOCKET_SELECT_SUPPORT
506#ifdef USE_SOCKET_SELECT_SUPPORT
521#ifdef USE_BINARY_SENSOR
536#ifdef USE_TEXT_SENSOR
554#ifdef USE_DATETIME_DATE
557#ifdef USE_DATETIME_TIME
560#ifdef USE_DATETIME_DATETIME
575#ifdef USE_MEDIA_PLAYER
578#ifdef USE_ALARM_CONTROL_PANEL
588extern Application
App;
StaticVector< switch_::Switch *, ESPHOME_ENTITY_SWITCH_COUNT > switches_
void enable_pending_loops_()
StaticVector< light::LightState *, ESPHOME_ENTITY_LIGHT_COUNT > lights_
void setup()
Set up all the registered components. Call this at the end of your setup() function.
uint32_t get_loop_interval() const
StaticVector< valve::Valve *, ESPHOME_ENTITY_VALVE_COUNT > valves_
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)
GET_ENTITY_METHOD(select::Select, select, selects) auto &get_locks() const
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
StaticVector< datetime::TimeEntity *, ESPHOME_ENTITY_TIME_COUNT > times_
GET_ENTITY_METHOD(fan::Fan, fan, fans) auto &get_covers() const
void register_light(light::LightState *light)
void register_binary_sensor(binary_sensor::BinarySensor *binary_sensor)
void pre_setup(const std::string &name, const std::string &friendly_name, const char *comment, const char *compilation_time, bool name_add_mac_suffix)
uint16_t looping_components_active_end_
void set_current_component(Component *component)
Component * get_current_component()
StaticVector< binary_sensor::BinarySensor *, ESPHOME_ENTITY_BINARY_SENSOR_COUNT > binary_sensors_
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(datetime::TimeEntity, time, times) auto &get_datetimes() const
StaticVector< select::Select *, ESPHOME_ENTITY_SELECT_COUNT > selects_
GET_ENTITY_METHOD(climate::Climate, climate, climates) auto &get_numbers() const
void register_update(update::UpdateEntity *update)
StaticVector< Area *, ESPHOME_AREA_COUNT > areas_
void register_media_player(media_player::MediaPlayer *media_player)
GET_ENTITY_METHOD(valve::Valve, valve, valves) auto &get_media_players() const
StaticVector< fan::Fan *, ESPHOME_ENTITY_FAN_COUNT > fans_
std::vector< int > socket_fds_
void register_number(number::Number *number)
GET_ENTITY_METHOD(update::UpdateEntity, update, updates) Scheduler scheduler
void set_loop_interval(uint32_t loop_interval)
Set the target interval with which to run the loop() calls.
StaticVector< Component *, ESPHOME_COMPONENT_COUNT > components_
void register_climate(climate::Climate *climate)
StaticVector< datetime::DateEntity *, ESPHOME_ENTITY_DATE_COUNT > dates_
void register_cover(cover::Cover *cover)
StaticVector< media_player::MediaPlayer *, ESPHOME_ENTITY_MEDIA_PLAYER_COUNT > media_players_
GET_ENTITY_METHOD(text::Text, text, texts) auto &get_selects() const
std::string get_compilation_time() const
StaticVector< update::UpdateEntity *, ESPHOME_ENTITY_UPDATE_COUNT > updates_
void register_area(Area *area)
Component * current_component_
GET_ENTITY_METHOD(datetime::DateEntity, date, dates) auto &get_times() const
void register_datetime(datetime::DateTimeEntity *datetime)
GET_ENTITY_METHOD(light::LightState, light, lights) auto &get_climates() const
void register_time(datetime::TimeEntity *time)
void enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
GET_ENTITY_METHOD(media_player::MediaPlayer, media_player, media_players) auto &get_alarm_control_panels() const
StaticVector< climate::Climate *, ESPHOME_ENTITY_CLIMATE_COUNT > climates_
GET_ENTITY_METHOD(text_sensor::TextSensor, text_sensor, text_sensors) auto &get_fans() const
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)
const auto & get_devices()
StaticVector< cover::Cover *, ESPHOME_ENTITY_COVER_COUNT > covers_
StaticVector< lock::Lock *, ESPHOME_ENTITY_LOCK_COUNT > locks_
std::string friendly_name_
void register_switch(switch_::Switch *a_switch)
const std::string & get_name() const
Get the name of this Application set by pre_setup().
void register_lock(lock::Lock *a_lock)
StaticVector< event::Event *, ESPHOME_ENTITY_EVENT_COUNT > events_
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)
bool name_add_mac_suffix_
void register_sensor(sensor::Sensor *sensor)
StaticVector< alarm_control_panel::AlarmControlPanel *, ESPHOME_ENTITY_ALARM_CONTROL_PANEL_COUNT > alarm_control_panels_
volatile bool has_pending_enable_loop_requests_
GET_ENTITY_METHOD(datetime::DateTimeEntity, datetime, datetimes) auto &get_texts() const
GET_ENTITY_METHOD(sensor::Sensor, sensor, sensors) auto &get_text_sensors() const
StaticVector< text_sensor::TextSensor *, ESPHOME_ENTITY_TEXT_SENSOR_COUNT > text_sensors_
const char * compilation_time_
StaticVector< datetime::DateTimeEntity *, ESPHOME_ENTITY_DATETIME_COUNT > datetimes_
std::vector< Component * > looping_components_
GET_ENTITY_METHOD(switch_::Switch, switch, switches) auto &get_buttons() const
void schedule_dump_config()
GET_ENTITY_METHOD(binary_sensor::BinarySensor, binary_sensor, binary_sensors) auto &get_switches() const
void register_text_sensor(text_sensor::TextSensor *sensor)
void run_safe_shutdown_hooks()
uint16_t current_loop_index_
StaticVector< button::Button *, ESPHOME_ENTITY_BUTTON_COUNT > buttons_
GET_ENTITY_METHOD(alarm_control_panel::AlarmControlPanel, alarm_control_panel, alarm_control_panels) auto &get_events() const
StaticVector< text::Text *, ESPHOME_ENTITY_TEXT_COUNT > texts_
void register_select(select::Select *select)
void feed_wdt(uint32_t time=0)
C * register_component(C *c)
Reserve space for components to avoid memory fragmentation.
void before_loop_tasks_(uint32_t loop_start_time)
void loop()
Make a loop iteration. Call this in your loop() function.
void register_text(text::Text *text)
void register_device(Device *device)
void unregister_socket_fd(int fd)
GET_ENTITY_METHOD(event::Event, event, events) auto &get_updates() const
GET_ENTITY_METHOD(cover::Cover, cover, covers) auto &get_lights() const
bool register_socket_fd(int fd)
Register/unregister a socket file descriptor to be monitored for read events.
StaticVector< Device *, ESPHOME_DEVICE_COUNT > devices_
StaticVector< number::Number *, ESPHOME_ENTITY_NUMBER_COUNT > numbers_
void calculate_looping_components_()
auto & get_binary_sensors() const
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
GET_ENTITY_METHOD(number::Number, number, numbers) auto &get_dates() const
GET_ENTITY_METHOD(lock::Lock, lock, locks) auto &get_valves() const
void run_powerdown_hooks()
StaticVector< sensor::Sensor *, ESPHOME_ENTITY_SENSOR_COUNT > sensors_
GET_ENTITY_METHOD(button::Button, button, buttons) auto &get_sensors() const
void register_component_(Component *comp)
void register_date(datetime::DateEntity *date)
Minimal static vector - saves memory by avoiding std::vector overhead.
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.