mirror of
https://github.com/esphome/esphome.git
synced 2026-09-17 18:18:43 +00:00
[sendspin] Start mDNS service disabled, enable once server is running (#19326)
This commit is contained in:
@@ -221,6 +221,10 @@ void MDNSComponent::compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUN
|
||||
sendspin_service.proto = MDNS_STR(SERVICE_TCP);
|
||||
sendspin_service.port = []() -> uint16_t { return USE_SENDSPIN_PORT; };
|
||||
sendspin_service.txt_records = {{MDNS_STR(TXT_SENDSPIN_PATH), MDNS_STR(VALUE_SENDSPIN_PATH)}};
|
||||
#ifdef USE_MDNS_SUPPORTS_ENABLE_DISABLE
|
||||
// Starts disabled; the sendspin hub enables it once its server is running
|
||||
sendspin_service.enabled = false;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
|
||||
@@ -2,7 +2,7 @@ from dataclasses import dataclass, field
|
||||
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32, network, psram, socket, wifi
|
||||
from esphome.components import esp32, mdns, network, psram, socket, wifi
|
||||
from esphome.components.const import CONF_MANUFACTURER
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
@@ -11,6 +11,7 @@ from esphome.const import (
|
||||
CONF_FORMAT,
|
||||
CONF_HEIGHT,
|
||||
CONF_ID,
|
||||
CONF_MDNS,
|
||||
CONF_MODEL,
|
||||
CONF_NAME,
|
||||
CONF_PROJECT,
|
||||
@@ -285,6 +286,11 @@ async def to_code(config: ConfigType) -> None:
|
||||
|
||||
cg.add_define("USE_SENDSPIN", True) # for MDNS
|
||||
|
||||
# Service starts disabled and the hub enables it; always advertised where unsupported
|
||||
if mdns.request_service_enable_disable():
|
||||
mdns_var = await cg.get_variable(CORE.config[CONF_MDNS][CONF_ID])
|
||||
cg.add(var.set_mdns(mdns_var))
|
||||
|
||||
data = _get_data()
|
||||
|
||||
# The color role is not yet wired up in ESPHome; disable it in the library for now.
|
||||
|
||||
@@ -21,6 +21,10 @@ namespace esphome::sendspin_ {
|
||||
|
||||
static const char *const TAG = "sendspin.hub";
|
||||
|
||||
#ifdef USE_MDNS_SUPPORTS_ENABLE_DISABLE
|
||||
static constexpr uint32_t MDNS_ENABLE_RETRY_MS = 1000;
|
||||
#endif
|
||||
|
||||
#ifdef USE_SENDSPIN_ARTWORK
|
||||
// Indexed by the library enums, which start at zero and are contiguous.
|
||||
static const char *const IMAGE_SOURCE_NAMES[] = {"ALBUM", "ARTIST", "NONE"};
|
||||
@@ -69,7 +73,21 @@ void SendspinHub::setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void SendspinHub::loop() { this->client_->loop(); }
|
||||
void SendspinHub::loop() {
|
||||
this->client_->loop();
|
||||
|
||||
#ifdef USE_MDNS_SUPPORTS_ENABLE_DISABLE
|
||||
// mdns sets up after this hub, so the service is enabled here once mdns is ready. A failed enable retries,
|
||||
// rate limited so a persistent failure does not flood the log or block on the mdns task every loop pass.
|
||||
if (!this->mdns_advertised_ && this->mdns_->is_ready()) {
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (this->mdns_enable_attempt_ms_ == 0 || now - this->mdns_enable_attempt_ms_ >= MDNS_ENABLE_RETRY_MS) {
|
||||
this->mdns_enable_attempt_ms_ = now;
|
||||
this->mdns_advertised_ = this->mdns_->set_service_enabled("_sendspin", "_tcp", true);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void SendspinHub::dump_config() {
|
||||
char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
|
||||
|
||||
@@ -10,6 +10,10 @@
|
||||
#include "esphome/core/preferences.h"
|
||||
#include "esphome/core/version.h"
|
||||
|
||||
#ifdef USE_MDNS_SUPPORTS_ENABLE_DISABLE
|
||||
#include "esphome/components/mdns/mdns_component.h"
|
||||
#endif
|
||||
|
||||
#include <sendspin/client.h>
|
||||
#include <sendspin/config.h>
|
||||
#include <sendspin/types.h>
|
||||
@@ -135,6 +139,10 @@ class SendspinHub final : public Component,
|
||||
void set_model(const char *model) { this->model_ = model; }
|
||||
void set_firmware_version(const char *firmware_version) { this->firmware_version_ = firmware_version; }
|
||||
|
||||
#ifdef USE_MDNS_SUPPORTS_ENABLE_DISABLE
|
||||
void set_mdns(mdns::MDNSComponent *mdns) { this->mdns_ = mdns; }
|
||||
#endif
|
||||
|
||||
// --- Sendspin role specific methods ---
|
||||
|
||||
#ifdef USE_SENDSPIN_ARTWORK
|
||||
@@ -287,6 +295,12 @@ class SendspinHub final : public Component,
|
||||
const char *manufacturer_{"ESPHome"};
|
||||
const char *model_{nullptr}; // nullptr reports the device name instead
|
||||
const char *firmware_version_{ESPHOME_VERSION};
|
||||
|
||||
#ifdef USE_MDNS_SUPPORTS_ENABLE_DISABLE
|
||||
mdns::MDNSComponent *mdns_{nullptr};
|
||||
uint32_t mdns_enable_attempt_ms_{0};
|
||||
bool mdns_advertised_{false};
|
||||
#endif
|
||||
};
|
||||
|
||||
/// @brief Base class for all sendspin subcomponents.
|
||||
|
||||
Reference in New Issue
Block a user