From 765e641d08dc30db15501eff7a66f33ec06314f7 Mon Sep 17 00:00:00 2001 From: richardweinberger Date: Mon, 14 Dec 2020 21:14:38 +0100 Subject: [PATCH] Fix mDNS webserver port and expose prometheus service (#1389) * Expose right webserver port using mDNS. 80 is the default value but can be changed in the config file. Add a new define for the port. Signed-off-by: Richard Weinberger * Expose prometheus service via mDNS That way prometheus auto discovery features can find us. Signed-off-by: Richard Weinberger --- esphome/components/prometheus/__init__.py | 2 ++ esphome/components/web_server/__init__.py | 1 + esphome/core/util.cpp | 11 +++++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/esphome/components/prometheus/__init__.py b/esphome/components/prometheus/__init__.py index d015af9f78..9c3deef73d 100644 --- a/esphome/components/prometheus/__init__.py +++ b/esphome/components/prometheus/__init__.py @@ -18,5 +18,7 @@ CONFIG_SCHEMA = cv.Schema({ def to_code(config): paren = yield cg.get_variable(config[CONF_WEB_SERVER_BASE_ID]) + cg.add_define('USE_PROMETHEUS') + var = cg.new_Pvariable(config[CONF_ID], paren) yield cg.register_component(var, config) diff --git a/esphome/components/web_server/__init__.py b/esphome/components/web_server/__init__.py index 2f0d179eba..069b0a3895 100644 --- a/esphome/components/web_server/__init__.py +++ b/esphome/components/web_server/__init__.py @@ -36,6 +36,7 @@ def to_code(config): yield cg.register_component(var, config) cg.add(paren.set_port(config[CONF_PORT])) + cg.add_define('WEBSERVER_PORT', config[CONF_PORT]) cg.add(var.set_css_url(config[CONF_CSS_URL])) cg.add(var.set_js_url(config[CONF_JS_URL])) if CONF_AUTH in config: diff --git a/esphome/core/util.cpp b/esphome/core/util.cpp index 867ba8421b..a701b8013c 100644 --- a/esphome/core/util.cpp +++ b/esphome/core/util.cpp @@ -43,6 +43,10 @@ bool network_is_connected() { bool mdns_setup; #endif +#ifndef WEBSERVER_PORT +static const uint8_t WEBSERVER_PORT = 80; +#endif + #ifdef ARDUINO_ARCH_ESP8266 void network_setup_mdns(IPAddress address, int interface) { // Latest arduino framework breaks mDNS for AP interface @@ -65,12 +69,15 @@ void network_setup_mdns(IPAddress address, int interface) { MDNS.addServiceTxt("esphomelib", "tcp", "mac", get_mac_address().c_str()); } else { #endif - // Publish "http" service if not using native API. + // Publish "http" service if not using native API nor the webserver component // This is just to have *some* mDNS service so that .local resolution works - MDNS.addService("http", "tcp", 80); + MDNS.addService("http", "tcp", WEBSERVER_PORT); MDNS.addServiceTxt("http", "tcp", "version", ESPHOME_VERSION); #ifdef USE_API } +#endif +#ifdef USE_PROMETHEUS + MDNS.addService("prometheus-http", "tcp", WEBSERVER_PORT); #endif } void network_tick_mdns() {