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
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()

View file

@ -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();

View file

@ -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)

View file

@ -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)

View file

@ -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;

View file

@ -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"

View file

@ -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

View file

@ -16,12 +16,14 @@
#include "esphome/components/ethernet/ethernet_component.h"
#endif
#ifdef USE_MDNS
#ifdef ARDUINO_ARCH_ESP32
#include <ESPmDNS.h>
#endif
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266mDNS.h>
#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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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"