ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
esphome::entity_transport::EntityTransport Class Reference

Sends the states of local sensors and binary sensors over a packet interface, and publishes the states received from other devices to remote entities. More...

#include <entity_transport.h>

Inheritance diagram for esphome::entity_transport::EntityTransport:
esphome::Component

Data Structures

struct  LocalEntity
 
struct  RemoteEntity
 

Public Member Functions

 EntityTransport (packet_interface::PacketInterface *interface, uint8_t *buffer, size_t buffer_size)
 
void setup () override
 
void loop () override
 
void dump_config () override
 
void set_update_interval (uint32_t interval_ms)
 0 chooses the default for the interface's layers; SCHEDULER_DONT_RUN never resends.
 
void set_remote_timeout (uint32_t timeout_ms)
 Remote entities lose their state when none arrives for this long; 0 keeps the last state.
 
void set_entity_count (size_t count)
 
void add_sensor (const char *id, sensor::Sensor *sensor)
 
void add_binary_sensor (const char *id, binary_sensor::BinarySensor *sensor)
 
void add_remote_sensor (const char *provider, const char *remote_id, sensor::Sensor *sensor)
 
void add_remote_binary_sensor (const char *provider, const char *remote_id, binary_sensor::BinarySensor *sensor)
 
- Public Member Functions inherited from esphome::Component
virtual float get_setup_priority () const
 priority of setup().
 
float get_actual_setup_priority () const
 
void set_setup_priority (float priority)
 
void call ()
 
virtual void on_shutdown ()
 
virtual void on_safe_shutdown ()
 
virtual bool teardown ()
 Called during teardown to allow component to gracefully finish operations.
 
virtual void on_powerdown ()
 Called last before a reboot or deep sleep to power down hardware.
 
uint8_t get_component_state () const
 
void reset_to_construction_state ()
 Reset this component back to the construction state to allow setup to run again.
 
bool is_in_loop_state () const
 Check if this component has completed setup and is in the loop state.
 
bool is_idle () const
 Check if this component is idle.
 
void mark_failed ()
 Mark this component as failed.
 
void mark_failed (const LogString *message)
 
bool ESPHOME_ALWAYS_INLINE setup_check (bool ok)
 Marks the component failed with message (default "Communication failed") unless ok; returns ok.
 
bool ESPHOME_ALWAYS_INLINE setup_check (bool ok, const LogString *message)
 
void disable_loop ()
 Disable this component's loop.
 
void enable_loop ()
 Enable this component's loop.
 
void enable_loop_soon_any_context ()
 Thread and ISR-safe version of enable_loop() that can be called from any context.
 
bool is_failed () const
 
bool is_ready () const
 
virtual bool can_proceed ()
 
bool status_has_warning () const
 
bool status_has_error () const
 
void status_set_warning ()
 
void status_set_warning (const char *message)
 
void status_set_warning (const LogString *message)
 
bool status_comm_result (bool ok)
 Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
 
bool status_comm_result (bool ok, const LogString *message)
 
bool status_comm_check (bool ok)
 Sets the warning ("Communication failed") when not ok but never clears it; returns ok.
 
void status_set_error ()
 
void status_set_error (const LogString *message)
 
void status_clear_warning ()
 
