mirror of
https://github.com/esphome/esphome.git
synced 2025-10-17 09:13:45 +01:00
[mdns][openthread] Use StaticVector for services storage with compile-time capacity (#10976)
This commit is contained in:
@@ -17,6 +17,11 @@ from esphome.coroutine import CoroPriority
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
DEPENDENCIES = ["network"]
|
||||
|
||||
# Components that create mDNS services at runtime
|
||||
# IMPORTANT: If you add a new component here, you must also update the corresponding
|
||||
# #ifdef blocks in mdns_component.cpp compile_records_() method
|
||||
COMPONENTS_WITH_MDNS_SERVICES = ("api", "prometheus", "web_server")
|
||||
|
||||
mdns_ns = cg.esphome_ns.namespace("mdns")
|
||||
MDNSComponent = mdns_ns.class_("MDNSComponent", cg.Component)
|
||||
MDNSTXTRecord = mdns_ns.struct("MDNSTXTRecord")
|
||||
@@ -91,12 +96,20 @@ async def to_code(config):
|
||||
|
||||
cg.add_define("USE_MDNS")
|
||||
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
# Calculate compile-time service count
|
||||
service_count = sum(
|
||||
1 for key in COMPONENTS_WITH_MDNS_SERVICES if key in CORE.config
|
||||
) + len(config[CONF_SERVICES])
|
||||
|
||||
if config[CONF_SERVICES]:
|
||||
cg.add_define("USE_MDNS_EXTRA_SERVICES")
|
||||
|
||||
# Ensure at least 1 service (fallback service)
|
||||
cg.add_define("MDNS_SERVICE_COUNT", max(1, service_count))
|
||||
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
for service in config[CONF_SERVICES]:
|
||||
txt = [
|
||||
cg.StructInitializer(
|
||||
|
Reference in New Issue
Block a user