ESPHome 2025.6.2
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
8namespace esphome {
9namespace openthread_info {
10
12
14 public:
15 void update() override {
16 auto lock = InstanceLock::try_acquire(10);
17 if (!lock) {
18 return;
19 }
20
21 this->update_instance(lock->get_instance());
22 }
23 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
24
25 protected:
26 virtual void update_instance(otInstance *instance) = 0;
27};
28
30 public:
31 void update() override {
33 if (!address) {
34 return;
35 }
36
37 char address_as_string[40];
38 otIp6AddressToString(&*address, address_as_string, 40);
39 std::string ip = address_as_string;
40
41 if (this->last_ip_ != ip) {
42 this->last_ip_ = ip;
43 this->publish_state(this->last_ip_);
44 }
45 }
46 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
47 void dump_config() override;
48
49 protected:
50 std::string last_ip_;
51};
52
54 public:
55 void update_instance(otInstance *instance) override {
56 otDeviceRole role = otThreadGetDeviceRole(instance);
57
58 if (this->last_role_ != role) {
59 this->last_role_ = role;
60 this->publish_state(otThreadDeviceRoleToString(this->last_role_));
61 }
62 }
63 void dump_config() override;
64
65 protected:
66 otDeviceRole last_role_;
67};
68
70 public:
71 void update_instance(otInstance *instance) override {
72 uint16_t rloc16 = otThreadGetRloc16(instance);
73 if (this->last_rloc16_ != rloc16) {
74 this->last_rloc16_ = rloc16;
75 char buf[5];
76 snprintf(buf, sizeof(buf), "%04x", rloc16);
77 this->publish_state(buf);
78 }
79 }
80 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
81 void dump_config() override;
82
83 protected:
84 uint16_t last_rloc16_;
85};
86
88 public:
89 void update_instance(otInstance *instance) override {
90 const auto *extaddr = otLinkGetExtendedAddress(instance);
91 if (!std::equal(this->last_extaddr_.begin(), this->last_extaddr_.end(), extaddr->m8)) {
92 std::copy(extaddr->m8, extaddr->m8 + 8, this->last_extaddr_.begin());
93 this->publish_state(format_hex(extaddr->m8, 8));
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 this->publish_state(format_hex(this->last_eui64_.begin(), 8));
112 }
113 }
114 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
115 void dump_config() override;
116
117 protected:
118 std::array<uint8_t, 8> last_eui64_{};
119};
120
122 public:
123 void update_instance(otInstance *instance) override {
124 uint8_t channel = otLinkGetChannel(instance);
125 if (this->last_channel_ != channel) {
126 this->last_channel_ = channel;
127 this->publish_state(std::to_string(this->last_channel_));
128 }
129 }
130 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
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 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
161 void dump_config() override;
162
163 protected:
165};
166
168 public:
169 void update_dataset(otOperationalDataset *dataset) override {
170 if (!std::equal(this->last_key_.begin(), this->last_key_.end(), dataset->mNetworkKey.m8)) {
171 std::copy(dataset->mNetworkKey.m8, dataset->mNetworkKey.m8 + 16, this->last_key_.begin());
172 this->publish_state(format_hex(dataset->mNetworkKey.m8, 16));
173 }
174 }
175 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
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 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
194 void dump_config() override;
195
196 protected:
197 uint16_t last_panid_;
198};
199
201 public:
202 void update_dataset(otOperationalDataset *dataset) override {
203 if (!std::equal(this->last_extpanid_.begin(), this->last_extpanid_.end(), dataset->mExtendedPanId.m8)) {
204 std::copy(dataset->mExtendedPanId.m8, dataset->mExtendedPanId.m8 + 8, this->last_extpanid_.begin());
205 this->publish_state(format_hex(this->last_extpanid_.begin(), 8));
206 }
207 }
208
209 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
210 void dump_config() override;
211
212 protected:
213 std::array<uint8_t, 8> last_extpanid_{};
214};
215
216} // namespace openthread_info
217} // namespace esphome
218#endif
uint8_t address
Definition bl0906.h:4
This class simplifies creating components that periodically check a state.
Definition component.h:331
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:27
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
Definition helpers.cpp:360