void status_clear_error ()
 
 ESPDEPRECATED ("Use status_momentary_warning() without a name. Removed in 2027.5.0", "2026.11.0") void status_momentary_warning(const char *name
 
void status_momentary_warning (uint32_t length=5000)
 Set warning status flag and clear it after length ms.
 
void status_momentary_warning (const LogString *message, uint32_t length=5000)
 
 ESPDEPRECATED ("Use status_momentary_error() without a name. Removed in 2027.5.0", "2026.11.0") void status_momentary_error(const char *name
 
void status_momentary_error (uint32_t length=5000)
 Set error status flag and clear it after length ms.
 
void status_momentary_error (const LogString *message, uint32_t length=5000)
 
bool has_overridden_loop () const
 
const LogString * get_component_log_str () const ESPHOME_ALWAYS_INLINE
 Get the integration where this component was declared as a LogString for logging.
 
bool should_warn_of_blocking (uint32_t blocking_time, uint32_t &threshold_ms_out)
 

Protected Types

enum class  DropReason : uint8_t { DROP_REASON_TOO_SHORT , DROP_REASON_FORMAT_VERSION , DROP_REASON_MALFORMED_DICTIONARY , DROP_REASON_MALFORMED_DATA }
 The kinds of dropped packet, each warned about at most once a minute, so a steady stream of one kind does not hide another. More...
 

Protected Member Functions

void send_all_ ()
 
void mark_updated_ (LocalEntity &entity)
 
bool send_dictionary_ ()
 
bool send_updated_ ()
 
size_t write_record_ (const LocalEntity &entity, uint8_t *record) const
 
bool flush_ (size_t length, size_t first, size_t last)
 
size_t write_header_ (uint8_t type) const
 
void receive_ (const packet_interface::PacketBuffer &data)
 
void receive_dictionary_ (std::span< const uint8_t > packet, uint32_t dictionary_id)
 
void receive_data_ (std::span< const uint8_t > packet, uint32_t dictionary_id)
 
void learn_ (const char *provider, size_t provider_length, uint8_t type, uint8_t number, const char *id, size_t id_length, uint32_t dictionary_id)
 
void request_dictionary_ (uint32_t dictionary_id)
 
bool dictionary_seen_ (uint32_t dictionary_id) const
 
void forget_unnamed_ (const char *provider, size_t provider_length, uint32_t dictionary_id, bool first_time)
 Clears each remote entity of provider that its complete dictionary does not name, warning if the entity was known or the dictionary is first_time seen, so a resent dictionary does not repeat the warning.
 
bool warning_due_ (DropReason reason)
 
void sending_refused_ ()
 
void on_session_ (bool connected)
 
void clear_remote_states_ (bool timed_out_only)
 
- Protected Member Functions inherited from esphome::Component
friend void::setup ()
 
friend void::original_setup ()
 
void set_component_source_ (uint8_t index)
 Set where this component was loaded from for some debug messages.
 
virtual void call_setup ()
 
void call_dump_config_ ()
 
void enable_loop_slow_path_ ()
 
void set_component_state_ (uint8_t state)
 Helper to set component state (clears state bits and sets new state)
 
bool set_status_flag_ (uint8_t flag)
 Helper to set a status LED flag on both this component and the app.
 
bool set_error_flag_ (const LogString *message)
 Set the error flag and log it; true when it was not set before.
 
void set_interval (const char *name, uint32_t interval, std::function< void()> &&f)
 Set an interval function with a const char* name.
 
void set_interval (uint32_t id, uint32_t interval, std::function< void()> &&f)
 Set an interval function with a numeric ID (zero heap allocation).
 
void set_interval (InternalSchedulerID id, uint32_t interval, std::function< void()> &&f)
 
void set_interval (uint32_t interval, std::function< void()> &&f)
 
bool cancel_interval (const char *name)
 Cancel an interval function.
 
bool cancel_interval (uint32_t id)
 
bool cancel_interval (InternalSchedulerID id)
 
void set_timeout (const char *name, uint32_t timeout, std::function< void()> &&f)
 Set a timeout function with a const char* name.
 
void set_timeout (uint32_t id, uint32_t timeout, std::function< void()> &&f)
 Set a timeout function with a numeric ID (zero heap allocation).
 
void set_timeout (InternalSchedulerID id, uint32_t timeout, std::function< void()> &&f)
 
void set_timeout (uint32_t timeout, std::function< void()> &&f)
 
bool cancel_timeout (const char *name)
 Cancel a timeout function.
 
bool cancel_timeout (uint32_t id)
 
bool cancel_timeout (InternalSchedulerID id)
 
void defer (const char *name, std::function< void()> &&f)
 Defer a callback to the next loop() call with a const char* name.
 
void defer (std::function< void()> &&f)
 Defer a callback to the next loop() call.
 
void defer (uint32_t id, std::function< void()> &&f)
 Defer a callback with a numeric ID (zero heap allocation)
 
bool cancel_defer (const char *name)
 Cancel a defer callback using the specified name, name must not be empty.
 
bool cancel_defer (uint32_t id)
 
void status_clear_warning_slow_path_ ()
 
void status_comm_check_slow_path_ ()
 
void status_clear_error_slow_path_ ()
 

Protected Attributes

packet_interface::PacketInterface * interface_
 
FixedVector< LocalEntity > entities_
 
StaticVector< RemoteEntity< sensor::Sensor >, ENTITY_TRANSPORT_REMOTE_SENSOR_COUNT > remote_sensors_
 
StaticVector< RemoteEntity< binary_sensor::BinarySensor >, ENTITY_TRANSPORT_REMOTE_BINARY_SENSOR_COUNT > remote_binary_sensors_
 
uint8_t * buffer_
 
size_t max_packet_size_
 
size_t dictionary_next_ {0}
 
uint32_t update_interval_ms_ {0}
 
uint32_t remote_timeout_ms_ {0}
 
uint32_t dictionary_id_ {0}
 
uint32_t last_request_ms_ {0}
 
uint32_t refused_since_ms_ {0}
 
uint32_t rx_dictionary_id_ {0}
 
std::array< uint32_t, 8 > seen_dictionaries_ {}
 
std::array< uint32_t, DROP_REASON_COUNT > last_warning_ms_ {}
 
uint8_t seen_next_ {0}
 
uint8_t rx_next_part_ {0}
 
uint8_t dictionary_part_ {0}
 
uint8_t sensor_count_ {0}
 
uint8_t binary_sensor_count_ {0}
 
uint8_t warned_ {0}
 
bool dictionary_pending_: 1 {true}
 
bool request_sent_: 1 {false}
 
bool refusing_: 1 {false}
 
- Protected Attributes inherited from esphome::Component
uint8_t component_source_index_ {0}
 Index into component source PROGMEM lookup table (0 = not set)
 
uint8_t warn_if_blocking_over_ {WARN_IF_BLOCKING_OVER_CS}
 Warn threshold in centiseconds (max 2550ms)
 
uint8_t component_state_ {0x00}
 State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION, 0x01=SETUP, 0x02=LOOP, 0x03=FAILED, 0x04=LOOP_DONE) Bit 3: STATUS_LED_WARNING Bit 4: STATUS_LED_ERROR Bit 5: Has overridden loop() (set at registration time) Bits 6-7: Unused - reserved for future expansion.
 
volatile bool pending_enable_loop_ {false}
 ISR-safe flag for enable_loop_soon_any_context.
 
ComponentRuntimeStats runtime_stats_
 

Static Protected Attributes

static constexpr size_t DROP_REASON_COUNT = 4
 

Additional Inherited Members

- Data Fields inherited from esphome::Component
uint32_t length = 5000)
 

