diff --git a/esphome/dashboard/dashboard.py b/esphome/dashboard/dashboard.py index 889f5cc0bf..9a8f072237 100644 --- a/esphome/dashboard/dashboard.py +++ b/esphome/dashboard/dashboard.py @@ -644,6 +644,33 @@ def _ping_func(filename, address): return filename, rc == 0 +class PrometheusServiceDiscoveryHandler(BaseHandler): + @authenticated + def get(self): + entries = _list_dashboard_entries() + self.set_header("content-type", "application/json") + sd = [] + for entry in entries: + if entry.web_port is None: + continue + labels = { + "__meta_name": entry.name, + "__meta_esp_platform": entry.target_platform, + "__meta_esphome_version": entry.storage.esphome_version, + } + for integration in entry.storage.loaded_integrations: + labels[f"__meta_integration_{integration}"] = "true" + sd.append( + { + "targets": [ + f"{entry.address}:{entry.web_port}", + ], + "labels": labels, + } + ) + self.write(json.dumps(sd)) + + class MDNSStatusThread(threading.Thread): def run(self): global IMPORT_RESULT @@ -993,6 +1020,7 @@ def make_app(debug=get_bool_env(ENV_DEV)): (f"{rel}import", ImportRequestHandler), (f"{rel}secret_keys", SecretKeysRequestHandler), (f"{rel}rename", EsphomeRenameHandler), + (f"{rel}prometheus-sd", PrometheusServiceDiscoveryHandler), ], **app_settings, )