ESPHome 2026.3.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
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 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 otOperationalDataset dataset;
141 if (otDatasetGetActive(instance, &dataset) != OT_ERROR_NONE) {
142 return;
143 }
144
145 this->update_dataset(&dataset);
146 }
147
148 protected:
149 virtual void update_dataset(otOperationalDataset *dataset) = 0;
150};
151
153 public:
154 void update_dataset(otOperationalDataset *dataset) override {
155 if (this->last_network_name_ != dataset->mNetworkName.m8) {
156 this->last_network_name_ = dataset->mNetworkName.m8;
158 }
159 }
160 void dump_config() override;
161
162 protected:
164};
165
167 public:
168 void update_dataset(otOperationalDataset *dataset) override {
169 if (!std::equal(this->last_key_.begin(), this->last_key_.end(), dataset->mNetworkKey.m8)) {
170 std::copy(dataset->mNetworkKey.m8, dataset->mNetworkKey.m8 + 16, this->last_key_.begin());
171 char buf[format_hex_size(16)];
172 format_hex_to(buf, dataset->mNetworkKey.m8, 16);
173 this->publish_state(buf);
174 }
175 }
176 void dump_config() override;
177
178 protected:
179 std::array<uint8_t, 16> last_key_{};
180};
181
183 public:
184 void update_dataset(otOperationalDataset *dataset) override {
185 uint16_t panid = dataset->mPanId;
186 if (this->last_panid_ != panid) {
187 this->last_panid_ = panid;
188 char buf[5];
189 snprintf(buf, sizeof(buf), "%04x", panid);
190 this->publish_state(buf);
191 }
192 }
193 void dump_config() override;
194
195 protected:
196 uint16_t last_panid_;
197};
198
200 public:
201 void update_dataset(otOperationalDataset *dataset) override {
202 if (!std::equal(this->last_extpanid_.begin(), this->last_extpanid_.end(), dataset->mExtendedPanId.m8)) {
203 std::copy(dataset->mExtendedPanId.m8, dataset->mExtendedPanId.m8 + 8, this->last_extpanid_.begin());
204 char buf[format_hex_size(8)];
205 format_hex_to(buf, this->last_extpanid_.data(), 8);
206 this->publish_state(buf);
207 }
208 }
209
210 void dump_config() override;
211
212 protected:
213 std::array<uint8_t, 8> last_extpanid_{};
214};
215
216} // namespace esphome::openthread_info
217#endif
uint8_t address
Definition bl0906.h:4
This class simplifies creating components that periodically check a state.
Definition component.h:527
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
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:41
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:1077
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