ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
openthread_info_text_sensor.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_OPENTHREAD
7
9
10using esphome::openthread::InstanceLock;
11
13 public:
14 void update() override {
15 auto lock = InstanceLock::try_acquire(10);
16 if (!lock) {
17 return;
18 }
19
20 this->update_instance(lock.get_instance());
21 }
22 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
23
24 protected:
25 virtual void update_instance(otInstance *instance) = 0;
26};
27
29 public:
30 void update() override {
31 std::optional<otIp6Address> address = openthread::global_openthread_component->get_omr_address();
32 if (!address) {
33 return;
34 }
35
36 char buf[OT_IP6_ADDRESS_STRING_SIZE];
37 otIp6AddressToString(&*address, buf, sizeof(buf));
38
39 if (this->last_ip_ != buf) {
40 this->last_ip_ = buf;
41 this->publish_state(buf);
42 }
43 }
44 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
45 void dump_config() override;
46
47 protected:
48 std::string last_ip_;
49};
50
52 public:
53 void update_instance(otInstance *instance) override {
54 otDeviceRole role = otThreadGetDeviceRole(instance);
55
56 if (this->last_role_ != role) {
57 this->last_role_ = role;
58 this->publish_state(otThreadDeviceRoleToString(this->last_role_));
59 }
60 }
61 void dump_config() override;
62
63 protected:
64 otDeviceRole last_role_;
65};
66
68 public:
69 void update_instance(otInstance *instance) override {
70 uint16_t rloc16 = otThreadGetRloc16(instance);
71 if (this->last_rloc16_ != rloc16) {
72 this->last_rloc16_ = rloc16;
73 char buf[5];
74 snprintf(buf, sizeof(buf), "%04x", rloc16);
75 this->publish_state(buf);
76 }
77 }
78 void dump_config() override;
79
80 protected:
81 uint16_t last_rloc16_;
82};
83
85 public:
86 void update_instance(otInstance *instance) override {
87 const auto *extaddr = otLinkGetExtendedAddress(instance);
88 if (!std::equal(this->last_extaddr_.begin(), this->last_extaddr_.end(), extaddr->m8)) {
89 std::copy(extaddr->m8, extaddr->m8 + 8, this->last_extaddr_.begin());
90 char buf[format_hex_size(8)];
91 format_hex_to(buf, extaddr->m8, 8);
92 this->publish_state(buf);
93 }
94 }
95 void dump_config() override;
96
97 protected:
98 std::array<uint8_t, 8> last_extaddr_{};
99};
100
102 public:
103 void update_instance(otInstance *instance) override {
104 otExtAddress addr;
105 otLinkGetFactoryAssignedIeeeEui64(instance, &addr);
106
107 if (!std::equal(this->last_eui64_.begin(), this->last_eui64_.end(), addr.m8)) {
108 std::copy(addr.m8, addr.m8 + 8, this->last_eui64_.begin());
109 char buf[format_hex_size(8)];
110 format_hex_to(buf, this->last_eui64_.data(), 8);
111 this->publish_state(buf);
112 }
113 }
114 void dump_config() override;
115
116 protected:
117 std::array<uint8_t, 8> last_eui64_{};
118};
119
121 public:
122 void update_instance(otInstance *instance) override {
123 uint8_t channel = otLinkGetChannel(instance);
124 if (this->last_channel_ != channel) {
125 this->last_channel_ = channel;
126 char buf[4]; // max "255" + null
127 snprintf(buf, sizeof(buf), "%u", channel);
128 this->publish_state(buf);
129 }
130 }
131 void dump_config() override;
132
133 protected:
135};
136
138 public:
139 void update_instance(otInstance *instance) override {
140 const otIp6Address *addr = otThreadGetMeshLocalEid(instance);
141 if (otIp6IsAddressEqual(addr, &this->last_addr_)) {
142 return;
143 }
144 this->last_addr_ = *addr;
145 char buf[OT_IP6_ADDRESS_STRING_SIZE];
146 otIp6AddressToString(addr, buf, sizeof(buf));
147 this->publish_state(buf);
148 }
149 void dump_config() override;
150
151 protected:
152 otIp6Address last_addr_{};
153};
154
156 public:
157 void update_instance(otInstance *instance) override {
158 otOperationalDataset dataset;
159 if (otDatasetGetActive(instance, &dataset) != OT_ERROR_NONE) {
160 return;
161 }
162
163 this->update_dataset(&dataset);
164 }
165
166 protected:
167 virtual void update_dataset(otOperationalDataset *dataset) = 0;
168};
169
171 public:
172 void update_dataset(otOperationalDataset *dataset) override {
173 if (this->last_network_name_ != dataset->mNetworkName.m8) {
174 this->last_network_name_ = dataset->mNetworkName.m8;
176 }
177 }
178 void dump_config() override;
179
180 protected:
182};
183
185 public:
186 void update_dataset(otOperationalDataset *dataset) override {
187 if (!std::equal(this->last_key_.begin(), this->last_key_.end(), dataset->mNetworkKey.m8)) {
188 std::copy(dataset->mNetworkKey.m8, dataset->mNetworkKey.m8 + 16, this->last_key_.begin());
189 char buf[format_hex_size(16)];
190 format_hex_to(buf, dataset->mNetworkKey.m8, 16);
191 this->publish_state(buf);
192 }
193 }
194 void dump_config() override;
195
196 protected:
197 std::array<uint8_t, 16> last_key_{};
198};
199
201 public:
202 void update_dataset(otOperationalDataset *dataset) override {
203 uint16_t panid = dataset->mPanId;
204 if (this->last_panid_ != panid) {
205 this->last_panid_ = panid;
206 char buf[5];
207 snprintf(buf, sizeof(buf), "%04x", panid);
208 this->publish_state(buf);
209 }
210 }
211 void dump_config() override;
212
213 protected:
214 uint16_t last_panid_;
215};
216
218 public:
219 void update_dataset(otOperationalDataset *dataset) override {
220 if (!std::equal(this->last_extpanid_.begin(), this->last_extpanid_.end(), dataset->mExtendedPanId.m8)) {
221 std::copy(dataset->mExtendedPanId.m8, dataset->mExtendedPanId.m8 + 8, this->last_extpanid_.begin());
222 char buf[format_hex_size(8)];
223 format_hex_to(buf, this->last_extpanid_.data(), 8);
224 this->publish_state(buf);
225 }
226 }
227
228 void dump_config() override;
229
230 protected:
231 std::array<uint8_t, 8> last_extpanid_{};
232};
233
234} // namespace esphome::openthread_info
235#endif
uint8_t address
Definition bl0906.h:4
This class simplifies creating components that periodically check a state.
Definition component.h:519
virtual void update_dataset(otOperationalDataset *dataset)=0
void update_dataset(otOperationalDataset *dataset) override
void update_dataset(otOperationalDataset *dataset) override
void update_dataset(otOperationalDataset *dataset) override
virtual void update_instance(otInstance *instance)=0
void update_dataset(otOperationalDataset *dataset) override
void update_instance(otInstance *instance) override
void publish_state(const std::string &state)
Definition text_sensor.h:42
OpenThreadComponent * global_openthread_component
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:55
constexpr size_t format_hex_size(size_t byte_count)
Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1.
Definition helpers.h:1583
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:428
SemaphoreHandle_t lock