diff --git a/homeassistant/components/dhcp/__init__.py b/homeassistant/components/dhcp/__init__.py index a65548d6654..7f872979ed7 100644 --- a/homeassistant/components/dhcp/__init__.py +++ b/homeassistant/components/dhcp/__init__.py @@ -37,6 +37,7 @@ MESSAGE_TYPE = "message-type" HOSTNAME = "hostname" MAC_ADDRESS = "macaddress" IP_ADDRESS = "ip" +SELF_ASSIGNED_BLOCK = "169.254." DHCP_REQUEST = 3 _LOGGER = logging.getLogger(__name__) @@ -81,6 +82,10 @@ class WatcherBase: def process_client(self, ip_address, hostname, mac_address): """Process a client.""" + if ip_address.startswith(SELF_ASSIGNED_BLOCK): + # Ignore self assigned addresses + return + data = self._address_data.get(ip_address) if data and data[MAC_ADDRESS] == mac_address and data[HOSTNAME] == hostname: diff --git a/tests/components/dhcp/test_init.py b/tests/components/dhcp/test_init.py index b5724f4a303..e1c2d988096 100644 --- a/tests/components/dhcp/test_init.py +++ b/tests/components/dhcp/test_init.py @@ -504,3 +504,32 @@ async def test_device_tracker_hostname_and_macaddress_after_start_hostname_missi await hass.async_block_till_done() assert len(mock_init.mock_calls) == 0 + + +async def test_device_tracker_ignore_self_assigned_ips_before_start(hass): + """Test matching ignores self assigned ip address.""" + hass.states.async_set( + "device_tracker.august_connect", + STATE_HOME, + { + ATTR_HOST_NAME: "connect", + ATTR_IP: "169.254.210.56", + ATTR_SOURCE_TYPE: SOURCE_TYPE_ROUTER, + ATTR_MAC: "B8:B7:F1:6D:B5:33", + }, + ) + + with patch.object( + hass.config_entries.flow, "async_init", return_value=mock_coro() + ) as mock_init: + device_tracker_watcher = dhcp.DeviceTrackerWatcher( + hass, + {}, + [{"domain": "mock-domain", "hostname": "connect", "macaddress": "B8B7F1*"}], + ) + device_tracker_watcher.async_start() + await hass.async_block_till_done() + device_tracker_watcher.async_stop() + await hass.async_block_till_done() + + assert len(mock_init.mock_calls) == 0