Detailed Description

Sends the states of local sensors and binary sensors over a packet interface, and publishes the states received from other devices to remote entities.

Each device numbers its sensors and its binary sensors from 0, and sends a dictionary that gives the id for each number. Data packets then refer to an entity by its number. Every packet carries the sender's dictionary id, a hash of its device name and dictionary, so that a receiver knows which device sent it and which dictionary applies. All values are little endian.

Dictionary packet:

  • type: 1 byte, PACKET_DICTIONARY;
  • dictionary id: 4 bytes;
  • part: 1 byte, the packet's index in the dictionary in bits 0-6, counting from 0 and wrapping, and bit 7 set on the last packet;
  • sender's device name: length (1 byte), then the name;
  • one or more sections, each: entity type (1 byte), first number (1 byte), count (1 byte), then for each entity its id: length (1 byte), then the id.

Data packet:

  • type: 1 byte, PACKET_DATA;
  • dictionary id: 4 bytes;
  • records, to the end, each a 16-bit tag, then its payload. The tag holds the entity's number in bits 0-7, its type in bits 8-12, and the payload form in bits 13-14: none, with the state in bit 15 (binary sensors); 4 bytes (a sensor's float); a length byte, then that many bytes; or no state, with no payload. The form gives the record's size, so a receiver skips records of types it does not know.

