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 | ||||||
|             if use_address := entry.address: |             ) -> None: | ||||||
|                 if use_address.endswith(".local"): |                 """Add a cache entry to the command arguments.""" | ||||||
|                     # Check mDNS cache for .local addresses |                 if not addresses: | ||||||
|                     if mdns := dashboard.mdns_status: |                     return | ||||||
|                         cached = mdns.get_cached_addresses(use_address) |                 normalized = hostname.rstrip(".").lower() | ||||||
|                         _LOGGER.debug( |  | ||||||
|                             "mDNS cache lookup for address %s: %s", use_address, cached |  | ||||||
|                         ) |  | ||||||
|                         if cached: |  | ||||||
|                             normalized = use_address.rstrip(".").lower() |  | ||||||
|                 cache_args.extend( |                 cache_args.extend( | ||||||
|                     [ |                     [ | ||||||
|                                     "--mdns-address-cache", |                         f"--{cache_type}-address-cache", | ||||||
|                                     f"{normalized}={','.join(sort_ip_addresses(cached))}", |                         f"{normalized}={','.join(sort_ip_addresses(addresses))}", | ||||||
|                                 ] |  | ||||||
|                             ) |  | ||||||
|                 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.address for cached addresses | ||||||
|  |             if use_address := entry.address: | ||||||
|  |                 if use_address.endswith(".local"): | ||||||
|  |                     # 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") | ||||||
|  |  | ||||||
|  |             # 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