Implementing mDNS into custom addon

Hi all,
I’m currently working on developing a custom addon that acts like a broker (http+mqtt) for Meross devices. For that purpose, I’d like to make that mqtt/http api services to be “advertised” on the local lan with mDNS. I tried to do so by setting host_network=“true” (within the config.json) and added avahi daemon on my addon. Despite from the logs it looks like it’s working, it is not (I am unable to see the entry with bonjour clients on the LAN). From the logs I can see the following log line being printed:
WARNING: Detected another IPv4 mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended.

My question is: how can I implement mDNS discovery from within my custom addon? Is there any example you can provide?

Thanks a lot in advance!

Hi there,

Did you manage to advertise the MQTT service? I’m trying to get the Mosquitto broker advertised. It seems we must use the HA internal helper APIs to access the running Zeroconf and AsyncZeroconf instances.

Thanks

Here is a simple python script that gives you access to the zeroconf instance of HA for anyone interested. To run it, you need HACS and Pyscript integration.

import socket
from homeassistant.components import zeroconf
from zeroconf.asyncio import AsyncServiceInfo

def register_service(type, name, ip, port):
    aiozc = zeroconf.async_get_async_instance(hass)
    info = AsyncServiceInfo(
        type_=type + "._tcp.local.",
        name=name + "._mqtt._tcp.local.",
        addresses=[socket.inet_aton(ip)],
        port=port,
        properties={}
    )
    aiozc.async_register_service(info)


register_service("_mqtt", "MQTT Host", "192.168.100.200", 1883)

This can be used to advertise any service.

I hope this helps.