33static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
37static constexpr size_t MAX_INITIAL_PER_BATCH = 24;
41#if defined(USE_ESP32) || defined(USE_HOST)
42static constexpr size_t MAX_PACKETS_PER_BATCH = 64;
44static constexpr size_t MAX_PACKETS_PER_BATCH = 32;
61#ifdef USE_BINARY_SENSOR
98#ifdef USE_DATETIME_DATE
102#ifdef USE_DATETIME_TIME
106#ifdef USE_DATETIME_DATETIME
129#ifdef USE_MEDIA_PLAYER
134#ifdef USE_API_HOMEASSISTANT_SERVICES
141#ifdef USE_BLUETOOTH_PROXY
156#ifdef USE_HOMEASSISTANT_TIME
163#ifdef USE_VOICE_ASSISTANT
174#ifdef USE_ALARM_CONTROL_PANEL
193#ifdef USE_API_HOMEASSISTANT_STATES
196#ifdef USE_HOMEASSISTANT_TIME
214#ifdef USE_API_HOMEASSISTANT_SERVICES
219#ifdef USE_API_HOMEASSISTANT_STATES
223#ifdef USE_API_SERVICES
246#ifdef USE_API_PASSWORD
254 uint8_t header_padding = this->
helper_->frame_header_padding();
262 shared_buf.reserve(reserve_size + header_padding + this->
helper_->frame_footer_size());
264 shared_buf.resize(header_padding);
265 return {&shared_buf};
273 if (is_first_message) {
277 size_t current_size = shared_buf.size();
282 size_t padding_to_add = is_first_message
283 ? this->
helper_->frame_header_padding()
284 : this->
helper_->frame_header_padding() + this->
helper_->frame_footer_size();
287 shared_buf.reserve(current_size + padding_to_add + message_size);
290 shared_buf.resize(current_size + padding_to_add);
292 return {&shared_buf};
308#ifdef USE_API_HOMEASSISTANT_STATES
314 uint32_t remaining_size,
bool is_single);
318 APIConnection *conn, uint32_t remaining_size,
bool is_single) {
328 APIConnection *conn, uint32_t remaining_size,
bool is_single) {
340#ifdef USE_ENTITY_ICON
351#ifdef USE_VOICE_ASSISTANT
359 while (!iterator.completed() && (this->deferred_batch_.size() - initial_size) < MAX_INITIAL_PER_BATCH) {
370#ifdef USE_BINARY_SENSOR
402#ifdef USE_TEXT_SENSOR
420#ifdef USE_DATETIME_DATE
424#ifdef USE_DATETIME_TIME
428#ifdef USE_DATETIME_DATETIME
457#ifdef USE_MEDIA_PLAYER
463#ifdef USE_ALARM_CONTROL_PANEL
471 uint32_t remaining_size,
bool is_single);
516#ifdef USE_API_HOMEASSISTANT_STATES
529 explicit MessageCreator(
const std::string &str_value) { data_.string_ptr =
new std::string(str_value); }
542 if (
this != &other) {
546 other.data_.function_ptr =
nullptr;
553 uint8_t message_type)
const;
559 delete data_.string_ptr;
560 data_.string_ptr =
nullptr;
568 std::string *string_ptr;
590 void cleanup_items_(
size_t count) {
591 for (
size_t i = 0; i < count; i++) {
592 items[i].creator.cleanup(
items[i].message_type);
614 cleanup_items_(
items.size());
621 cleanup_items_(count);
657#ifdef HAS_PROTO_MESSAGE_DUMP
684 this->deferred_batch_.
clear();
688#ifdef HAS_PROTO_MESSAGE_DUMP
704 uint8_t estimated_size) {
716 this->helper_->can_write_without_blocking()) {
720#ifdef HAS_PROTO_MESSAGE_DUMP
736 this->deferred_batch_.
add_item(entity, std::move(creator), message_type, estimated_size);
742 uint8_t estimated_size) {
748 uint8_t estimated_size) {
void schedule_dump_config()
void begin(bool include_internal=false)
bool has_own_name() const
uint32_t get_object_id_hash()
const StringRef & get_name() const
StringRef get_icon_ref() const
uint32_t get_device_id() const
bool is_disabled_by_default() const
std::string get_object_id() const
EntityCategory get_entity_category() const
StringRef is a reference to a string owned by something else.
MessageCreator(MessageCreator &&other) noexcept
void cleanup(uint8_t message_type)
uint16_t operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single, uint8_t message_type) const
MessageCreator(const MessageCreator &other)=delete
MessageCreator(const std::string &str_value)
MessageCreator & operator=(const MessageCreator &other)=delete
MessageCreator(MessageCreatorPtr ptr)
MessageCreator & operator=(MessageCreator &&other) noexcept
static uint16_t try_send_binary_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
struct esphome::api::APIConnection::APIFlags flags_
bool send_ping_response(const PingRequest &msg) override
bool send_message_smart_(EntityBase *entity, MessageCreatorPtr creator, uint8_t message_type, uint8_t estimated_size)
void subscribe_voice_assistant(const SubscribeVoiceAssistantRequest &msg) override
static uint16_t try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void select_command(const SelectCommandRequest &msg) override
static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint8_t message_type, APIConnection *conn, uint32_t remaining_size, bool is_single)
void bluetooth_gatt_write(const BluetoothGATTWriteRequest &msg) override
bool send_get_time_response(const GetTimeRequest &msg) override
static uint16_t try_send_text_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_media_player_state(media_player::MediaPlayer *media_player)
static uint16_t try_send_datetime_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_time_state(datetime::TimeEntity *time)
static uint16_t try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void time_command(const TimeCommandRequest &msg) override
static uint16_t try_send_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
uint16_t client_api_version_major_
void update_command(const UpdateCommandRequest &msg) override
void on_ping_response(const PingResponse &value) override
static uint16_t try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_connect_response(const ConnectRequest &msg) override
void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg) override
bool send_subscribe_bluetooth_connections_free_response(const SubscribeBluetoothConnectionsFreeRequest &msg) override
ProtoWriteBuffer create_buffer(uint32_t reserve_size) override
static uint16_t try_send_lock_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_time_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor)
bool send_fan_state(fan::Fan *fan)
static uint16_t try_send_switch_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void bluetooth_scanner_set_mode(const BluetoothScannerSetModeRequest &msg) override
static uint16_t try_send_media_player_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void on_voice_assistant_response(const VoiceAssistantResponse &msg) override
bool check_voice_assistant_api_connection_() const
void log_proto_message_(EntityBase *entity, const MessageCreator &creator, uint8_t message_type)
static uint16_t try_send_number_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void log_socket_operation_failed_(APIError err)
InitialStateIterator initial_state_iterator_
void subscribe_logs(const SubscribeLogsRequest &msg) override
std::unique_ptr< APIFrameHelper > helper_
uint16_t client_api_version_minor_
void date_command(const DateCommandRequest &msg) override
bool schedule_message_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type, uint8_t estimated_size)
void set_camera_state(std::shared_ptr< camera::CameraImage > image)
static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
uint32_t get_batch_delay_ms_() const
void send_event(event::Event *event, const std::string &event_type)
bool send_sensor_state(sensor::Sensor *sensor)
std::string get_client_combined_info() const
static constexpr size_t MAX_BATCH_PACKET_SIZE
void log_batch_item_(const DeferredBatch::BatchItem &item)
void datetime_command(const DateTimeCommandRequest &msg) override
uint16_t(*)(EntityBase *, APIConnection *, uint32_t remaining_size, bool is_single) MessageCreatorPtr
void voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg) override
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor)
void process_iterator_batch_(Iterator &iterator)
static uint16_t try_send_climate_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_valve_state(valve::Valve *valve)
static uint16_t try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_select_state(select::Select *select)
bool send_switch_state(switch_::Switch *a_switch)
void bluetooth_gatt_write_descriptor(const BluetoothGATTWriteDescriptorRequest &msg) override
void bluetooth_gatt_read(const BluetoothGATTReadRequest &msg) override
static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void text_command(const TextCommandRequest &msg) override
void process_state_subscriptions_()
bool send_lock_state(lock::Lock *a_lock)
bool send_hello_response(const HelloRequest &msg) override
static uint16_t try_send_button_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_list_info_done(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
ProtoWriteBuffer allocate_batch_message_buffer(uint16_t size)
static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_valve_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void log_warning_(const char *message, APIError err)
bool send_update_state(update::UpdateEntity *update)
void on_voice_assistant_audio(const VoiceAssistantAudio &msg) override
static uint16_t fill_and_encode_entity_state(EntityBase *entity, StateResponseProtoMessage &msg, uint8_t message_type, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool schedule_message_(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)
static uint16_t try_send_light_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void on_fatal_error() override
static uint16_t try_send_event_response(event::Event *event, const std::string &event_type, APIConnection *conn, uint32_t remaining_size, bool is_single)
void alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) override
static uint16_t fill_and_encode_entity_info(EntityBase *entity, InfoResponseProtoMessage &msg, uint8_t message_type, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_device_info_response(const DeviceInfoRequest &msg) override
bool schedule_message_front_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type, uint8_t estimated_size)
void unsubscribe_bluetooth_le_advertisements(const UnsubscribeBluetoothLEAdvertisementsRequest &msg) override
void number_command(const NumberCommandRequest &msg) override
void list_entities(const ListEntitiesRequest &msg) override
std::unique_ptr< camera::CameraImageReader > image_reader_
void media_player_command(const MediaPlayerCommandRequest &msg) override
void bluetooth_gatt_notify(const BluetoothGATTNotifyRequest &msg) override
void subscribe_homeassistant_services(const SubscribeHomeassistantServicesRequest &msg) override
APIConnection(std::unique_ptr< socket::Socket > socket, APIServer *parent)
void subscribe_states(const SubscribeStatesRequest &msg) override
bool is_authenticated() override
ProtoWriteBuffer allocate_single_message_buffer(uint16_t size)
void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) override
bool send_date_state(datetime::DateEntity *date)
static uint16_t try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void bluetooth_gatt_read_descriptor(const BluetoothGATTReadDescriptorRequest &msg) override
void climate_command(const ClimateCommandRequest &msg) override
static uint16_t try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void bluetooth_gatt_get_services(const BluetoothGATTGetServicesRequest &msg) override
bool is_connection_setup() override
bool send_number_state(number::Number *number)
void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg) override
void fan_command(const FanCommandRequest &msg) override
void on_no_setup_connection() override
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override
static uint16_t try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_noise_encryption_set_key_response(const NoiseEncryptionSetKeyRequest &msg) override
bool send_light_state(light::LightState *light)
bool send_voice_assistant_get_configuration_response(const VoiceAssistantConfigurationRequest &msg) override
DeferredBatch deferred_batch_
void valve_command(const ValveCommandRequest &msg) override
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override
void cover_command(const CoverCommandRequest &msg) override
uint8_t get_log_subscription_level() const
void on_get_time_response(const GetTimeResponse &value) override
static uint16_t try_send_select_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) override
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
void on_disconnect_response(const DisconnectResponse &value) override
bool send_datetime_state(datetime::DateTimeEntity *datetime)
ListEntitiesIterator list_entities_iterator_
static uint16_t try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_disconnect_response(const DisconnectRequest &msg) override
bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)
static uint16_t try_send_text_sensor_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
void complete_authentication_()
void camera_image(const CameraImageRequest &msg) override
void light_command(const LightCommandRequest &msg) override
bool send_text_state(text::Text *text)
void switch_command(const SwitchCommandRequest &msg) override
static uint16_t try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_date_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len)
static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
bool send_list_info_done()
bool client_supports_api_version(uint16_t major, uint16_t minor) const
void lock_command(const LockCommandRequest &msg) override
void bluetooth_device_request(const BluetoothDeviceRequest &msg) override
bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override
void execute_service(const ExecuteServiceRequest &msg) override
bool send_climate_state(climate::Climate *climate)
bool try_to_clear_buffer(bool log_out_of_space)
bool send_cover_state(cover::Cover *cover)
void button_command(const ButtonCommandRequest &msg) override
static uint16_t try_send_alarm_control_panel_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_cover_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single)
ProtoWriteBuffer prepare_message_buffer(uint16_t message_size, bool is_first_message)
void on_unauthenticated_access() override
bool send_message(const ProtoMessage &msg, uint8_t message_type)
std::vector< uint8_t > & get_shared_buffer_ref()
static constexpr uint8_t MESSAGE_TYPE
static constexpr uint8_t MESSAGE_TYPE
static constexpr uint8_t MESSAGE_TYPE
void set_object_id(const StringRef &ref)
enums::EntityCategory entity_category
void set_icon(const StringRef &ref)
void set_name(const StringRef &ref)
static constexpr uint8_t MESSAGE_TYPE
static constexpr uint8_t ESTIMATED_SIZE
static constexpr uint8_t MESSAGE_TYPE
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.
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t batch_first_message
uint8_t state_subscription
uint8_t should_try_send_immediately
uint8_t service_call_subscription
BatchItem(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)
void remove_front(size_t count)
std::vector< BatchItem > items
uint32_t batch_start_time
const BatchItem & operator[](size_t index) const
void add_item(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)
void add_item_front(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)
std::string get_combined_info() const