Every packet is checked in full before anything in it is used, so a damaged or forged packet changes no state.

Dictionary request packet, sent by a device that receives data with a dictionary id it has not received in full, with every part in order:

  • type: 1 byte, PACKET_REQUEST;
  • the dictionary id wanted: 4 bytes.

Changed states are sent from loop(). The dictionary and all states are sent at boot, when a session layer reports the peer connected, when the dictionary is requested, and every update interval. Unless the interface has both a session and a reliability layer, the update interval defaults to DEFAULT_UPDATE_INTERVAL_MS. When the interface refuses a packet, sending is tried again after a delay, and a warning is set once it has refused every packet for a minute. With a remote timeout, a remote entity loses its state when none arrives for it within the timeout.

Definition at line 60 of file entity_transport.h.

Member Enumeration Documentation

◆ DropReason

enum class esphome::entity_transport::EntityTransport::DropReason : uint8_t
strongprotected

The kinds of dropped packet, each warned about at most once a minute, so a steady stream of one kind does not hide another.

Enumerator
DROP_REASON_TOO_SHORT 
DROP_REASON_FORMAT_VERSION 
DROP_REASON_MALFORMED_DICTIONARY 
DROP_REASON_MALFORMED_DATA 

Definition at line 137 of file entity_transport.h.

Constructor & Destructor Documentation

◆ EntityTransport()

esphome::entity_transport::EntityTransport::EntityTransport ( packet_interface::PacketInterface * interface,
uint8_t * buffer,
size_t buffer_size )
inline
Parameters
bufferHolds the packet being built, buffer_size bytes, at least the largest packet of the interface's top layer; setup() fails if it is nullptr or smaller

Definition at line 64 of file entity_transport.h.

Member Function Documentation

◆ add_binary_sensor()

void esphome::entity_transport::EntityTransport::add_binary_sensor ( const char * id,
binary_sensor::BinarySensor * sensor )
inline

Definition at line 83 of file entity_transport.h.

◆ add_remote_binary_sensor()

void esphome::entity_transport::EntityTransport::add_remote_binary_sensor ( const char * provider,
const char * remote_id,
binary_sensor::BinarySensor * sensor )
inline

Definition at line 93 of file entity_transport.h.

◆ add_remote_sensor()

void esphome::entity_transport::EntityTransport::add_remote_sensor ( const char * provider,
const char * remote_id,
sensor::Sensor * sensor )
inline

Definition at line 88 of file entity_transport.h.

◆ add_sensor()

void esphome::entity_transport::EntityTransport::add_sensor ( const char * id,
sensor::Sensor * sensor )
inline

Definition at line 78 of file entity_transport.h.

◆ clear_remote_states_()

void esphome::entity_transport::EntityTransport::clear_remote_states_ ( bool timed_out_only)
protected

Definition at line 672 of file entity_transport.cpp.

◆ dictionary_seen_()

bool esphome::entity_transport::EntityTransport::dictionary_seen_ ( uint32_t dictionary_id) const
protected

Definition at line 561 of file entity_transport.cpp.

◆ dump_config()

void esphome::entity_transport::EntityTransport::dump_config ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 208 of file entity_transport.cpp.

◆ flush_()

bool esphome::entity_transport::EntityTransport::flush_ ( size_t length,
size_t first,
size_t last )
protected

Definition at line 404 of file entity_transport.cpp.

◆ forget_unnamed_()

void esphome::entity_transport::EntityTransport::forget_unnamed_ ( const char * provider,
size_t provider_length,
uint32_t dictionary_id,
bool first_time )
protected

Clears each remote entity of provider that its complete dictionary does not name, warning if the entity was known or the dictionary is first_time seen, so a resent dictionary does not repeat the warning.

Definition at line 501 of file entity_transport.cpp.

◆ learn_()

