Make DHCP discovery ignore self assigned ip addresses (#45256)

This commit is contained in:
J. Nick Koston 2021-01-16 23:35:02 -10:00 committed by GitHub
parent a42d43d054
commit fbc98b1291
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -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:

View File

@ -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