Ignore unspecified addresses from zeroconf (#81620)

This commit is contained in:
J. Nick Koston 2022-11-07 15:50:45 -06:00 committed by GitHub
parent 7f23ab9d86
commit 5c38321c4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View File

@ -552,12 +552,20 @@ def _first_non_link_local_address(
"""Return the first ipv6 or non-link local ipv4 address, preferring IPv4.""" """Return the first ipv6 or non-link local ipv4 address, preferring IPv4."""
for address in addresses: for address in addresses:
ip_addr = ip_address(address) ip_addr = ip_address(address)
if not ip_addr.is_link_local and ip_addr.version == 4: if (
not ip_addr.is_link_local
and not ip_addr.is_unspecified
and ip_addr.version == 4
):
return str(ip_addr) return str(ip_addr)
# If we didn't find a good IPv4 address, check for IPv6 addresses. # If we didn't find a good IPv4 address, check for IPv6 addresses.
for address in addresses: for address in addresses:
ip_addr = ip_address(address) ip_addr = ip_address(address)
if not ip_addr.is_link_local and ip_addr.version == 6: if (
not ip_addr.is_link_local
and not ip_addr.is_unspecified
and ip_addr.version == 6
):
return str(ip_addr) return str(ip_addr)
return None return None

View File

@ -819,6 +819,24 @@ async def test_info_from_service_with_link_local_address_first(hass):
assert info.host == "192.168.66.12" assert info.host == "192.168.66.12"
async def test_info_from_service_with_unspecified_address_first(hass):
"""Test that the unspecified address is ignored."""
service_type = "_test._tcp.local."
service_info = get_service_info_mock(service_type, f"test.{service_type}")
service_info.addresses = ["0.0.0.0", "192.168.66.12"]
info = zeroconf.info_from_service(service_info)
assert info.host == "192.168.66.12"
async def test_info_from_service_with_unspecified_address_only(hass):
"""Test that the unspecified address is ignored."""
service_type = "_test._tcp.local."
service_info = get_service_info_mock(service_type, f"test.{service_type}")
service_info.addresses = ["0.0.0.0"]
info = zeroconf.info_from_service(service_info)
assert info is None
async def test_info_from_service_with_link_local_address_second(hass): async def test_info_from_service_with_link_local_address_second(hass):
"""Test that the link local address is ignored.""" """Test that the link local address is ignored."""
service_type = "_test._tcp.local." service_type = "_test._tcp.local."