Skip to content
Snippets Groups Projects
Unverified Commit 54f460b7 authored by J. Nick Koston's avatar J. Nick Koston Committed by GitHub
Browse files

Improve performance of dhcp on high activity networks (#105884)

* Improve performance of dhcp on high activity networks

Most of the overhead was constructing IPAddress objects
which is solved with https://github.com/bdraco/cached-ipaddress

* fix test

* fix: bump

* bump again

* lets do it on the correct branch
parent f9c09668
Branches
Tags
No related merge requests found
...@@ -9,7 +9,6 @@ from dataclasses import dataclass ...@@ -9,7 +9,6 @@ from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from fnmatch import translate from fnmatch import translate
from functools import lru_cache from functools import lru_cache
from ipaddress import ip_address as make_ip_address
import logging import logging
import os import os
import re import re
...@@ -22,6 +21,7 @@ from aiodiscover.discovery import ( ...@@ -22,6 +21,7 @@ from aiodiscover.discovery import (
IP_ADDRESS as DISCOVERY_IP_ADDRESS, IP_ADDRESS as DISCOVERY_IP_ADDRESS,
MAC_ADDRESS as DISCOVERY_MAC_ADDRESS, MAC_ADDRESS as DISCOVERY_MAC_ADDRESS,
) )
from cached_ipaddress import cached_ip_addresses
from scapy.config import conf from scapy.config import conf
from scapy.error import Scapy_Exception from scapy.error import Scapy_Exception
...@@ -153,7 +153,10 @@ class WatcherBase(ABC): ...@@ -153,7 +153,10 @@ class WatcherBase(ABC):
self, ip_address: str, hostname: str, mac_address: str self, ip_address: str, hostname: str, mac_address: str
) -> None: ) -> None:
"""Process a client.""" """Process a client."""
made_ip_address = make_ip_address(ip_address) if (made_ip_address := cached_ip_addresses(ip_address)) is None:
# Ignore invalid addresses
_LOGGER.debug("Ignoring invalid IP Address: %s", ip_address)
return
if ( if (
made_ip_address.is_link_local made_ip_address.is_link_local
......
...@@ -7,5 +7,9 @@ ...@@ -7,5 +7,9 @@
"iot_class": "local_push", "iot_class": "local_push",
"loggers": ["aiodiscover", "dnspython", "pyroute2", "scapy"], "loggers": ["aiodiscover", "dnspython", "pyroute2", "scapy"],
"quality_scale": "internal", "quality_scale": "internal",
"requirements": ["scapy==2.5.0", "aiodiscover==1.6.0"] "requirements": [
"scapy==2.5.0",
"aiodiscover==1.6.0",
"cached_ipaddress==0.3.0"
]
} }
...@@ -16,6 +16,7 @@ bleak==0.21.1 ...@@ -16,6 +16,7 @@ bleak==0.21.1
bluetooth-adapters==0.16.2 bluetooth-adapters==0.16.2
bluetooth-auto-recovery==1.2.3 bluetooth-auto-recovery==1.2.3
bluetooth-data-tools==1.19.0 bluetooth-data-tools==1.19.0
cached_ipaddress==0.3.0
certifi>=2021.5.30 certifi>=2021.5.30
ciso8601==2.3.0 ciso8601==2.3.0
cryptography==41.0.7 cryptography==41.0.7
......
...@@ -608,6 +608,9 @@ btsmarthub-devicelist==0.2.3 ...@@ -608,6 +608,9 @@ btsmarthub-devicelist==0.2.3
# homeassistant.components.buienradar # homeassistant.components.buienradar
buienradar==1.0.5 buienradar==1.0.5
# homeassistant.components.dhcp
cached_ipaddress==0.3.0
# homeassistant.components.caldav # homeassistant.components.caldav
caldav==1.3.8 caldav==1.3.8
......
...@@ -507,6 +507,9 @@ bthome-ble==3.3.1 ...@@ -507,6 +507,9 @@ bthome-ble==3.3.1
# homeassistant.components.buienradar # homeassistant.components.buienradar
buienradar==1.0.5 buienradar==1.0.5
# homeassistant.components.dhcp
cached_ipaddress==0.3.0
# homeassistant.components.caldav # homeassistant.components.caldav
caldav==1.3.8 caldav==1.3.8
......
...@@ -828,6 +828,36 @@ async def test_device_tracker_hostname_and_macaddress_after_start_hostname_missi ...@@ -828,6 +828,36 @@ async def test_device_tracker_hostname_and_macaddress_after_start_hostname_missi
assert len(mock_init.mock_calls) == 0 assert len(mock_init.mock_calls) == 0
async def test_device_tracker_invalid_ip_address(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test an invalid ip address."""
with patch.object(hass.config_entries.flow, "async_init") as mock_init:
device_tracker_watcher = dhcp.DeviceTrackerWatcher(
hass,
{},
[{"domain": "mock-domain", "hostname": "connect", "macaddress": "B8B7F1*"}],
)
await device_tracker_watcher.async_start()
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.august_connect",
STATE_HOME,
{
ATTR_IP: "invalid",
ATTR_SOURCE_TYPE: SourceType.ROUTER,
ATTR_MAC: "B8:B7:F1:6D:B5:33",
},
)
await hass.async_block_till_done()
await device_tracker_watcher.async_stop()
await hass.async_block_till_done()
assert "Ignoring invalid IP Address: invalid" in caplog.text
assert len(mock_init.mock_calls) == 0
async def test_device_tracker_ignore_self_assigned_ips_before_start( async def test_device_tracker_ignore_self_assigned_ips_before_start(
hass: HomeAssistant, hass: HomeAssistant,
) -> None: ) -> None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment