Openthread code updates (#9047)

This commit is contained in:
Jesse Hills 2025-06-11 22:41:38 +12:00 committed by GitHub
parent 052f558131
commit c3c3a27af2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 90 additions and 103 deletions

View File

@ -8,7 +8,6 @@ from esphome.components.esp32 import (
from esphome.components.mdns import MDNSComponent from esphome.components.mdns import MDNSComponent
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import CONF_CHANNEL, CONF_ENABLE_IPV6, CONF_ID from esphome.const import CONF_CHANNEL, CONF_ENABLE_IPV6, CONF_ID
from esphome.core import CORE
import esphome.final_validate as fv import esphome.final_validate as fv
from .const import ( from .const import (
@ -140,7 +139,6 @@ async def to_code(config):
await cg.register_component(ot, config) await cg.register_component(ot, config)
srp = cg.new_Pvariable(config[CONF_SRP_ID]) srp = cg.new_Pvariable(config[CONF_SRP_ID])
cg.add(srp.set_host_name(cg.RawExpression(f'"{CORE.name}"')))
mdns_component = await cg.get_variable(config[CONF_MDNS_ID]) mdns_component = await cg.get_variable(config[CONF_MDNS_ID])
cg.add(srp.set_mdns(mdns_component)) cg.add(srp.set_mdns(mdns_component))
await cg.register_component(srp, config) await cg.register_component(srp, config)

View File

@ -14,15 +14,17 @@
#include <cstring> #include <cstring>
#include "esphome/core/application.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#define TAG "openthread" static const char *const TAG = "openthread";
namespace esphome { namespace esphome {
namespace openthread { namespace openthread {
OpenThreadComponent *global_openthread_component = nullptr; OpenThreadComponent *global_openthread_component = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
OpenThreadComponent::OpenThreadComponent() { global_openthread_component = this; } OpenThreadComponent::OpenThreadComponent() { global_openthread_component = this; }
@ -58,63 +60,67 @@ bool OpenThreadComponent::is_connected() {
// Gets the off-mesh routable address // Gets the off-mesh routable address
std::optional<otIp6Address> OpenThreadComponent::get_omr_address() { std::optional<otIp6Address> OpenThreadComponent::get_omr_address() {
auto lock = InstanceLock::acquire(); InstanceLock lock = InstanceLock::acquire();
return this->get_omr_address_(lock); return this->get_omr_address_(lock);
} }
std::optional<otIp6Address> OpenThreadComponent::get_omr_address_(std::optional<InstanceLock> &lock) { std::optional<otIp6Address> OpenThreadComponent::get_omr_address_(InstanceLock &lock) {
otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT; otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT;
otInstance *instance = nullptr; otInstance *instance = nullptr;
instance = lock->get_instance(); instance = lock.get_instance();
otBorderRouterConfig aConfig; otBorderRouterConfig config;
if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &aConfig) != OT_ERROR_NONE) { if (otNetDataGetNextOnMeshPrefix(instance, &iterator, &config) != OT_ERROR_NONE) {
return std::nullopt; return std::nullopt;
} }
const otIp6Prefix *omrPrefix = &aConfig.mPrefix; const otIp6Prefix *omr_prefix = &config.mPrefix;
const otNetifAddress *unicastAddrs = otIp6GetUnicastAddresses(instance); const otNetifAddress *unicast_addresses = otIp6GetUnicastAddresses(instance);
for (const otNetifAddress *addr = unicastAddrs; addr; addr = addr->mNext) { for (const otNetifAddress *addr = unicast_addresses; addr; addr = addr->mNext) {
const otIp6Address *localIp = &addr->mAddress; const otIp6Address *local_ip = &addr->mAddress;
if (otIp6PrefixMatch(&omrPrefix->mPrefix, localIp)) { if (otIp6PrefixMatch(&omr_prefix->mPrefix, local_ip)) {
return *localIp; return *local_ip;
} }
} }
return {}; return {};
} }
void srpCallback(otError aError, const otSrpClientHostInfo *aHostInfo, const otSrpClientService *aServices, void srp_callback(otError err, const otSrpClientHostInfo *host_info, const otSrpClientService *services,
const otSrpClientService *aRemovedServices, void *aContext) { const otSrpClientService *removed_services, void *context) {
if (aError != 0) { if (err != 0) {
ESP_LOGW(TAG, "SRP client reported an error: %s", otThreadErrorToString(aError)); ESP_LOGW(TAG, "SRP client reported an error: %s", otThreadErrorToString(err));
for (const otSrpClientHostInfo *host = aHostInfo; host; host = nullptr) { for (const otSrpClientHostInfo *host = host_info; host; host = nullptr) {
ESP_LOGW(TAG, " Host: %s", host->mName); ESP_LOGW(TAG, " Host: %s", host->mName);
} }
for (const otSrpClientService *service = aServices; service; service = service->mNext) { for (const otSrpClientService *service = services; service; service = service->mNext) {
ESP_LOGW(TAG, " Service: %s", service->mName); ESP_LOGW(TAG, " Service: %s", service->mName);
} }
} }
} }
void srpStartCallback(const otSockAddr *aServerSockAddr, void *aContext) { ESP_LOGI(TAG, "SRP client has started"); } void srp_start_callback(const otSockAddr *server_socket_address, void *context) {
ESP_LOGI(TAG, "SRP client has started");
}
void OpenThreadSrpComponent::setup() { void OpenThreadSrpComponent::setup() {
otError error; otError error;
auto lock = InstanceLock::acquire(); InstanceLock lock = InstanceLock::acquire();
otInstance *instance = lock->get_instance(); otInstance *instance = lock.get_instance();
otSrpClientSetCallback(instance, srpCallback, nullptr); otSrpClientSetCallback(instance, srp_callback, nullptr);
// set the host name // set the host name
uint16_t size; uint16_t size;
char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &size); char *existing_host_name = otSrpClientBuffersGetHostNameString(instance, &size);
uint16_t len = this->host_name_.size(); const std::string &host_name = App.get_name();
if (len > size) { uint16_t host_name_len = host_name.size();
if (host_name_len > size) {
ESP_LOGW(TAG, "Hostname is too long, choose a shorter project name"); ESP_LOGW(TAG, "Hostname is too long, choose a shorter project name");
return; return;
} }
memcpy(existing_host_name, this->host_name_.c_str(), len + 1); memset(existing_host_name, 0, size);
memcpy(existing_host_name, host_name.c_str(), host_name_len);
error = otSrpClientSetHostName(instance, existing_host_name); error = otSrpClientSetHostName(instance, existing_host_name);
if (error != 0) { if (error != 0) {
@ -150,27 +156,28 @@ void OpenThreadSrpComponent::setup() {
// Set instance name (using host_name) // Set instance name (using host_name)
string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size); string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size);
if (this->host_name_.size() > size) { if (host_name_len > size) {
ESP_LOGW(TAG, "Instance name too long: %s", this->host_name_.c_str()); ESP_LOGW(TAG, "Instance name too long: %s", host_name.c_str());
continue; continue;
} }
memcpy(string, this->host_name_.c_str(), this->host_name_.size() + 1); memset(string, 0, size);
memcpy(string, host_name.c_str(), host_name_len);
// Set port // Set port
entry->mService.mPort = const_cast<TemplatableValue<uint16_t> &>(service.port).value(); entry->mService.mPort = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
otDnsTxtEntry *mTxtEntries = otDnsTxtEntry *txt_entries =
reinterpret_cast<otDnsTxtEntry *>(this->pool_alloc_(sizeof(otDnsTxtEntry) * service.txt_records.size())); reinterpret_cast<otDnsTxtEntry *>(this->pool_alloc_(sizeof(otDnsTxtEntry) * service.txt_records.size()));
// Set TXT records // Set TXT records
entry->mService.mNumTxtEntries = service.txt_records.size(); entry->mService.mNumTxtEntries = service.txt_records.size();
for (size_t i = 0; i < service.txt_records.size(); i++) { for (size_t i = 0; i < service.txt_records.size(); i++) {
const auto &txt = service.txt_records[i]; const auto &txt = service.txt_records[i];
auto value = const_cast<TemplatableValue<std::string> &>(txt.value).value(); auto value = const_cast<TemplatableValue<std::string> &>(txt.value).value();
mTxtEntries[i].mKey = txt.key.c_str(); txt_entries[i].mKey = strdup(txt.key.c_str());
mTxtEntries[i].mValue = reinterpret_cast<const uint8_t *>(value.c_str()); txt_entries[i].mValue = reinterpret_cast<const uint8_t *>(strdup(value.c_str()));
mTxtEntries[i].mValueLength = value.size(); txt_entries[i].mValueLength = value.size();
} }
entry->mService.mTxtEntries = mTxtEntries; entry->mService.mTxtEntries = txt_entries;
entry->mService.mNumTxtEntries = service.txt_records.size(); entry->mService.mNumTxtEntries = service.txt_records.size();
// Add service // Add service
@ -181,8 +188,8 @@ void OpenThreadSrpComponent::setup() {
ESP_LOGW(TAG, "Added service: %s", full_service.c_str()); ESP_LOGW(TAG, "Added service: %s", full_service.c_str());
} }
otSrpClientEnableAutoStartMode(instance, srpStartCallback, nullptr); otSrpClientEnableAutoStartMode(instance, srp_start_callback, nullptr);
ESP_LOGW(TAG, "Finished SRP setup **** "); ESP_LOGW(TAG, "Finished SRP setup");
} }
void *OpenThreadSrpComponent::pool_alloc_(size_t size) { void *OpenThreadSrpComponent::pool_alloc_(size_t size) {
@ -191,8 +198,6 @@ void *OpenThreadSrpComponent::pool_alloc_(size_t size) {
return ptr; return ptr;
} }
void OpenThreadSrpComponent::set_host_name(std::string host_name) { this->host_name_ = host_name; }
void OpenThreadSrpComponent::set_mdns(esphome::mdns::MDNSComponent *mdns) { this->mdns_ = mdns; } void OpenThreadSrpComponent::set_mdns(esphome::mdns::MDNSComponent *mdns) { this->mdns_ = mdns; }
} // namespace openthread } // namespace openthread

View File

@ -2,14 +2,14 @@
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
#ifdef USE_OPENTHREAD #ifdef USE_OPENTHREAD
#include "esphome/core/component.h"
#include "esphome/components/mdns/mdns_component.h" #include "esphome/components/mdns/mdns_component.h"
#include "esphome/components/network/ip_address.h" #include "esphome/components/network/ip_address.h"
#include "esphome/core/component.h"
#include <openthread/thread.h> #include <openthread/thread.h>
#include <vector>
#include <optional> #include <optional>
#include <vector>
namespace esphome { namespace esphome {
namespace openthread { namespace openthread {
@ -29,23 +29,19 @@ class OpenThreadComponent : public Component {
void ot_main(); void ot_main();
protected: protected:
std::optional<otIp6Address> get_omr_address_(std::optional<InstanceLock> &lock); std::optional<otIp6Address> get_omr_address_(InstanceLock &lock);
}; };
extern OpenThreadComponent *global_openthread_component; extern OpenThreadComponent *global_openthread_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
class OpenThreadSrpComponent : public Component { class OpenThreadSrpComponent : public Component {
public: public:
void set_mdns(esphome::mdns::MDNSComponent *mdns); void set_mdns(esphome::mdns::MDNSComponent *mdns);
void set_host_name(std::string host_name);
// This has to run after the mdns component or else no services are available to advertise // This has to run after the mdns component or else no services are available to advertise
float get_setup_priority() const override { return this->mdns_->get_setup_priority() - 1.0; } float get_setup_priority() const override { return this->mdns_->get_setup_priority() - 1.0; }
protected:
void setup() override; void setup() override;
private: protected:
std::string host_name_;
esphome::mdns::MDNSComponent *mdns_{nullptr}; esphome::mdns::MDNSComponent *mdns_{nullptr};
std::vector<esphome::mdns::MDNSService> mdns_services_; std::vector<esphome::mdns::MDNSService> mdns_services_;
std::vector<std::unique_ptr<uint8_t[]>> memory_pool_; std::vector<std::unique_ptr<uint8_t[]>> memory_pool_;
@ -55,7 +51,7 @@ class OpenThreadSrpComponent : public Component {
class InstanceLock { class InstanceLock {
public: public:
static std::optional<InstanceLock> try_acquire(int delay); static std::optional<InstanceLock> try_acquire(int delay);
static std::optional<InstanceLock> acquire(); static InstanceLock acquire();
~InstanceLock(); ~InstanceLock();
// Returns the global openthread instance guarded by this lock // Returns the global openthread instance guarded by this lock

View File

@ -1,34 +1,34 @@
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
#if defined(USE_OPENTHREAD) && defined(USE_ESP_IDF) #if defined(USE_OPENTHREAD) && defined(USE_ESP_IDF)
#include "openthread.h"
#include <openthread/logging.h> #include <openthread/logging.h>
#include "openthread.h"
#include "esp_log.h"
#include "esp_openthread.h" #include "esp_openthread.h"
#include "esp_openthread_lock.h" #include "esp_openthread_lock.h"
#include "esp_log.h"
#include "esp_task_wdt.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esp_task_wdt.h"
#include "esp_openthread_cli.h" #include "esp_err.h"
#include "esp_openthread_netif_glue.h"
#include "esp_event.h" #include "esp_event.h"
#include "nvs_flash.h"
#include "esp_vfs_eventfd.h"
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_netif_types.h" #include "esp_netif_types.h"
#include "esp_err.h" #include "esp_openthread_cli.h"
#include "esp_openthread_netif_glue.h"
#include "esp_vfs_eventfd.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "nvs_flash.h"
#define TAG "openthread" static const char *const TAG = "openthread";
namespace esphome { namespace esphome {
namespace openthread { namespace openthread {
void OpenThreadComponent::setup() { void OpenThreadComponent::setup() {
ESP_LOGI(TAG, "Setting up OpenThread..."); ESP_LOGCONFIG(TAG, "Running setup");
// Used eventfds: // Used eventfds:
// * netif // * netif
// * ot task queue // * ot task queue
@ -47,8 +47,6 @@ void OpenThreadComponent::setup() {
vTaskDelete(nullptr); vTaskDelete(nullptr);
}, },
"ot_main", 10240, this, 5, nullptr); "ot_main", 10240, this, 5, nullptr);
ESP_LOGI(TAG, "OpenThread started");
} }
static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config) { static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config) {
@ -150,7 +148,7 @@ std::optional<InstanceLock> InstanceLock::try_acquire(int delay) {
return {}; return {};
} }
std::optional<InstanceLock> InstanceLock::acquire() { InstanceLock InstanceLock::acquire() {
while (!esp_openthread_lock_acquire(100)) { while (!esp_openthread_lock_acquire(100)) {
esp_task_wdt_reset(); esp_task_wdt_reset();
} }

View File

@ -8,16 +8,16 @@ namespace openthread_info {
static const char *const TAG = "openthread_info"; static const char *const TAG = "openthread_info";
void IPAddressOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo IPAddress", this); } void IPAddressOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "IPAddress", this); }
void RoleOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo Role", this); } void RoleOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "Role", this); }
void ChannelOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo Channel", this); } void ChannelOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "Channel", this); }
void Rloc16OpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo Rloc16", this); } void Rloc16OpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "Rloc16", this); }
void ExtAddrOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo ExtAddr", this); } void ExtAddrOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "ExtAddr", this); }
void Eui64OpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo Eui64", this); } void Eui64OpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "Eui64", this); }
void NetworkNameOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo Network Name", this); } void NetworkNameOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "Network Name", this); }
void NetworkKeyOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo Network Key", this); } void NetworkKeyOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "Network Key", this); }
void PanIdOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo PAN ID", this); } void PanIdOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "PAN ID", this); }
void ExtPanIdOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "OpenThreadInfo Extended PAN ID", this); } void ExtPanIdOpenThreadInfo::dump_config() { LOG_TEXT_SENSOR("", "Extended PAN ID", this); }
} // namespace openthread_info } // namespace openthread_info
} // namespace esphome } // namespace esphome

View File

@ -1,8 +1,8 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/components/openthread/openthread.h" #include "esphome/components/openthread/openthread.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/core/component.h"
#ifdef USE_OPENTHREAD #ifdef USE_OPENTHREAD
namespace esphome { namespace esphome {
@ -18,12 +18,12 @@ class OpenThreadInstancePollingComponent : public PollingComponent {
return; return;
} }
this->update_instance_(lock->get_instance()); this->update_instance(lock->get_instance());
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
protected: protected:
virtual void update_instance_(otInstance *instance) = 0; virtual void update_instance(otInstance *instance) = 0;
}; };
class IPAddressOpenThreadInfo : public PollingComponent, public text_sensor::TextSensor { class IPAddressOpenThreadInfo : public PollingComponent, public text_sensor::TextSensor {
@ -34,9 +34,9 @@ class IPAddressOpenThreadInfo : public PollingComponent, public text_sensor::Tex
return; return;
} }
char addressAsString[40]; char address_as_string[40];
otIp6AddressToString(&*address, addressAsString, 40); otIp6AddressToString(&*address, address_as_string, 40);
std::string ip = addressAsString; std::string ip = address_as_string;
if (this->last_ip_ != ip) { if (this->last_ip_ != ip) {
this->last_ip_ = ip; this->last_ip_ = ip;
@ -44,7 +44,6 @@ class IPAddressOpenThreadInfo : public PollingComponent, public text_sensor::Tex
} }
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-ip"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -53,7 +52,7 @@ class IPAddressOpenThreadInfo : public PollingComponent, public text_sensor::Tex
class RoleOpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor { class RoleOpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor {
public: public:
void update_instance_(otInstance *instance) override { void update_instance(otInstance *instance) override {
otDeviceRole role = otThreadGetDeviceRole(instance); otDeviceRole role = otThreadGetDeviceRole(instance);
if (this->last_role_ != role) { if (this->last_role_ != role) {
@ -61,7 +60,6 @@ class RoleOpenThreadInfo : public OpenThreadInstancePollingComponent, public tex
this->publish_state(otThreadDeviceRoleToString(this->last_role_)); this->publish_state(otThreadDeviceRoleToString(this->last_role_));
} }
} }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-role"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -70,7 +68,7 @@ class RoleOpenThreadInfo : public OpenThreadInstancePollingComponent, public tex
class Rloc16OpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor { class Rloc16OpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor {
public: public:
void update_instance_(otInstance *instance) override { void update_instance(otInstance *instance) override {
uint16_t rloc16 = otThreadGetRloc16(instance); uint16_t rloc16 = otThreadGetRloc16(instance);
if (this->last_rloc16_ != rloc16) { if (this->last_rloc16_ != rloc16) {
this->last_rloc16_ = rloc16; this->last_rloc16_ = rloc16;
@ -80,7 +78,6 @@ class Rloc16OpenThreadInfo : public OpenThreadInstancePollingComponent, public t
} }
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-rloc16"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -89,15 +86,14 @@ class Rloc16OpenThreadInfo : public OpenThreadInstancePollingComponent, public t
class ExtAddrOpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor { class ExtAddrOpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor {
public: public:
void update_instance_(otInstance *instance) override { void update_instance(otInstance *instance) override {
auto extaddr = otLinkGetExtendedAddress(instance); const auto *extaddr = otLinkGetExtendedAddress(instance);
if (!std::equal(this->last_extaddr_.begin(), this->last_extaddr_.end(), extaddr->m8)) { if (!std::equal(this->last_extaddr_.begin(), this->last_extaddr_.end(), extaddr->m8)) {
std::copy(extaddr->m8, extaddr->m8 + 8, this->last_extaddr_.begin()); std::copy(extaddr->m8, extaddr->m8 + 8, this->last_extaddr_.begin());
this->publish_state(format_hex(extaddr->m8, 8)); this->publish_state(format_hex(extaddr->m8, 8));
} }
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-extaddr"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -106,7 +102,7 @@ class ExtAddrOpenThreadInfo : public OpenThreadInstancePollingComponent, public
class Eui64OpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor { class Eui64OpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor {
public: public:
void update_instance_(otInstance *instance) override { void update_instance(otInstance *instance) override {
otExtAddress addr; otExtAddress addr;
otLinkGetFactoryAssignedIeeeEui64(instance, &addr); otLinkGetFactoryAssignedIeeeEui64(instance, &addr);
@ -116,7 +112,6 @@ class Eui64OpenThreadInfo : public OpenThreadInstancePollingComponent, public te
} }
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-extaddr"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -125,7 +120,7 @@ class Eui64OpenThreadInfo : public OpenThreadInstancePollingComponent, public te
class ChannelOpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor { class ChannelOpenThreadInfo : public OpenThreadInstancePollingComponent, public text_sensor::TextSensor {
public: public:
void update_instance_(otInstance *instance) override { void update_instance(otInstance *instance) override {
uint8_t channel = otLinkGetChannel(instance); uint8_t channel = otLinkGetChannel(instance);
if (this->last_channel_ != channel) { if (this->last_channel_ != channel) {
this->last_channel_ = channel; this->last_channel_ = channel;
@ -133,7 +128,6 @@ class ChannelOpenThreadInfo : public OpenThreadInstancePollingComponent, public
} }
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-extaddr"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -142,29 +136,28 @@ class ChannelOpenThreadInfo : public OpenThreadInstancePollingComponent, public
class DatasetOpenThreadInfo : public OpenThreadInstancePollingComponent { class DatasetOpenThreadInfo : public OpenThreadInstancePollingComponent {
public: public:
void update_instance_(otInstance *instance) override { void update_instance(otInstance *instance) override {
otOperationalDataset dataset; otOperationalDataset dataset;
if (otDatasetGetActive(instance, &dataset) != OT_ERROR_NONE) { if (otDatasetGetActive(instance, &dataset) != OT_ERROR_NONE) {
return; return;
} }
this->update_dataset_(&dataset); this->update_dataset(&dataset);
} }
protected: protected:
virtual void update_dataset_(otOperationalDataset *dataset) = 0; virtual void update_dataset(otOperationalDataset *dataset) = 0;
}; };
class NetworkNameOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::TextSensor { class NetworkNameOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::TextSensor {
public: public:
void update_dataset_(otOperationalDataset *dataset) override { void update_dataset(otOperationalDataset *dataset) override {
if (this->last_network_name_ != dataset->mNetworkName.m8) { if (this->last_network_name_ != dataset->mNetworkName.m8) {
this->last_network_name_ = dataset->mNetworkName.m8; this->last_network_name_ = dataset->mNetworkName.m8;
this->publish_state(this->last_network_name_); this->publish_state(this->last_network_name_);
} }
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-networkname"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -173,14 +166,13 @@ class NetworkNameOpenThreadInfo : public DatasetOpenThreadInfo, public text_sens
class NetworkKeyOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::TextSensor { class NetworkKeyOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::TextSensor {
public: public:
void update_dataset_(otOperationalDataset *dataset) override { void update_dataset(otOperationalDataset *dataset) override {
if (!std::equal(this->last_key_.begin(), this->last_key_.end(), dataset->mNetworkKey.m8)) { if (!std::equal(this->last_key_.begin(), this->last_key_.end(), dataset->mNetworkKey.m8)) {
std::copy(dataset->mNetworkKey.m8, dataset->mNetworkKey.m8 + 16, this->last_key_.begin()); std::copy(dataset->mNetworkKey.m8, dataset->mNetworkKey.m8 + 16, this->last_key_.begin());
this->publish_state(format_hex(dataset->mNetworkKey.m8, 16)); this->publish_state(format_hex(dataset->mNetworkKey.m8, 16));
} }
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-networkkey"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -189,7 +181,7 @@ class NetworkKeyOpenThreadInfo : public DatasetOpenThreadInfo, public text_senso
class PanIdOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::TextSensor { class PanIdOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::TextSensor {
public: public:
void update_dataset_(otOperationalDataset *dataset) override { void update_dataset(otOperationalDataset *dataset) override {
uint16_t panid = dataset->mPanId; uint16_t panid = dataset->mPanId;
if (this->last_panid_ != panid) { if (this->last_panid_ != panid) {
this->last_panid_ = panid; this->last_panid_ = panid;
@ -199,7 +191,6 @@ class PanIdOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::Te
} }
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-panid"; }
void dump_config() override; void dump_config() override;
protected: protected:
@ -208,7 +199,7 @@ class PanIdOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::Te
class ExtPanIdOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::TextSensor { class ExtPanIdOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor::TextSensor {
public: public:
void update_dataset_(otOperationalDataset *dataset) override { void update_dataset(otOperationalDataset *dataset) override {
if (!std::equal(this->last_extpanid_.begin(), this->last_extpanid_.end(), dataset->mExtendedPanId.m8)) { if (!std::equal(this->last_extpanid_.begin(), this->last_extpanid_.end(), dataset->mExtendedPanId.m8)) {
std::copy(dataset->mExtendedPanId.m8, dataset->mExtendedPanId.m8 + 8, this->last_extpanid_.begin()); std::copy(dataset->mExtendedPanId.m8, dataset->mExtendedPanId.m8 + 8, this->last_extpanid_.begin());
this->publish_state(format_hex(this->last_extpanid_.begin(), 8)); this->publish_state(format_hex(this->last_extpanid_.begin(), 8));
@ -216,7 +207,6 @@ class ExtPanIdOpenThreadInfo : public DatasetOpenThreadInfo, public text_sensor:
} }
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
std::string unique_id() override { return get_mac_address() + "-openthreadinfo-extpanid"; }
void dump_config() override; void dump_config() override;
protected: protected: