Add zeroconf discovery

This commit is contained in:
Michael Hansen
2025-09-18 10:52:56 -05:00
parent ed16301767
commit 7a4b3e661a
5 changed files with 77 additions and 7 deletions
+64
View File
@@ -0,0 +1,64 @@
"""Runs mDNS zeroconf service for Home Assistant discovery."""
import logging
import socket
import uuid
from typing import Optional
_LOGGER = logging.getLogger(__name__)
try:
from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf
except ImportError:
_LOGGER.fatal("pip install zeroconf")
raise
MDNS_TARGET_IP = "224.0.0.251"
class HomeAssistantZeroconf:
def __init__(
self, port: int, name: Optional[str] = None, host: Optional[str] = None
) -> None:
self.port = port
self.name = name or _get_mac_address()
if not host:
test_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
test_sock.setblocking(False)
test_sock.connect((MDNS_TARGET_IP, 1))
host = test_sock.getsockname()[0]
_LOGGER.debug("Detected IP: %s", host)
assert host
self.host = host
self._aiozc = AsyncZeroconf()
async def register_server(self) -> None:
service_info = AsyncServiceInfo(
"_esphomelib._tcp.local.",
f"{self.name}._esphomelib._tcp.local.",
addresses=[socket.inet_aton(self.host)],
port=self.port,
properties={
"version": "2025.9.0",
"mac": _get_mac_address(),
"board": "host",
"platform": "HOST",
"network": "ethernet", # or "wifi"
},
server=f"{self.name}.local.",
)
await self._aiozc.async_register_service(service_info)
_LOGGER.debug("Zeroconf discovery enabled: %s", service_info)
def _get_mac_address() -> str:
"""Return MAC address formatted as hex with no colons."""
return "".join(
# pylint: disable=consider-using-f-string
["{:02x}".format((uuid.getnode() >> ele) & 0xFF) for ele in range(0, 8 * 6, 8)][
::-1
]
)