mirror of
https://github.com/esphome/esphome.git
synced 2025-10-31 23:21:54 +00:00
wip
This commit is contained in:
@@ -344,62 +344,49 @@ class EsphomePortCommandWebSocket(EsphomeCommandWebSocket):
|
|||||||
entry.name,
|
entry.name,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Build cache entries for any cached addresses we have
|
def add_cache_entry(
|
||||||
# First check entry.address (use_address)
|
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 := entry.address:
|
||||||
if use_address.endswith(".local"):
|
if use_address.endswith(".local"):
|
||||||
# Check mDNS cache for .local addresses
|
# mDNS cache for .local addresses
|
||||||
if mdns := dashboard.mdns_status:
|
if (mdns := dashboard.mdns_status) and (
|
||||||
cached = mdns.get_cached_addresses(use_address)
|
cached := mdns.get_cached_addresses(use_address)
|
||||||
_LOGGER.debug(
|
):
|
||||||
"mDNS cache lookup for address %s: %s", use_address, cached
|
_LOGGER.debug("mDNS cache hit for %s: %s", use_address, cached)
|
||||||
)
|
add_cache_entry(use_address, cached, "mdns")
|
||||||
if cached:
|
# DNS cache for non-.local addresses
|
||||||
normalized = use_address.rstrip(".").lower()
|
elif cached := dashboard.dns_cache.get_cached_addresses(
|
||||||
cache_args.extend(
|
use_address, now
|
||||||
[
|
):
|
||||||
"--mdns-address-cache",
|
_LOGGER.debug("DNS cache hit for %s: %s", use_address, cached)
|
||||||
f"{normalized}={','.join(sort_ip_addresses(cached))}",
|
add_cache_entry(use_address, cached, "dns")
|
||||||
]
|
|
||||||
)
|
|
||||||
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))}",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
# 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
|
# 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:
|
||||||
if (
|
|
||||||
entry.name and not use_address
|
|
||||||
): # Only if we didn't already check address
|
|
||||||
# Try mDNS cache with .local suffix
|
|
||||||
mdns_name = (
|
mdns_name = (
|
||||||
f"{entry.name}.local"
|
f"{entry.name}.local"
|
||||||
if not entry.name.endswith(".local")
|
if not entry.name.endswith(".local")
|
||||||
else entry.name
|
else entry.name
|
||||||
)
|
)
|
||||||
if mdns := dashboard.mdns_status:
|
if (mdns := dashboard.mdns_status) and (
|
||||||
cached = mdns.get_cached_addresses(mdns_name)
|
cached := mdns.get_cached_addresses(mdns_name)
|
||||||
_LOGGER.debug("mDNS cache lookup for %s: %s", mdns_name, cached)
|
):
|
||||||
if cached:
|
_LOGGER.debug("mDNS cache hit for %s: %s", mdns_name, cached)
|
||||||
normalized = mdns_name.rstrip(".").lower()
|
add_cache_entry(mdns_name, cached, "mdns")
|
||||||
cache_args.extend(
|
|
||||||
[
|
|
||||||
"--mdns-address-cache",
|
|
||||||
f"{normalized}={','.join(sort_ip_addresses(cached))}",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
# Cache arguments must come before the subcommand
|
# Cache arguments must come before the subcommand
|
||||||
cmd = [*DASHBOARD_COMMAND, *cache_args, *args, config_file, "--device", port]
|
cmd = [*DASHBOARD_COMMAND, *cache_args, *args, config_file, "--device", port]
|
||||||
|
|||||||
21
tests/dashboard/conftest.py
Normal file
21
tests/dashboard/conftest.py
Normal file
@@ -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
|
||||||
@@ -8,22 +8,9 @@ import pytest
|
|||||||
import pytest_asyncio
|
import pytest_asyncio
|
||||||
from zeroconf import AddressResolver, IPVersion
|
from zeroconf import AddressResolver, IPVersion
|
||||||
|
|
||||||
from esphome.dashboard.core import ESPHomeDashboard
|
|
||||||
from esphome.dashboard.status.mdns import MDNSStatus
|
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
|
@pytest_asyncio.fixture
|
||||||
async def mdns_status(mock_dashboard: Mock) -> MDNSStatus:
|
async def mdns_status(mock_dashboard: Mock) -> MDNSStatus:
|
||||||
"""Create an MDNSStatus instance in async context."""
|
"""Create an MDNSStatus instance in async context."""
|
||||||
|
|||||||
Reference in New Issue
Block a user