From 854a4158050aac61b2d8649334cf5fde470c3aca Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 11 Sep 2025 19:17:54 -0500 Subject: [PATCH] wip --- esphome/dashboard/web_server.py | 81 ++++++++++++----------------- tests/dashboard/conftest.py | 21 ++++++++ tests/dashboard/status/test_mdns.py | 13 ----- 3 files changed, 55 insertions(+), 60 deletions(-) create mode 100644 tests/dashboard/conftest.py diff --git a/esphome/dashboard/web_server.py b/esphome/dashboard/web_server.py index 2ab449b8da..9524611d76 100644 --- a/esphome/dashboard/web_server.py +++ b/esphome/dashboard/web_server.py @@ -344,62 +344,49 @@ class EsphomePortCommandWebSocket(EsphomeCommandWebSocket): entry.name, ) - # Build cache entries for any cached addresses we have - # First check entry.address (use_address) + def add_cache_entry( + hostname: str, addresses: list[str], cache_type: str + ) -> None: + """Add a cache entry to the command arguments.""" + if not addresses: + return + normalized = hostname.rstrip(".").lower() + cache_args.extend( + [ + f"--{cache_type}-address-cache", + f"{normalized}={','.join(sort_ip_addresses(addresses))}", + ] + ) + + # Check entry.address for cached addresses if use_address := entry.address: if use_address.endswith(".local"): - # Check mDNS cache for .local addresses - if mdns := dashboard.mdns_status: - cached = mdns.get_cached_addresses(use_address) - _LOGGER.debug( - "mDNS cache lookup for address %s: %s", use_address, cached - ) - if cached: - normalized = use_address.rstrip(".").lower() - cache_args.extend( - [ - "--mdns-address-cache", - f"{normalized}={','.join(sort_ip_addresses(cached))}", - ] - ) - else: - # Check DNS cache for non-.local addresses - cached = dashboard.dns_cache.get_cached_addresses(use_address, now) - _LOGGER.debug( - "DNS cache lookup for address %s: %s", use_address, cached - ) - if cached: - normalized = use_address.rstrip(".").lower() - cache_args.extend( - [ - "--dns-address-cache", - f"{normalized}={','.join(sort_ip_addresses(cached))}", - ] - ) + # mDNS cache for .local addresses + if (mdns := dashboard.mdns_status) and ( + cached := mdns.get_cached_addresses(use_address) + ): + _LOGGER.debug("mDNS cache hit for %s: %s", use_address, cached) + add_cache_entry(use_address, cached, "mdns") + # DNS cache for non-.local addresses + elif cached := dashboard.dns_cache.get_cached_addresses( + use_address, now + ): + _LOGGER.debug("DNS cache hit for %s: %s", use_address, cached) + add_cache_entry(use_address, cached, "dns") - # Also check entry.name for cache entries + # Check entry.name if we haven't already cached via address # For mDNS devices, entry.name typically doesn't have .local suffix - # but we should check both with and without .local - if ( - entry.name and not use_address - ): # Only if we didn't already check address - # Try mDNS cache with .local suffix + if entry.name and not use_address: mdns_name = ( f"{entry.name}.local" if not entry.name.endswith(".local") else entry.name ) - if mdns := dashboard.mdns_status: - cached = mdns.get_cached_addresses(mdns_name) - _LOGGER.debug("mDNS cache lookup for %s: %s", mdns_name, cached) - if cached: - normalized = mdns_name.rstrip(".").lower() - cache_args.extend( - [ - "--mdns-address-cache", - f"{normalized}={','.join(sort_ip_addresses(cached))}", - ] - ) + if (mdns := dashboard.mdns_status) and ( + cached := mdns.get_cached_addresses(mdns_name) + ): + _LOGGER.debug("mDNS cache hit for %s: %s", mdns_name, cached) + add_cache_entry(mdns_name, cached, "mdns") # Cache arguments must come before the subcommand cmd = [*DASHBOARD_COMMAND, *cache_args, *args, config_file, "--device", port] diff --git a/tests/dashboard/conftest.py b/tests/dashboard/conftest.py new file mode 100644 index 0000000000..358be1bf5d --- /dev/null +++ b/tests/dashboard/conftest.py @@ -0,0 +1,21 @@ +"""Common fixtures for dashboard tests.""" + +from __future__ import annotations + +from unittest.mock import Mock + +import pytest + +from esphome.dashboard.core import ESPHomeDashboard + + +@pytest.fixture +def mock_dashboard() -> Mock: + """Create a mock dashboard.""" + dashboard = Mock(spec=ESPHomeDashboard) + dashboard.entries = Mock() + dashboard.entries.async_all.return_value = [] + dashboard.stop_event = Mock() + dashboard.stop_event.is_set.return_value = True + dashboard.ping_request = Mock() + return dashboard diff --git a/tests/dashboard/status/test_mdns.py b/tests/dashboard/status/test_mdns.py index ad66eed2c2..7130c2c73a 100644 --- a/tests/dashboard/status/test_mdns.py +++ b/tests/dashboard/status/test_mdns.py @@ -8,22 +8,9 @@ import pytest import pytest_asyncio from zeroconf import AddressResolver, IPVersion -from esphome.dashboard.core import ESPHomeDashboard from esphome.dashboard.status.mdns import MDNSStatus -@pytest.fixture -def mock_dashboard() -> Mock: - """Create a mock dashboard.""" - dashboard = Mock(spec=ESPHomeDashboard) - dashboard.entries = Mock() - dashboard.entries.async_all.return_value = [] - dashboard.stop_event = Mock() - dashboard.stop_event.is_set.return_value = True - dashboard.ping_request = Mock() - return dashboard - - @pytest_asyncio.fixture async def mdns_status(mock_dashboard: Mock) -> MDNSStatus: """Create an MDNSStatus instance in async context."""