Refactor dhcp to move all mac formatting into the client processor (#110509)

This commit is contained in:
J. Nick Koston 2024-02-13 19:12:38 -06:00 committed by GitHub
parent 9a8c5af0a6
commit b3452dc3f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -186,7 +186,7 @@ class WatcherBase(ABC):
@callback @callback
def async_process_client( def async_process_client(
self, ip_address: str, hostname: str, mac_address: str self, ip_address: str, hostname: str, unformatted_mac_address: str
) -> None: ) -> None:
"""Process a client.""" """Process a client."""
if (made_ip_address := cached_ip_addresses(ip_address)) is None: if (made_ip_address := cached_ip_addresses(ip_address)) is None:
@ -202,6 +202,12 @@ class WatcherBase(ABC):
# Ignore self assigned addresses, loopback, invalid # Ignore self assigned addresses, loopback, invalid
return return
formatted_mac = format_mac(unformatted_mac_address)
# Historically, the MAC address was formatted without colons
# and since all consumers of this data are expecting it to be
# formatted without colons we will continue to do so
mac_address = formatted_mac.replace(":", "")
data = self._address_data.get(ip_address) data = self._address_data.get(ip_address)
if ( if (
data data
@ -231,7 +237,7 @@ class WatcherBase(ABC):
dev_reg: DeviceRegistry = async_get(self.hass) dev_reg: DeviceRegistry = async_get(self.hass)
if device := dev_reg.async_get_device( if device := dev_reg.async_get_device(
connections={(CONNECTION_NETWORK_MAC, uppercase_mac)} connections={(CONNECTION_NETWORK_MAC, formatted_mac)}
): ):
for entry_id in device.config_entries: for entry_id in device.config_entries:
if ( if (
@ -319,7 +325,7 @@ class NetworkWatcher(WatcherBase):
self.async_process_client( self.async_process_client(
host[DISCOVERY_IP_ADDRESS], host[DISCOVERY_IP_ADDRESS],
host[DISCOVERY_HOSTNAME], host[DISCOVERY_HOSTNAME],
_format_mac(host[DISCOVERY_MAC_ADDRESS]), host[DISCOVERY_MAC_ADDRESS],
) )
@ -360,7 +366,7 @@ class DeviceTrackerWatcher(WatcherBase):
if ip_address is None or mac_address is None: if ip_address is None or mac_address is None:
return return
self.async_process_client(ip_address, hostname, _format_mac(mac_address)) self.async_process_client(ip_address, hostname, mac_address)
class DeviceTrackerRegisteredWatcher(WatcherBase): class DeviceTrackerRegisteredWatcher(WatcherBase):
@ -383,7 +389,7 @@ class DeviceTrackerRegisteredWatcher(WatcherBase):
if ip_address is None or mac_address is None: if ip_address is None or mac_address is None:
return return
self.async_process_client(ip_address, hostname, _format_mac(mac_address)) self.async_process_client(ip_address, hostname, mac_address)
class DHCPWatcher(WatcherBase): class DHCPWatcher(WatcherBase):
@ -393,7 +399,7 @@ class DHCPWatcher(WatcherBase):
def _async_process_dhcp_request(self, response: aiodhcpwatcher.DHCPRequest) -> None: def _async_process_dhcp_request(self, response: aiodhcpwatcher.DHCPRequest) -> None:
"""Process a dhcp request.""" """Process a dhcp request."""
self.async_process_client( self.async_process_client(
response.ip_address, response.hostname, _format_mac(response.mac_address) response.ip_address, response.hostname, response.mac_address
) )
@callback @callback
@ -402,11 +408,6 @@ class DHCPWatcher(WatcherBase):
self._unsub = aiodhcpwatcher.start(self._async_process_dhcp_request) self._unsub = aiodhcpwatcher.start(self._async_process_dhcp_request)
def _format_mac(mac_address: str) -> str:
"""Format a mac address for matching."""
return format_mac(mac_address).replace(":", "")
@lru_cache(maxsize=4096, typed=True) @lru_cache(maxsize=4096, typed=True)
def _compile_fnmatch(pattern: str) -> re.Pattern: def _compile_fnmatch(pattern: str) -> re.Pattern:
"""Compile a fnmatch pattern.""" """Compile a fnmatch pattern."""