ESPHome 2026.2.4
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
27struct Provider {
28 std::vector<uint8_t> encryption_key;
29 const char *name;
30 uint32_t last_code[2];
32#ifdef USE_STATUS_SENSOR
34#endif
35};
36
37#ifdef USE_SENSOR
38struct Sensor {
40 const char *id;
41 bool updated;
42};
43#endif
44#ifdef USE_BINARY_SENSOR
50#endif
51
53 public:
54 void setup() override;
55 void loop() override;
56 void update() override;
57 void dump_config() override;
58
59#ifdef USE_SENSOR
60 void add_sensor(const char *id, sensor::Sensor *sensor) {
61 Sensor st{sensor, id, true};
62 this->sensors_.push_back(st);
63 }
64 void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor) {
65 this->add_provider(hostname);
66 this->remote_sensors_[hostname][remote_id] = sensor;
67 }
68#endif
69#ifdef USE_BINARY_SENSOR
70 void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor) {
71 BinarySensor st{sensor, id, true};
72 this->binary_sensors_.push_back(st);
73 }
74
75 void add_remote_binary_sensor(const char *hostname, const char *remote_id, binary_sensor::BinarySensor *sensor) {
76 this->add_provider(hostname);
77 this->remote_binary_sensors_[hostname][remote_id] = sensor;
78 }
79#endif
80
81 void add_provider(const char *hostname) {
82 if (this->providers_.count(hostname) == 0) {
83 Provider provider{};
84 provider.name = hostname;
85 this->providers_[hostname] = provider;
86#ifdef USE_SENSOR
87 this->remote_sensors_[hostname] = std::map<std::string, sensor::Sensor *>();
88#endif
89#ifdef USE_BINARY_SENSOR
90 this->remote_binary_sensors_[hostname] = std::map<std::string, binary_sensor::BinarySensor *>();
91#endif
92 }
93 }
94
95 void set_is_provider(bool is_provider) { this->is_provider_ = is_provider; }
96 void set_encryption_key(std::vector<uint8_t> key) { this->encryption_key_ = std::move(key); }
97 void set_rolling_code_enable(bool enable) { this->rolling_code_enable_ = enable; }
98 void set_ping_pong_enable(bool enable) { this->ping_pong_enable_ = enable; }
99 void set_ping_pong_recycle_time(uint32_t recycle_time) { this->ping_pong_recyle_time_ = recycle_time; }
100 void set_provider_encryption(const char *name, std::vector<uint8_t> key) {
101 this->providers_[name].encryption_key = std::move(key);
102 }
103#ifdef USE_STATUS_SENSOR
105 this->providers_[name].status_sensor = sensor;
106 }
107#endif
108 void set_platform_name(const char *name) { this->platform_name_ = name; }
109
110 protected:
111 // child classes must implement this
112 virtual void send_packet(const std::vector<uint8_t> &buf) const = 0;
113 virtual size_t get_max_packet_size() = 0;
114 virtual bool should_send() { return true; }
115
116 // to be called by child classes when a data packet is received.
117 void process_(std::span<const uint8_t> data);
118 void send_data_(bool all);
119 void flush_();
120 void add_data_(uint8_t key, const char *id, float data);
121 void add_data_(uint8_t key, const char *id, uint32_t data);
122 void increment_code_();
123 void add_binary_data_(uint8_t key, const char *id, bool data);
124 void init_data_();
125
126 bool updated_{};
127 uint32_t ping_key_{};
128 uint32_t rolling_code_[2]{};
132 uint32_t last_key_time_{};
135 const char *name_{};
137
138 std::vector<uint8_t> encryption_key_{};
139
140#ifdef USE_SENSOR
141 std::vector<Sensor> sensors_{};
142 std::map<std::string, std::map<std::string, sensor::Sensor *>> remote_sensors_{};
143#endif
144#ifdef USE_BINARY_SENSOR
145 std::vector<BinarySensor> binary_sensors_{};
146 std::map<std::string, std::map<std::string, binary_sensor::BinarySensor *>> remote_binary_sensors_{};
147#endif
148
149 std::map<std::string, Provider> providers_{};
150 std::vector<uint8_t> ping_header_{};
151 std::vector<uint8_t> header_{};
152 std::vector<uint8_t> data_{};
153 std::map<const char *, uint32_t> ping_keys_{};
154 const char *platform_name_{""};
155 void add_key_(const char *name, uint32_t key);
157
158 inline bool is_encrypted_() { return !this->encryption_key_.empty(); }
159};
160
161} // namespace packet_transport
162} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:512
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::map< std::string, std::map< std::string, binary_sensor::BinarySensor * > > remote_binary_sensors_
std::vector< BinarySensor > binary_sensors_
void set_ping_pong_recycle_time(uint32_t recycle_time)
void add_key_(const char *name, uint32_t key)
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)
std::map< std::string, std::map< std::string, sensor::Sensor * > > remote_sensors_
void add_binary_data_(uint8_t key, const char *id, bool data)
void process_(std::span< const uint8_t > data)
Process a received packet.
std::map< const char *, uint32_t > ping_keys_
void set_encryption_key(std::vector< uint8_t > key)
virtual void send_packet(const std::vector< uint8_t > &buf) const =0
std::map< std::string, Provider > providers_
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:43
uint16_t id
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
binary_sensor::BinarySensor * sensor
std::vector< uint8_t > encryption_key
binary_sensor::BinarySensor * status_sensor