diff --git a/esphome/components/network/util.cpp b/esphome/components/network/util.cpp index 27ad9448a4..cb8f8569ad 100644 --- a/esphome/components/network/util.cpp +++ b/esphome/components/network/util.cpp @@ -99,7 +99,11 @@ const std::string &get_use_address() { return wifi::global_wifi_component->get_use_address(); #endif -#if !defined(USE_ETHERNET) && !defined(USE_MODEM) && !defined(USE_WIFI) +#ifdef USE_OPENTHREAD + return openthread::global_openthread_component->get_use_address(); +#endif + +#if !defined(USE_ETHERNET) && !defined(USE_MODEM) && !defined(USE_WIFI) && !defined(USE_OPENTHREAD) // Fallback when no network component is defined (e.g., host platform) static const std::string empty; return empty; diff --git a/esphome/components/openthread/__init__.py b/esphome/components/openthread/__init__.py index 4865399d02..572ec144d4 100644 --- a/esphome/components/openthread/__init__.py +++ b/esphome/components/openthread/__init__.py @@ -8,8 +8,10 @@ from esphome.components.esp32 import ( ) from esphome.components.mdns import MDNSComponent, enable_mdns_storage 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, CONF_USE_ADDRESS +from esphome.core import CORE import esphome.final_validate as fv +from esphome.types import ConfigType from .const import ( CONF_DEVICE_TYPE, @@ -108,6 +110,12 @@ _CONNECTION_SCHEMA = cv.Schema( ) +def _validate(config: ConfigType) -> ConfigType: + if CONF_USE_ADDRESS not in config: + config[CONF_USE_ADDRESS] = f"{CORE.name}.local" + return config + + def _require_vfs_select(config): """Register VFS select requirement during config validation.""" # OpenThread uses esp_vfs_eventfd which requires VFS select support @@ -126,11 +134,13 @@ CONFIG_SCHEMA = cv.All( ), cv.Optional(CONF_FORCE_DATASET): cv.boolean, cv.Optional(CONF_TLV): cv.string_strict, + cv.Optional(CONF_USE_ADDRESS): cv.string_strict, } ).extend(_CONNECTION_SCHEMA), cv.has_exactly_one_key(CONF_NETWORK_KEY, CONF_TLV), cv.only_with_esp_idf, only_on_variant(supported=[VARIANT_ESP32C6, VARIANT_ESP32H2]), + _validate, _require_vfs_select, ) @@ -155,6 +165,7 @@ async def to_code(config): enable_mdns_storage() ot = cg.new_Pvariable(config[CONF_ID]) + cg.add(ot.set_use_address(config[CONF_USE_ADDRESS])) await cg.register_component(ot, config) srp = cg.new_Pvariable(config[CONF_SRP_ID]) diff --git a/esphome/components/openthread/openthread.cpp b/esphome/components/openthread/openthread.cpp index b2c2519c08..db909e6b1f 100644 --- a/esphome/components/openthread/openthread.cpp +++ b/esphome/components/openthread/openthread.cpp @@ -252,6 +252,12 @@ void OpenThreadComponent::on_factory_reset(std::function callback) { ESP_LOGD(TAG, "Waiting on Confirmation Removal SRP Host and Services"); } +// set_use_address() is guaranteed to be called during component setup by Python code generation, +// so use_address_ will always be valid when get_use_address() is called - no fallback needed. +const std::string &OpenThreadComponent::get_use_address() const { return this->use_address_; } + +void OpenThreadComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; } + } // namespace openthread } // namespace esphome diff --git a/esphome/components/openthread/openthread.h b/esphome/components/openthread/openthread.h index 5d139c633d..19dbeb4628 100644 --- a/esphome/components/openthread/openthread.h +++ b/esphome/components/openthread/openthread.h @@ -33,11 +33,15 @@ class OpenThreadComponent : public Component { void on_factory_reset(std::function callback); void defer_factory_reset_external_callback(); + const std::string &get_use_address() const; + void set_use_address(const std::string &use_address); + protected: std::optional get_omr_address_(InstanceLock &lock); bool teardown_started_{false}; bool teardown_complete_{false}; std::function factory_reset_external_callback_; + std::string use_address_; }; extern OpenThreadComponent *global_openthread_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) diff --git a/esphome/core/__init__.py b/esphome/core/__init__.py index 2d49d29c5e..fed5265d6b 100644 --- a/esphome/core/__init__.py +++ b/esphome/core/__init__.py @@ -636,11 +636,9 @@ class EsphomeCore: if self.config is None: raise ValueError("Config has not been loaded yet") - if CONF_WIFI in self.config: - return self.config[CONF_WIFI][CONF_USE_ADDRESS] - - if CONF_ETHERNET in self.config: - return self.config[CONF_ETHERNET][CONF_USE_ADDRESS] + for network_type in (CONF_WIFI, CONF_ETHERNET, CONF_OPENTHREAD): + if network_type in self.config: + return self.config[network_type][CONF_USE_ADDRESS] if CONF_OPENTHREAD in self.config: return f"{self.name}.local" diff --git a/tests/unit_tests/test_core.py b/tests/unit_tests/test_core.py index 41114ae18b..92b60efd93 100644 --- a/tests/unit_tests/test_core.py +++ b/tests/unit_tests/test_core.py @@ -571,9 +571,11 @@ class TestEsphomeCore: assert target.address == "4.3.2.1" def test_address__openthread(self, target): - target.name = "test-device" target.config = {} - target.config[const.CONF_OPENTHREAD] = {} + target.config[const.CONF_OPENTHREAD] = { + const.CONF_USE_ADDRESS: "test-device.local" + } + target.name = "test-device" assert target.address == "test-device.local"