ESPHome 2026.3.0
Loading...
Searching...
No Matches
packet_transport.h
Go to the documentation of this file.
1#pragma once
2
5#ifdef USE_SENSOR
7#endif
8#ifdef USE_BINARY_SENSOR
10#endif
11
12#include <map>
13#include <span>
14#include <vector>
15
24namespace esphome {
25namespace packet_transport {
26
27// std::less provides allocation-free comparison with const char *
28template<typename T> using string_map_t = std::map<std::string, T, std::less<>>;
29
30struct Provider {
31 std::vector<uint8_t> encryption_key;
32 const char *name;
35#ifdef USE_STATUS_SENSOR
37#endif
38};
39
40#ifdef USE_SENSOR
41struct Sensor {
43 const char *id;
44 bool updated;
45};
46#endif
47#ifdef USE_BINARY_SENSOR
53#endif
54
56 public:
57 void setup() override;
58 void loop() override;
59 void update() override;
60 void dump_config() override;
61
62#ifdef USE_SENSOR
63 void add_sensor(const char *id, sensor::Sensor *sensor) {
64 Sensor st{sensor, id, true};
65 this->sensors_.push_back(st);
66 }
67 void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor) {
68 this->add_provider(hostname);
69 this->remote_sensors_[hostname][remote_id] = sensor;
70 }
71#endif
72#ifdef USE_BINARY_SENSOR
73 void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor) {
74 BinarySensor st{sensor, id, true};
75 this->binary_sensors_.push_back(st);
76 }
77
78 void add_remote_binary_sensor(const char *hostname, const char *remote_id, binary_sensor::BinarySensor *sensor) {
79 this->add_provider(hostname);
80 this->remote_binary_sensors_[hostname][remote_id] = sensor;
81 }
82#endif
83
84 void add_provider(const char *hostname) {
85 if (!this->providers_.contains(hostname)) {
86 Provider provider{};
87 provider.name = hostname;
88 this->providers_[hostname] = provider;
89#ifdef USE_SENSOR
91#endif
92#ifdef USE_BINARY_SENSOR
94#endif
95 }
96 }
97
98 void set_is_provider(bool is_provider) { this->is_provider_ = is_provider; }
99 void set_encryption_key(std::vector<uint8_t> key) { this->encryption_key_ = std::move(key); }
100 void set_rolling_code_enable(bool enable) { this->rolling_code_enable_ = enable; }
101 void set_ping_pong_enable(bool enable) { this->ping_pong_enable_ = enable; }
102 void set_ping_pong_recycle_time(uint32_t recycle_time) { this->ping_pong_recyle_time_ = recycle_time; }
103 void set_provider_encryption(const char *name, std::vector<uint8_t> key) {
104 this->providers_[name].encryption_key = std::move(key);
105 }
106#ifdef USE_STATUS_SENSOR
108 this->providers_[name].status_sensor = sensor;
109 }
110#endif
111 void set_platform_name(const char *name) { this->platform_name_ = name; }
112
113 protected:
114 // child classes must implement this
115 virtual void send_packet(const std::vector<uint8_t> &buf) const = 0;
116 virtual size_t get_max_packet_size() = 0;
117 virtual bool should_send() { return true; }
118
119 // to be called by child classes when a data packet is received.
120 void process_(std::span<const uint8_t> data);
121 void send_data_(bool all);
122 void flush_();
123 void add_data_(uint8_t key, const char *id, float data);
124 void add_data_(uint8_t key, const char *id, uint32_t data);
125 void increment_code_();
126 void add_binary_data_(uint8_t key, const char *id, bool data);
127 void init_data_();
128
129 bool updated_{};
138 const char *name_{};
140
141 std::vector<uint8_t> encryption_key_{};
142
143#ifdef USE_SENSOR
144 std::vector<Sensor> sensors_{};
146#endif
147#ifdef USE_BINARY_SENSOR
148 std::vector<BinarySensor> binary_sensors_{};
150#endif
151
153 std::vector<uint8_t> ping_header_{};
154 std::vector<uint8_t> header_{};
155 std::vector<uint8_t> data_{};
157 const char *platform_name_{""};
158 void add_key_(const char *name, uint32_t key);
160
161 bool is_encrypted_() const { return !this->encryption_key_.empty(); }
162};
163
164} // namespace packet_transport
165} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:538
Base class for all binary_sensor-type classes.
void set_provider_encryption(const char *name, std::vector< uint8_t > key)
void add_remote_binary_sensor(const char *hostname, const char *remote_id, binary_sensor::BinarySensor *sensor)
void add_data_(uint8_t key, const char *id, float data)
std::vector< BinarySensor > binary_sensors_
void set_ping_pong_recycle_time(uint32_t recycle_time)
void add_key_(const char *name, uint32_t key)
string_map_t< string_map_t< binary_sensor::BinarySensor * > > remote_binary_sensors_
void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor)
void set_provider_status_sensor(const char *name, binary_sensor::BinarySensor *sensor)
void add_binary_data_(uint8_t key, const char *id, bool data)
void process_(std::span< const uint8_t > data)
Process a received packet.
void set_encryption_key(std::vector< uint8_t > key)
virtual void send_packet(const std::vector< uint8_t > &buf) const =0
string_map_t< string_map_t< sensor::Sensor * > > remote_sensors_
void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor)
void add_sensor(const char *id, sensor::Sensor *sensor)
Base-class for all sensors.
Definition sensor.h:47
uint16_t id
std::map< std::string, T, std::less<> > string_map_t
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t
binary_sensor::BinarySensor * sensor
std::vector< uint8_t > encryption_key
binary_sensor::BinarySensor * status_sensor