Allow integrations to request dhcp discovery flows for registered devices (#66528)

This commit is contained in:
J. Nick Koston 2022-02-15 11:02:52 -06:00 committed by GitHub
parent 2b43293363
commit f069a37f7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 239 additions and 647 deletions

View file

@ -25,10 +25,11 @@ from homeassistant.const import (
STATE_HOME,
STATE_NOT_HOME,
)
import homeassistant.helpers.device_registry as dr
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed
from tests.common import MockConfigEntry, async_fire_time_changed
# connect b8:b7:f1:6d:b5:33 192.168.210.56
RAW_DHCP_REQUEST = (
@ -207,6 +208,52 @@ async def test_dhcp_renewal_match_hostname_and_macaddress(hass):
)
async def test_registered_devices(hass):
"""Test discovery flows are created for registered devices."""
integration_matchers = [
{"domain": "not-matching", "registered_devices": True},
{"domain": "mock-domain", "registered_devices": True},
]
packet = Ether(RAW_DHCP_RENEWAL)
registry = dr.async_get(hass)
config_entry = MockConfigEntry(domain="mock-domain", data={})
config_entry.add_to_hass(hass)
registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "50147903852c")},
name="name",
)
# Not enabled should not get flows
config_entry2 = MockConfigEntry(domain="mock-domain-2", data={})
config_entry2.add_to_hass(hass)
registry.async_get_or_create(
config_entry_id=config_entry2.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "50147903852c")},
name="name",
)
async_handle_dhcp_packet = await _async_get_handle_dhcp_packet(
hass, integration_matchers
)
with patch.object(hass.config_entries.flow, "async_init") as mock_init:
await async_handle_dhcp_packet(packet)
# Ensure no change is ignored
await async_handle_dhcp_packet(packet)
assert len(mock_init.mock_calls) == 1
assert mock_init.mock_calls[0][1][0] == "mock-domain"
assert mock_init.mock_calls[0][2]["context"] == {
"source": config_entries.SOURCE_DHCP
}
assert mock_init.mock_calls[0][2]["data"] == dhcp.DhcpServiceInfo(
ip="192.168.1.120",
hostname="irobot-ae9ec12dd3b04885bcbfa36afb01e1cc",
macaddress="50147903852c",
)
async def test_dhcp_match_hostname(hass):
"""Test matching based on hostname only."""
integration_matchers = [{"domain": "mock-domain", "hostname": "connect"}]