void esphome::entity_transport::EntityTransport::learn_ ( const char * provider,
size_t provider_length,
uint8_t type,
uint8_t number,
const char * id,
size_t id_length,
uint32_t dictionary_id )
protected

Definition at line 535 of file entity_transport.cpp.

◆ loop()

void esphome::entity_transport::EntityTransport::loop ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 259 of file entity_transport.cpp.

◆ mark_updated_()

void esphome::entity_transport::EntityTransport::mark_updated_ ( LocalEntity & entity)
protected

Definition at line 254 of file entity_transport.cpp.

◆ on_session_()

void esphome::entity_transport::EntityTransport::on_session_ ( bool connected)
protected

Definition at line 663 of file entity_transport.cpp.

◆ receive_()

void esphome::entity_transport::EntityTransport::receive_ ( const packet_interface::PacketBuffer & data)
protected

Definition at line 416 of file entity_transport.cpp.

◆ receive_data_()

void esphome::entity_transport::EntityTransport::receive_data_ ( std::span< const uint8_t > packet,
uint32_t dictionary_id )
protected

Definition at line 566 of file entity_transport.cpp.

◆ receive_dictionary_()

void esphome::entity_transport::EntityTransport::receive_dictionary_ ( std::span< const uint8_t > packet,
uint32_t dictionary_id )
protected

Definition at line 456 of file entity_transport.cpp.

◆ request_dictionary_()

void esphome::entity_transport::EntityTransport::request_dictionary_ ( uint32_t dictionary_id)
protected

Definition at line 633 of file entity_transport.cpp.

◆ send_all_()

void esphome::entity_transport::EntityTransport::send_all_ ( )
protected

Definition at line 245 of file entity_transport.cpp.

◆ send_dictionary_()

bool esphome::entity_transport::EntityTransport::send_dictionary_ ( )
protected

Definition at line 295 of file entity_transport.cpp.

◆ send_updated_()

bool esphome::entity_transport::EntityTransport::send_updated_ ( )
protected

Definition at line 382 of file entity_transport.cpp.

◆ sending_refused_()

void esphome::entity_transport::EntityTransport::sending_refused_ ( )
protected

Definition at line 275 of file entity_transport.cpp.

◆ set_entity_count()

void esphome::entity_transport::EntityTransport::set_entity_count ( size_t count)
inline

Definition at line 75 of file entity_transport.h.

◆ set_remote_timeout()

void esphome::entity_transport::EntityTransport::set_remote_timeout ( uint32_t timeout_ms)
inline

Remote entities lose their state when none arrives for this long; 0 keeps the last state.

Definition at line 74 of file entity_transport.h.

◆ set_update_interval()

void esphome::entity_transport::EntityTransport::set_update_interval ( uint32_t interval_ms)
inline

0 chooses the default for the interface's layers; SCHEDULER_DONT_RUN never resends.

Definition at line 72 of file entity_transport.h.

◆ setup()

void esphome::entity_transport::EntityTransport::setup ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 144 of file entity_transport.cpp.

◆ warning_due_()

bool esphome::entity_transport::EntityTransport::warning_due_ ( DropReason reason)
protected

Definition at line 650 of file entity_transport.cpp.

◆ write_header_()

size_t esphome::entity_transport::EntityTransport::write_header_ ( uint8_t type) const
protected

Definition at line 289 of file entity_transport.cpp.

◆ write_record_()

size_t esphome::entity_transport::EntityTransport::write_record_ ( const LocalEntity & entity,
uint8_t * record ) const
protected

Definition at line 349 of file entity_transport.cpp.

Field Documentation

◆ binary_sensor_count_

uint8_t esphome::entity_transport::EntityTransport::binary_sensor_count_ {0}
protected

Definition at line 175 of file entity_transport.h.

◆ buffer_

uint8_t* esphome::entity_transport::EntityTransport::buffer_
protected

Definition at line 158 of file entity_transport.h.

◆ dictionary_id_

uint32_t esphome::entity_transport::EntityTransport::dictionary_id_ {0}
protected

Definition at line 163 of file entity_transport.h.

