Ignore unspecified addresses from zeroconf (#81620)

pull/81784/head
J. Nick Koston 2022-11-07 15:50:45 -06:00 committed by Franck Nijhof
parent c60c99bd74
commit 4391640734
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
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."""
for address in addresses:
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)
# If we didn't find a good IPv4 address, check for IPv6 addresses.
for address in addresses:
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 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"
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):
"""Test that the link local address is ignored."""
service_type = "_test._tcp.local."