ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
entity_transport.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <span>
5
11#ifdef USE_SENSOR
13#endif
14#ifdef USE_BINARY_SENSOR
16#endif
17
19
60class EntityTransport : public Component {
61 public:
64 EntityTransport(packet_interface::PacketInterface *interface, uint8_t *buffer, size_t buffer_size)
65 : interface_(interface), buffer_(buffer), max_packet_size_(buffer_size) {}
66
67 void setup() override;
68 void loop() override;
69 void dump_config() override;
70
72 void set_update_interval(uint32_t interval_ms) { this->update_interval_ms_ = interval_ms; }
74 void set_remote_timeout(uint32_t timeout_ms) { this->remote_timeout_ms_ = timeout_ms; }
75 void set_entity_count(size_t count) { this->entities_.init(count); }
76 // Sensors must be added before binary sensors, so that data packets hold sensor records first
77#ifdef USE_SENSOR
78 void add_sensor(const char *id, sensor::Sensor *sensor) {
79 this->entities_.push_back({sensor, id, this, this->sensor_count_++, true, true});
80 }
81#endif
82#ifdef USE_BINARY_SENSOR
83 void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor) {
84 this->entities_.push_back({sensor, id, this, this->binary_sensor_count_++, false, true});
85 }
86#endif
87#ifdef ENTITY_TRANSPORT_REMOTE_SENSOR_COUNT
88 void add_remote_sensor(const char *provider, const char *remote_id, sensor::Sensor *sensor) {
89 this->remote_sensors_.push_back({provider, remote_id, sensor});
90 }
91#endif
92#ifdef ENTITY_TRANSPORT_REMOTE_BINARY_SENSOR_COUNT
93 void add_remote_binary_sensor(const char *provider, const char *remote_id, binary_sensor::BinarySensor *sensor) {
94 this->remote_binary_sensors_.push_back({provider, remote_id, sensor});
95 }
96#endif
97
98 protected:
99 struct LocalEntity {
100 EntityBase *entity; // a sensor::Sensor or a binary_sensor::BinarySensor, as is_sensor says
101 const char *id;
103 uint8_t number;
106 };
107 template<typename T> struct RemoteEntity {
108 const char *provider;
109 const char *remote_id;
111 uint32_t dictionary_id{0}; // learnt from the provider's dictionary
113 uint8_t number{0};
114 bool known{false};
115 bool has_state{false}; // a state arrived, and has not yet timed out
116 };
117
118 void send_all_();
119 void mark_updated_(LocalEntity &entity);
120 bool send_dictionary_();
121 bool send_updated_();
122 size_t write_record_(const LocalEntity &entity, uint8_t *record) const;
123 bool flush_(size_t length, size_t first, size_t last);
124 size_t write_header_(uint8_t type) const;
126 void receive_dictionary_(std::span<const uint8_t> packet, uint32_t dictionary_id);
127 void receive_data_(std::span<const uint8_t> packet, uint32_t dictionary_id);
128 void learn_(const char *provider, size_t provider_length, uint8_t type, uint8_t number, const char *id,
129 size_t id_length, uint32_t dictionary_id);
130 void request_dictionary_(uint32_t dictionary_id);
131 bool dictionary_seen_(uint32_t dictionary_id) const;
134 void forget_unnamed_(const char *provider, size_t provider_length, uint32_t dictionary_id, bool first_time);
143 static constexpr size_t DROP_REASON_COUNT = 4;
144 bool warning_due_(DropReason reason);
145 void sending_refused_();
146 void on_session_(bool connected);
147 void clear_remote_states_(bool timed_out_only);
148
151#ifdef ENTITY_TRANSPORT_REMOTE_SENSOR_COUNT
152 StaticVector<RemoteEntity<sensor::Sensor>, ENTITY_TRANSPORT_REMOTE_SENSOR_COUNT> remote_sensors_;
153#endif
154#ifdef ENTITY_TRANSPORT_REMOTE_BINARY_SENSOR_COUNT
155 StaticVector<RemoteEntity<binary_sensor::BinarySensor>, ENTITY_TRANSPORT_REMOTE_BINARY_SENSOR_COUNT>
157#endif
158 uint8_t *buffer_;
159 size_t max_packet_size_; // the buffer's size until setup(), then the interface's largest packet
160 size_t dictionary_next_{0}; // the first entity of the next dictionary packet to send
165 uint32_t refused_since_ms_{0}; // when the interface began refusing every packet
166 uint32_t rx_dictionary_id_{0}; // the dictionary whose parts are arriving
167 // The last dictionary ids received in full, so a provider whose dictionary names none of our entities is not asked
168 // again
169 std::array<uint32_t, 8> seen_dictionaries_{};
170 std::array<uint32_t, DROP_REASON_COUNT> last_warning_ms_{};
171 uint8_t seen_next_{0};
172 uint8_t rx_next_part_{0}; // the index of its next part, or PART_NONE if one was missed
173 uint8_t dictionary_part_{0}; // the index of the next dictionary packet to send
174 uint8_t sensor_count_{0};
176 uint8_t warned_{0}; // a bit for each DropReason already warned about
177 bool dictionary_pending_ : 1 {true};
178 bool request_sent_ : 1 {false};
179 bool refusing_ : 1 {false};
180};
181
182} // namespace esphome::entity_transport
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:757
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:323
Base class for all binary_sensor-type classes.
Sends the states of local sensors and binary sensors over a packet interface, and publishes the state...
void receive_dictionary_(std::span< const uint8_t > packet, uint32_t dictionary_id)
void add_remote_binary_sensor(const char *provider, const char *remote_id, binary_sensor::BinarySensor *sensor)
void receive_data_(std::span< const uint8_t > packet, uint32_t dictionary_id)
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 enti...
void add_remote_sensor(const char *provider, const char *remote_id, sensor::Sensor *sensor)
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)
EntityTransport(packet_interface::PacketInterface *interface, uint8_t *buffer, size_t buffer_size)
StaticVector< RemoteEntity< sensor::Sensor >, ENTITY_TRANSPORT_REMOTE_SENSOR_COUNT > remote_sensors_
void add_sensor(const char *id, sensor::Sensor *sensor)
DropReason
The kinds of dropped packet, each warned about at most once a minute, so a steady stream of one kind ...
bool flush_(size_t length, size_t first, size_t last)
void request_dictionary_(uint32_t dictionary_id)
std::array< uint32_t, DROP_REASON_COUNT > last_warning_ms_
StaticVector< RemoteEntity< binary_sensor::BinarySensor >, ENTITY_TRANSPORT_REMOTE_BINARY_SENSOR_COUNT > remote_binary_sensors_
packet_interface::PacketInterface * interface_
bool dictionary_seen_(uint32_t dictionary_id) const
void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor)
void receive_(const packet_interface::PacketBuffer &data)
void set_remote_timeout(uint32_t timeout_ms)
Remote entities lose their state when none arrives for this long; 0 keeps the last state.
size_t write_record_(const LocalEntity &entity, uint8_t *record) const
void set_update_interval(uint32_t interval_ms)
0 chooses the default for the interface's layers; SCHEDULER_DONT_RUN never resends.
A buffer class that efficiently represents a composite buffer with three sections:
A class that will transport data to and from an abstract channel.
Base-class for all sensors.
Definition sensor.h:48
uint16_t id
uint8_t type
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
static void uint32_t