From e63bda7e7660421e816db77c6a39b6827d6d28f9 Mon Sep 17 00:00:00 2001 From: imonlinux <39863321+imonlinux@users.noreply.github.com> Date: Wed, 19 Nov 2025 18:46:05 -0800 Subject: [PATCH] Update zeroconf.py Uses the new util to ensure MDNS matches the device ID. --- linux_voice_assistant/zeroconf.py | 37 ++++++++++++++----------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/linux_voice_assistant/zeroconf.py b/linux_voice_assistant/zeroconf.py index 2477f0c..3987872 100644 --- a/linux_voice_assistant/zeroconf.py +++ b/linux_voice_assistant/zeroconf.py @@ -2,9 +2,10 @@ import logging import socket -import uuid from typing import Optional +from .util import get_mac_address + _LOGGER = logging.getLogger(__name__) try: @@ -21,21 +22,27 @@ class HomeAssistantZeroconf: self, port: int, name: Optional[str] = None, host: Optional[str] = None ) -> None: self.port = port - self.name = name or _get_mac_address() + # Use the stable MAC address + self.mac_address = get_mac_address() + self.name = name or self.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) + try: + 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] + test_sock.close() + _LOGGER.debug("Detected IP: %s", host) + except Exception: + _LOGGER.warning("Could not detect IP address, mDNS might fail.") + host = "127.0.0.1" 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.", @@ -43,22 +50,12 @@ class HomeAssistantZeroconf: port=self.port, properties={ "version": "2025.9.0", - "mac": _get_mac_address(), + "mac": self.mac_address, "board": "host", "platform": "HOST", - "network": "ethernet", # or "wifi" + "network": "ethernet", }, 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 - ] - )