◆ dictionary_next_

size_t esphome::entity_transport::EntityTransport::dictionary_next_ {0}
protected

Definition at line 160 of file entity_transport.h.

◆ dictionary_part_

uint8_t esphome::entity_transport::EntityTransport::dictionary_part_ {0}
protected

Definition at line 173 of file entity_transport.h.

◆ dictionary_pending_

bool esphome::entity_transport::EntityTransport::dictionary_pending_
protected

Definition at line 177 of file entity_transport.h.

◆ DROP_REASON_COUNT

size_t esphome::entity_transport::EntityTransport::DROP_REASON_COUNT = 4
staticconstexprprotected

Definition at line 143 of file entity_transport.h.

◆ entities_

FixedVector<LocalEntity> esphome::entity_transport::EntityTransport::entities_
protected

Definition at line 150 of file entity_transport.h.

◆ interface_

packet_interface::PacketInterface* esphome::entity_transport::EntityTransport::interface_
protected

Definition at line 149 of file entity_transport.h.

◆ last_request_ms_

uint32_t esphome::entity_transport::EntityTransport::last_request_ms_ {0}
protected

Definition at line 164 of file entity_transport.h.

◆ last_warning_ms_

std::array<uint32_t, DROP_REASON_COUNT> esphome::entity_transport::EntityTransport::last_warning_ms_ {}
protected

Definition at line 170 of file entity_transport.h.

◆ max_packet_size_

size_t esphome::entity_transport::EntityTransport::max_packet_size_
protected

Definition at line 159 of file entity_transport.h.

◆ refused_since_ms_

uint32_t esphome::entity_transport::EntityTransport::refused_since_ms_ {0}
protected

Definition at line 165 of file entity_transport.h.

◆ refusing_

bool esphome::entity_transport::EntityTransport::refusing_
protected

Definition at line 179 of file entity_transport.h.

◆ remote_binary_sensors_

StaticVector<RemoteEntity<binary_sensor::BinarySensor>, ENTITY_TRANSPORT_REMOTE_BINARY_SENSOR_COUNT> esphome::entity_transport::EntityTransport::remote_binary_sensors_
protected

Definition at line 156 of file entity_transport.h.

◆ remote_sensors_

StaticVector<RemoteEntity<sensor::Sensor>, ENTITY_TRANSPORT_REMOTE_SENSOR_COUNT> esphome::entity_transport::EntityTransport::remote_sensors_
protected

Definition at line 152 of file entity_transport.h.

◆ remote_timeout_ms_

uint32_t esphome::entity_transport::EntityTransport::remote_timeout_ms_ {0}
protected

Definition at line 162 of file entity_transport.h.

◆ request_sent_

bool esphome::entity_transport::EntityTransport::request_sent_
protected

Definition at line 178 of file entity_transport.h.

◆ rx_dictionary_id_

uint32_t esphome::entity_transport::EntityTransport::rx_dictionary_id_ {0}
protected

Definition at line 166 of file entity_transport.h.

◆ rx_next_part_

uint8_t esphome::entity_transport::EntityTransport::rx_next_part_ {0}
protected

Definition at line 172 of file entity_transport.h.

◆ seen_dictionaries_

std::array<uint32_t, 8> esphome::entity_transport::EntityTransport::seen_dictionaries_ {}
protected

Definition at line 169 of file entity_transport.h.

◆ seen_next_

uint8_t esphome::entity_transport::EntityTransport::seen_next_ {0}
protected

Definition at line 171 of file entity_transport.h.

◆ sensor_count_

uint8_t esphome::entity_transport::EntityTransport::sensor_count_ {0}
protected

Definition at line 174 of file entity_transport.h.

◆ update_interval_ms_

uint32_t esphome::entity_transport::EntityTransport::update_interval_ms_ {0}
protected

Definition at line 161 of file entity_transport.h.

◆ warned_

uint8_t esphome::entity_transport::EntityTransport::warned_ {0}
protected

Definition at line 176 of file entity_transport.h.


The documentation for this class was generated from the following files: