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