ESPHome 2026.2.1
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
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 {
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 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
79 void dump_config() override;
80
81 protected:
82 uint16_t last_rloc16_;
83};
84
86 public:
87 void update_instance(otInstance *instance) override {
88 const auto *extaddr = otLinkGetExtendedAddress(instance);
89 if (!std::equal(this->last_extaddr_.begin(), this->last_extaddr_.end(), extaddr->m8)) {
90 std::copy(extaddr->m8, extaddr->m8 + 8, this->last_extaddr_.begin());
91 char buf[format_hex_size(8)];
92 format_hex_to(buf, extaddr->m8, 8);
93 this->publish_state(buf);
94 }
95 }
96 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
97 void dump_config() override;
98
99 protected:
100 std::array<uint8_t, 8> last_extaddr_{};
101};
102
104 public:
105 void update_instance(otInstance *instance) override {
106 otExtAddress addr;
107 otLinkGetFactoryAssignedIeeeEui64(instance, &addr);
108
109 if (!std::equal(this->last_eui64_.begin(), this->last_eui64_.end(), addr.m8)) {
110 std::copy(addr.m8, addr.m8 + 8, this->last_eui64_.begin());
111 char buf[format_hex_size(8)];
112 format_hex_to(buf, this->last_eui64_.data(), 8);
113 this->publish_state(buf);
114 }
115 }
116 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
117 void dump_config() override;
118
119 protected:
120 std::array<uint8_t, 8> last_eui64_{};
121};
122
124 public:
125 void update_instance(otInstance *instance) override {
126 uint8_t channel = otLinkGetChannel(instance);
127 if (this->last_channel_ != channel) {
128 this->last_channel_ = channel;
129 char buf[4]; // max "255" + null
130 snprintf(buf, sizeof(buf), "%u", channel);
131 this->publish_state(buf);
132 }
133 }
134 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
135 void dump_config() override;
136
137 protected:
139};
140
142 public:
143 void update_instance(otInstance *instance) override {
144 otOperationalDataset dataset;
145 if (otDatasetGetActive(instance, &dataset) != OT_ERROR_NONE) {
146 return;
147 }
148
149 this->update_dataset(&dataset);
150 }
151
152 protected:
153 virtual void update_dataset(otOperationalDataset *dataset) = 0;
154};
155
157 public:
158 void update_dataset(otOperationalDataset *dataset) override {
159 if (this->last_network_name_ != dataset->mNetworkName.m8) {
160 this->last_network_name_ = dataset->mNetworkName.m8;
162 }
163 }
164 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
165 void dump_config() override;
166
167 protected:
169};
170
172 public:
173 void update_dataset(otOperationalDataset *dataset) override {
174 if (!std::equal(this->last_key_.begin(), this->last_key_.end(), dataset->mNetworkKey.m8)) {
175 std::copy(dataset->mNetworkKey.m8, dataset->mNetworkKey.m8 + 16, this->last_key_.begin());
176 char buf[format_hex_size(16)];
177 format_hex_to(buf, dataset->mNetworkKey.m8, 16);
178 this->publish_state(buf);
179 }
180 }
181 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
182 void dump_config() override;
183
184 protected:
185 std::array<uint8_t, 16> last_key_{};
186};
187
189 public:
190 void update_dataset(otOperationalDataset *dataset) override {
191 uint16_t panid = dataset->mPanId;
192 if (this->last_panid_ != panid) {
193 this->last_panid_ = panid;
194 char buf[5];
195 snprintf(buf, sizeof(buf), "%04x", panid);
196 this->publish_state(buf);
197 }
198 }
199 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
200 void dump_config() override;
201
202 protected:
203 uint16_t last_panid_;
204};
205
207 public:
208 void update_dataset(otOperationalDataset *dataset) override {
209 if (!std::equal(this->last_extpanid_.begin(), this->last_extpanid_.end(), dataset->mExtendedPanId.m8)) {
210 std::copy(dataset->mExtendedPanId.m8, dataset->mExtendedPanId.m8 + 8, this->last_extpanid_.begin());
211 char buf[format_hex_size(8)];
212 format_hex_to(buf, this->last_extpanid_.data(), 8);
213 this->publish_state(buf);
214 }
215 }
216
217 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
218 void dump_config() override;
219
220 protected:
221 std::array<uint8_t, 8> last_extpanid_{};
222};
223
224} // namespace esphome::openthread_info
225#endif
uint8_t address
Definition bl0906.h:4
This class simplifies creating components that periodically check a state.
Definition component.h:512
static std::optional< InstanceLock > try_acquire(int delay)
std::optional< otIp6Address > get_omr_address()
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)
OpenThreadComponent * global_openthread_component
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:91
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:952
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:341