Update zeroconf.py

Uses the new util to ensure MDNS matches the device ID.
This commit is contained in:
imonlinux
2025-11-19 18:46:05 -08:00
committed by GitHub
parent 592d5eb0c3
commit e63bda7e76
+17 -20
View File
@@ -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
]
)