mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Ensure zeroconf does not generate config flows when matching attributes are missing (#50208)
If macaddress, name, or manufacturer were missing from the discovery info, the matcher would accept instead of reject.
This commit is contained in:
parent
c037ebb27c
commit
dd3965e4e2
@ -348,32 +348,29 @@ async def _async_start_zeroconf_browser(
|
|||||||
|
|
||||||
# Not all homekit types are currently used for discovery
|
# Not all homekit types are currently used for discovery
|
||||||
# so not all service type exist in zeroconf_types
|
# so not all service type exist in zeroconf_types
|
||||||
for entry in zeroconf_types.get(service_type, []):
|
for matcher in zeroconf_types.get(service_type, []):
|
||||||
if len(entry) > 1:
|
if len(matcher) > 1:
|
||||||
if (
|
if "macaddress" in matcher and (
|
||||||
uppercase_mac is not None
|
uppercase_mac is None
|
||||||
and "macaddress" in entry
|
or not fnmatch.fnmatch(uppercase_mac, matcher["macaddress"])
|
||||||
and not fnmatch.fnmatch(uppercase_mac, entry["macaddress"])
|
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
if (
|
if "name" in matcher and (
|
||||||
lowercase_name is not None
|
lowercase_name is None
|
||||||
and "name" in entry
|
or not fnmatch.fnmatch(lowercase_name, matcher["name"])
|
||||||
and not fnmatch.fnmatch(lowercase_name, entry["name"])
|
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
if (
|
if "manufacturer" in matcher and (
|
||||||
lowercase_manufacturer is not None
|
lowercase_manufacturer is None
|
||||||
and "manufacturer" in entry
|
or not fnmatch.fnmatch(
|
||||||
and not fnmatch.fnmatch(
|
lowercase_manufacturer, matcher["manufacturer"]
|
||||||
lowercase_manufacturer, entry["manufacturer"]
|
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
hass.add_job(
|
hass.add_job(
|
||||||
hass.config_entries.flow.async_init(
|
hass.config_entries.flow.async_init(
|
||||||
entry["domain"], context={"source": DOMAIN}, data=info
|
matcher["domain"], context={"source": DOMAIN}, data=info
|
||||||
) # type: ignore
|
) # type: ignore
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -346,6 +346,38 @@ async def test_zeroconf_match_manufacturer(hass, mock_zeroconf):
|
|||||||
assert mock_config_flow.mock_calls[0][1][0] == "samsungtv"
|
assert mock_config_flow.mock_calls[0][1][0] == "samsungtv"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_zeroconf_match_manufacturer_not_present(hass, mock_zeroconf):
|
||||||
|
"""Test matchers reject when a property is missing."""
|
||||||
|
|
||||||
|
def http_only_service_update_mock(zeroconf, services, handlers):
|
||||||
|
"""Call service update handler."""
|
||||||
|
handlers[0](
|
||||||
|
zeroconf,
|
||||||
|
"_airplay._tcp.local.",
|
||||||
|
"s1000._airplay._tcp.local.",
|
||||||
|
ServiceStateChange.Added,
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch.dict(
|
||||||
|
zc_gen.ZEROCONF,
|
||||||
|
{"_airplay._tcp.local.": [{"domain": "samsungtv", "manufacturer": "samsung*"}]},
|
||||||
|
clear=True,
|
||||||
|
), patch.object(
|
||||||
|
hass.config_entries.flow, "async_init"
|
||||||
|
) as mock_config_flow, patch.object(
|
||||||
|
zeroconf, "HaServiceBrowser", side_effect=http_only_service_update_mock
|
||||||
|
) as mock_service_browser:
|
||||||
|
mock_zeroconf.get_service_info.side_effect = get_zeroconf_info_mock(
|
||||||
|
"aa:bb:cc:dd:ee:ff"
|
||||||
|
)
|
||||||
|
assert await async_setup_component(hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {}})
|
||||||
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert len(mock_service_browser.mock_calls) == 1
|
||||||
|
assert len(mock_config_flow.mock_calls) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_zeroconf_no_match(hass, mock_zeroconf):
|
async def test_zeroconf_no_match(hass, mock_zeroconf):
|
||||||
"""Test configured options for a device are loaded via config entry."""
|
"""Test configured options for a device are loaded via config entry."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user