ESPHome 2025.9.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_encryption_key(std::vector<uint8_t> key) { this->encryption_key_ = std::move(key); }
95 void set_rolling_code_enable(bool enable) { this->rolling_code_enable_ = enable; }
96 void set_ping_pong_enable(bool enable) { this->ping_pong_enable_ = enable; }
97 void set_ping_pong_recycle_time(uint32_t recycle_time) { this->ping_pong_recyle_time_ = recycle_time; }
98 void set_provider_encryption(const char *name, std::vector<uint8_t> key) {
99 this->providers_[name].encryption_key = std::move(key);
100 }
101#ifdef USE_STATUS_SENSOR
103 this->providers_[name].status_sensor = sensor;
104 }
105#endif
106 void set_platform_name(const char *name) { this->platform_name_ = name; }
107
108 protected:
109 // child classes must implement this
110 virtual void send_packet(const std::vector<uint8_t> &buf) const = 0;
111 virtual size_t get_max_packet_size() = 0;
112 virtual bool should_send() { return true; }
113
114 // to be called by child classes when a data packet is received.
115 void process_(const std::vector<uint8_t> &data);
116 void send_data_(bool all);
117 void flush_();
118 void add_data_(uint8_t key, const char *id, float data);
119 void add_data_(uint8_t key, const char *id, uint32_t data);
120 void increment_code_();
121 void add_binary_data_(uint8_t key, const char *id, bool data);
122 void init_data_();
123
124 bool updated_{};
125 uint32_t ping_key_{};
126 uint32_t rolling_code_[2]{};
130 uint32_t last_key_time_{};
133 const char *name_{};
135
136 std::vector<uint8_t> encryption_key_{};
137
138#ifdef USE_SENSOR
139 std::vector<Sensor> sensors_{};
140 std::map<std::string, std::map<std::string, sensor::Sensor *>> remote_sensors_{};
141#endif
142#ifdef USE_BINARY_SENSOR
143 std::vector<BinarySensor> binary_sensors_{};
144 std::map<std::string, std::map<std::string, binary_sensor::BinarySensor *>> remote_binary_sensors_{};
145#endif
146
147 std::map<std::string, Provider> providers_{};
148 std::vector<uint8_t> ping_header_{};
149 std::vector<uint8_t> header_{};
150 std::vector<uint8_t> data_{};
151 std::map<const char *, uint32_t> ping_keys_{};
152 const char *platform_name_{""};
153 void add_key_(const char *name, uint32_t key);
155
156 inline bool is_encrypted_() { return !this->encryption_key_.empty(); }
157};
158
159} // namespace packet_transport
160} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:425
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:59
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition helpers.h:933
binary_sensor::BinarySensor * sensor
std::vector< uint8_t > encryption_key
binary_sensor::BinarySensor * status_sensor