Added an option to disable mDNS (#1716)

* Added an option to disable mDNS

* Fixed linter issues

* Moved the enable_mdns option to WiFi and Ethernet components

* extracted common method for add mdns library

* lint

Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
This commit is contained in:
Farzad E 2021-05-06 08:31:42 -07:00 committed by GitHub
parent f7232b199a
commit 2225594ee8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 45 additions and 15 deletions

View File

@ -1,6 +1,7 @@
from esphome import pins from esphome import pins
import esphome.config_validation as cv import esphome.config_validation as cv
import esphome.codegen as cg import esphome.codegen as cg
from esphome.components.network import add_mdns_library
from esphome.const import ( from esphome.const import (
CONF_DOMAIN, CONF_DOMAIN,
CONF_ID, CONF_ID,
@ -9,6 +10,7 @@ from esphome.const import (
CONF_TYPE, CONF_TYPE,
CONF_USE_ADDRESS, CONF_USE_ADDRESS,
ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP32,
CONF_ENABLE_MDNS,
CONF_GATEWAY, CONF_GATEWAY,
CONF_SUBNET, CONF_SUBNET,
CONF_DNS1, 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_PHY_ADDR, default=0): cv.int_range(min=0, max=31),
cv.Optional(CONF_POWER_PIN): pins.gpio_output_pin_schema, cv.Optional(CONF_POWER_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_MANUAL_IP): MANUAL_IP_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_DOMAIN, default=".local"): cv.domain_name,
cv.Optional(CONF_USE_ADDRESS): cv.string_strict, cv.Optional(CONF_USE_ADDRESS): cv.string_strict,
cv.Optional("hostname"): cv.invalid( 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(var.set_manual_ip(manual_ip(config[CONF_MANUAL_IP])))
cg.add_define("USE_ETHERNET") cg.add_define("USE_ETHERNET")
if config[CONF_ENABLE_MDNS]:
add_mdns_library()

View File

@ -33,7 +33,9 @@ void EthernetComponent::setup() {
this->start_connect_(); this->start_connect_();
#ifdef USE_MDNS
network_setup_mdns(); network_setup_mdns();
#endif
} }
void EthernetComponent::loop() { void EthernetComponent::loop() {
const uint32_t now = millis(); const uint32_t now = millis();

View File

@ -1,2 +1,13 @@
# Dummy package to allow components to depend on network # Dummy package to allow components to depend on network
import esphome.codegen as cg
from esphome.core import CORE
CODEOWNERS = ["@esphome/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)

View File

@ -2,6 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import automation from esphome import automation
from esphome.automation import Condition from esphome.automation import Condition
from esphome.components.network import add_mdns_library
from esphome.const import ( from esphome.const import (
CONF_AP, CONF_AP,
CONF_BSSID, CONF_BSSID,
@ -22,6 +23,7 @@ from esphome.const import (
CONF_STATIC_IP, CONF_STATIC_IP,
CONF_SUBNET, CONF_SUBNET,
CONF_USE_ADDRESS, CONF_USE_ADDRESS,
CONF_ENABLE_MDNS,
CONF_PRIORITY, CONF_PRIORITY,
CONF_IDENTITY, CONF_IDENTITY,
CONF_CERTIFICATE_AUTHORITY, CONF_CERTIFICATE_AUTHORITY,
@ -187,6 +189,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_MANUAL_IP): STA_MANUAL_IP_SCHEMA, cv.Optional(CONF_MANUAL_IP): STA_MANUAL_IP_SCHEMA,
cv.Optional(CONF_EAP): EAP_AUTH_SCHEMA, cv.Optional(CONF_EAP): EAP_AUTH_SCHEMA,
cv.Optional(CONF_AP): WIFI_NETWORK_AP, 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_DOMAIN, default=".local"): cv.domain_name,
cv.Optional( cv.Optional(
CONF_REBOOT_TIMEOUT, default="15min" CONF_REBOOT_TIMEOUT, default="15min"
@ -298,6 +301,9 @@ def to_code(config):
cg.add_define("USE_WIFI") cg.add_define("USE_WIFI")
if config[CONF_ENABLE_MDNS]:
add_mdns_library()
# Register at end for OTA safe mode # Register at end for OTA safe mode
yield cg.register_component(var, config) yield cg.register_component(var, config)

View File

@ -62,7 +62,7 @@ void WiFiComponent::setup() {
} }
this->wifi_apply_hostname_(); this->wifi_apply_hostname_();
#ifdef ARDUINO_ARCH_ESP32 #if defined(ARDUINO_ARCH_ESP32) && defined(USE_MDNS)
network_setup_mdns(); network_setup_mdns();
#endif #endif
} }
@ -171,7 +171,7 @@ void WiFiComponent::setup_ap_config_() {
this->ap_setup_ = this->wifi_start_ap_(this->ap_); this->ap_setup_ = this->wifi_start_ap_(this->ap_);
ESP_LOGCONFIG(TAG, " IP Address: %s", this->wifi_soft_ap_ip().toString().c_str()); 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); network_setup_mdns(this->wifi_soft_ap_ip(), 1);
#endif #endif
@ -466,7 +466,7 @@ void WiFiComponent::check_connecting_finished() {
ESP_LOGD(TAG, "Disabling AP..."); ESP_LOGD(TAG, "Disabling AP...");
this->wifi_mode_({}, false); this->wifi_mode_({}, false);
} }
#ifdef ARDUINO_ARCH_ESP8266 #if defined(ARDUINO_ARCH_ESP8266) && defined(USE_MDNS)
network_setup_mdns(this->wifi_sta_ip_(), 0); network_setup_mdns(this->wifi_sta_ip_(), 0);
#endif #endif
this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTED; this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTED;

View File

@ -183,12 +183,14 @@ CONF_ECHO_PIN = "echo_pin"
CONF_EFFECT = "effect" CONF_EFFECT = "effect"
CONF_EFFECTS = "effects" CONF_EFFECTS = "effects"
CONF_ELSE = "else" CONF_ELSE = "else"
CONF_ENABLE_MDNS = "enable_mdns"
CONF_ENABLE_PIN = "enable_pin" CONF_ENABLE_PIN = "enable_pin"
CONF_ENABLE_TIME = "enable_time" CONF_ENABLE_TIME = "enable_time"
CONF_ENERGY = "energy" CONF_ENERGY = "energy"
CONF_ENTITY_ID = "entity_id" CONF_ENTITY_ID = "entity_id"
CONF_ESP8266_RESTORE_FROM_FLASH = "esp8266_restore_from_flash" CONF_ESP8266_RESTORE_FROM_FLASH = "esp8266_restore_from_flash"
CONF_ESPHOME = "esphome" CONF_ESPHOME = "esphome"
CONF_ETHERNET = "ethernet"
CONF_EVENT = "event" CONF_EVENT = "event"
CONF_EXPIRE_AFTER = "expire_after" CONF_EXPIRE_AFTER = "expire_after"
CONF_EXTERNAL_VCC = "external_vcc" CONF_EXTERNAL_VCC = "external_vcc"

View File

@ -16,6 +16,7 @@ from esphome.const import (
CONF_COMMENT, CONF_COMMENT,
CONF_ESPHOME, CONF_ESPHOME,
CONF_USE_ADDRESS, CONF_USE_ADDRESS,
CONF_ETHERNET,
CONF_WIFI, CONF_WIFI,
) )
from esphome.helpers import ensure_unique_string, is_hassio from esphome.helpers import ensure_unique_string, is_hassio
@ -580,8 +581,8 @@ class EsphomeCore:
if "wifi" in self.config: if "wifi" in self.config:
return self.config[CONF_WIFI][CONF_USE_ADDRESS] return self.config[CONF_WIFI][CONF_USE_ADDRESS]
if "ethernet" in self.config: if CONF_ETHERNET in self.config:
return self.config["ethernet"][CONF_USE_ADDRESS] return self.config[CONF_ETHERNET][CONF_USE_ADDRESS]
return None return None

View File

@ -16,12 +16,14 @@
#include "esphome/components/ethernet/ethernet_component.h" #include "esphome/components/ethernet/ethernet_component.h"
#endif #endif
#ifdef USE_MDNS
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
#include <ESPmDNS.h> #include <ESPmDNS.h>
#endif #endif
#ifdef ARDUINO_ARCH_ESP8266 #ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#endif #endif
#endif
namespace esphome { namespace esphome {
@ -39,7 +41,7 @@ bool network_is_connected() {
return false; return false;
} }
#ifdef ARDUINO_ARCH_ESP8266 #if defined(ARDUINO_ARCH_ESP8266) && defined(USE_MDNS)
bool mdns_setup; bool mdns_setup;
#endif #endif
@ -47,6 +49,7 @@ bool mdns_setup;
static const uint8_t WEBSERVER_PORT = 80; static const uint8_t WEBSERVER_PORT = 80;
#endif #endif
#ifdef USE_MDNS
#ifdef ARDUINO_ARCH_ESP8266 #ifdef ARDUINO_ARCH_ESP8266
void network_setup_mdns(IPAddress address, int interface) { void network_setup_mdns(IPAddress address, int interface) {
// Latest arduino framework breaks mDNS for AP 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); MDNS.addService("prometheus-http", "tcp", WEBSERVER_PORT);
#endif #endif
} }
#endif
void network_tick_mdns() { void network_tick_mdns() {
#ifdef ARDUINO_ARCH_ESP8266 #if defined(ARDUINO_ARCH_ESP8266) && defined(USE_MDNS)
if (mdns_setup) if (mdns_setup)
MDNS.update(); MDNS.update();
#endif #endif

View File

@ -17,6 +17,7 @@ void network_setup_mdns(IPAddress address, int interface);
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
void network_setup_mdns(); void network_setup_mdns();
#endif #endif
void network_tick_mdns(); void network_tick_mdns();
} // namespace esphome } // namespace esphome

View File

@ -308,12 +308,6 @@ def to_code(config):
cg.add_build_flag("-fno-exceptions") cg.add_build_flag("-fno-exceptions")
# Libraries # 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]: for lib in config[CONF_LIBRARIES]:
if "@" in lib: if "@" in lib:
name, vers = lib.split("@", 1) name, vers = lib.split("@", 1)

View File

@ -71,6 +71,7 @@ wifi:
password: '' password: ''
channel: 14 channel: 14
bssid: 'A1:63:95:47:D3:1D' bssid: 'A1:63:95:47:D3:1D'
enable_mdns: true
manual_ip: manual_ip:
static_ip: 192.168.178.230 static_ip: 192.168.178.230
gateway: 192.168.178.1 gateway: 192.168.178.1

View File

@ -14,6 +14,7 @@ ethernet:
clk_mode: GPIO0_IN clk_mode: GPIO0_IN
phy_addr: 0 phy_addr: 0
power_pin: GPIO25 power_pin: GPIO25
enable_mdns: false
manual_ip: manual_ip:
static_ip: 192.168.178.56 static_ip: 192.168.178.56
gateway: 192.168.178.1 gateway: 192.168.178.1

View File

@ -503,13 +503,13 @@ class TestEsphomeCore:
def test_address__wifi(self, target): def test_address__wifi(self, target):
target.config = {} target.config = {}
target.config[const.CONF_WIFI] = {const.CONF_USE_ADDRESS: "1.2.3.4"} 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" assert target.address == "1.2.3.4"
def test_address__ethernet(self, target): def test_address__ethernet(self, target):
target.config = {} 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" assert target.address == "4.3.2.1"