diff --git a/esphome/components/ethernet/__init__.py b/esphome/components/ethernet/__init__.py index ca00f33359..82366eeac2 100644 --- a/esphome/components/ethernet/__init__.py +++ b/esphome/components/ethernet/__init__.py @@ -1,6 +1,7 @@ from esphome import pins import esphome.config_validation as cv import esphome.codegen as cg +from esphome.components.network import add_mdns_library from esphome.const import ( CONF_DOMAIN, CONF_ID, @@ -9,6 +10,7 @@ from esphome.const import ( CONF_TYPE, CONF_USE_ADDRESS, ESP_PLATFORM_ESP32, + CONF_ENABLE_MDNS, CONF_GATEWAY, CONF_SUBNET, CONF_DNS1, @@ -80,6 +82,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_PHY_ADDR, default=0): cv.int_range(min=0, max=31), cv.Optional(CONF_POWER_PIN): pins.gpio_output_pin_schema, cv.Optional(CONF_MANUAL_IP): MANUAL_IP_SCHEMA, + cv.Optional(CONF_ENABLE_MDNS, default=True): cv.boolean, cv.Optional(CONF_DOMAIN, default=".local"): cv.domain_name, cv.Optional(CONF_USE_ADDRESS): cv.string_strict, cv.Optional("hostname"): cv.invalid( @@ -122,3 +125,6 @@ def to_code(config): cg.add(var.set_manual_ip(manual_ip(config[CONF_MANUAL_IP]))) cg.add_define("USE_ETHERNET") + + if config[CONF_ENABLE_MDNS]: + add_mdns_library() diff --git a/esphome/components/ethernet/ethernet_component.cpp b/esphome/components/ethernet/ethernet_component.cpp index 0553d66273..005712420f 100644 --- a/esphome/components/ethernet/ethernet_component.cpp +++ b/esphome/components/ethernet/ethernet_component.cpp @@ -33,7 +33,9 @@ void EthernetComponent::setup() { this->start_connect_(); +#ifdef USE_MDNS network_setup_mdns(); +#endif } void EthernetComponent::loop() { const uint32_t now = millis(); diff --git a/esphome/components/network/__init__.py b/esphome/components/network/__init__.py index 46713d3ffe..5581a2b8d3 100644 --- a/esphome/components/network/__init__.py +++ b/esphome/components/network/__init__.py @@ -1,2 +1,13 @@ # Dummy package to allow components to depend on network +import esphome.codegen as cg +from esphome.core import CORE + CODEOWNERS = ["@esphome/core"] + + +def add_mdns_library(): + cg.add_define("USE_MDNS") + if CORE.is_esp32: + cg.add_library("ESPmDNS", None) + elif CORE.is_esp8266: + cg.add_library("ESP8266mDNS", None) diff --git a/esphome/components/wifi/__init__.py b/esphome/components/wifi/__init__.py index f5b7340ad6..421797eb28 100644 --- a/esphome/components/wifi/__init__.py +++ b/esphome/components/wifi/__init__.py @@ -2,6 +2,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import automation from esphome.automation import Condition +from esphome.components.network import add_mdns_library from esphome.const import ( CONF_AP, CONF_BSSID, @@ -22,6 +23,7 @@ from esphome.const import ( CONF_STATIC_IP, CONF_SUBNET, CONF_USE_ADDRESS, + CONF_ENABLE_MDNS, CONF_PRIORITY, CONF_IDENTITY, CONF_CERTIFICATE_AUTHORITY, @@ -187,6 +189,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_MANUAL_IP): STA_MANUAL_IP_SCHEMA, cv.Optional(CONF_EAP): EAP_AUTH_SCHEMA, cv.Optional(CONF_AP): WIFI_NETWORK_AP, + cv.Optional(CONF_ENABLE_MDNS, default=True): cv.boolean, cv.Optional(CONF_DOMAIN, default=".local"): cv.domain_name, cv.Optional( CONF_REBOOT_TIMEOUT, default="15min" @@ -298,6 +301,9 @@ def to_code(config): cg.add_define("USE_WIFI") + if config[CONF_ENABLE_MDNS]: + add_mdns_library() + # Register at end for OTA safe mode yield cg.register_component(var, config) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index df80c5b109..0a6607852d 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -62,7 +62,7 @@ void WiFiComponent::setup() { } this->wifi_apply_hostname_(); -#ifdef ARDUINO_ARCH_ESP32 +#if defined(ARDUINO_ARCH_ESP32) && defined(USE_MDNS) network_setup_mdns(); #endif } @@ -171,7 +171,7 @@ void WiFiComponent::setup_ap_config_() { this->ap_setup_ = this->wifi_start_ap_(this->ap_); ESP_LOGCONFIG(TAG, " IP Address: %s", this->wifi_soft_ap_ip().toString().c_str()); -#ifdef ARDUINO_ARCH_ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) && defined(USE_MDNS) network_setup_mdns(this->wifi_soft_ap_ip(), 1); #endif @@ -466,7 +466,7 @@ void WiFiComponent::check_connecting_finished() { ESP_LOGD(TAG, "Disabling AP..."); this->wifi_mode_({}, false); } -#ifdef ARDUINO_ARCH_ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) && defined(USE_MDNS) network_setup_mdns(this->wifi_sta_ip_(), 0); #endif this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTED; diff --git a/esphome/const.py b/esphome/const.py index 1d992d12e9..cece8df4e3 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -183,12 +183,14 @@ CONF_ECHO_PIN = "echo_pin" CONF_EFFECT = "effect" CONF_EFFECTS = "effects" CONF_ELSE = "else" +CONF_ENABLE_MDNS = "enable_mdns" CONF_ENABLE_PIN = "enable_pin" CONF_ENABLE_TIME = "enable_time" CONF_ENERGY = "energy" CONF_ENTITY_ID = "entity_id" CONF_ESP8266_RESTORE_FROM_FLASH = "esp8266_restore_from_flash" CONF_ESPHOME = "esphome" +CONF_ETHERNET = "ethernet" CONF_EVENT = "event" CONF_EXPIRE_AFTER = "expire_after" CONF_EXTERNAL_VCC = "external_vcc" diff --git a/esphome/core.py b/esphome/core.py index cf2a07d35f..ce7eaad6fb 100644 --- a/esphome/core.py +++ b/esphome/core.py @@ -16,6 +16,7 @@ from esphome.const import ( CONF_COMMENT, CONF_ESPHOME, CONF_USE_ADDRESS, + CONF_ETHERNET, CONF_WIFI, ) from esphome.helpers import ensure_unique_string, is_hassio @@ -580,8 +581,8 @@ class EsphomeCore: if "wifi" in self.config: return self.config[CONF_WIFI][CONF_USE_ADDRESS] - if "ethernet" in self.config: - return self.config["ethernet"][CONF_USE_ADDRESS] + if CONF_ETHERNET in self.config: + return self.config[CONF_ETHERNET][CONF_USE_ADDRESS] return None diff --git a/esphome/core/util.cpp b/esphome/core/util.cpp index a701b8013c..4e15d142be 100644 --- a/esphome/core/util.cpp +++ b/esphome/core/util.cpp @@ -16,12 +16,14 @@ #include "esphome/components/ethernet/ethernet_component.h" #endif +#ifdef USE_MDNS #ifdef ARDUINO_ARCH_ESP32 #include #endif #ifdef ARDUINO_ARCH_ESP8266 #include #endif +#endif namespace esphome { @@ -39,7 +41,7 @@ bool network_is_connected() { return false; } -#ifdef ARDUINO_ARCH_ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) && defined(USE_MDNS) bool mdns_setup; #endif @@ -47,6 +49,7 @@ bool mdns_setup; static const uint8_t WEBSERVER_PORT = 80; #endif +#ifdef USE_MDNS #ifdef ARDUINO_ARCH_ESP8266 void network_setup_mdns(IPAddress address, int interface) { // Latest arduino framework breaks mDNS for AP interface @@ -80,8 +83,10 @@ void network_setup_mdns(IPAddress address, int interface) { MDNS.addService("prometheus-http", "tcp", WEBSERVER_PORT); #endif } +#endif + void network_tick_mdns() { -#ifdef ARDUINO_ARCH_ESP8266 +#if defined(ARDUINO_ARCH_ESP8266) && defined(USE_MDNS) if (mdns_setup) MDNS.update(); #endif diff --git a/esphome/core/util.h b/esphome/core/util.h index 0e121ef382..8e30211be6 100644 --- a/esphome/core/util.h +++ b/esphome/core/util.h @@ -17,6 +17,7 @@ void network_setup_mdns(IPAddress address, int interface); #ifdef ARDUINO_ARCH_ESP32 void network_setup_mdns(); #endif + void network_tick_mdns(); } // namespace esphome diff --git a/esphome/core_config.py b/esphome/core_config.py index 55d219d86b..1dbe2ec33a 100644 --- a/esphome/core_config.py +++ b/esphome/core_config.py @@ -308,12 +308,6 @@ def to_code(config): cg.add_build_flag("-fno-exceptions") # Libraries - if CORE.is_esp32: - cg.add_library("ESPmDNS", None) - elif CORE.is_esp8266: - cg.add_library("ESP8266WiFi", None) - cg.add_library("ESP8266mDNS", None) - for lib in config[CONF_LIBRARIES]: if "@" in lib: name, vers = lib.split("@", 1) diff --git a/tests/test1.yaml b/tests/test1.yaml index 167c0b4902..c5c39fb52d 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -71,6 +71,7 @@ wifi: password: '' channel: 14 bssid: 'A1:63:95:47:D3:1D' + enable_mdns: true manual_ip: static_ip: 192.168.178.230 gateway: 192.168.178.1 diff --git a/tests/test2.yaml b/tests/test2.yaml index fa21c1044a..34724ee955 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -14,6 +14,7 @@ ethernet: clk_mode: GPIO0_IN phy_addr: 0 power_pin: GPIO25 + enable_mdns: false manual_ip: static_ip: 192.168.178.56 gateway: 192.168.178.1 diff --git a/tests/unit_tests/test_core.py b/tests/unit_tests/test_core.py index fd3f171275..37a4920224 100644 --- a/tests/unit_tests/test_core.py +++ b/tests/unit_tests/test_core.py @@ -503,13 +503,13 @@ class TestEsphomeCore: def test_address__wifi(self, target): target.config = {} target.config[const.CONF_WIFI] = {const.CONF_USE_ADDRESS: "1.2.3.4"} - target.config["ethernet"] = {const.CONF_USE_ADDRESS: "4.3.2.1"} + target.config[const.CONF_ETHERNET] = {const.CONF_USE_ADDRESS: "4.3.2.1"} assert target.address == "1.2.3.4" def test_address__ethernet(self, target): target.config = {} - target.config["ethernet"] = {const.CONF_USE_ADDRESS: "4.3.2.1"} + target.config[const.CONF_ETHERNET] = {const.CONF_USE_ADDRESS: "4.3.2.1"} assert target.address == "4.3.2.1"