diff --git a/homeassistant/components/dhcp/__init__.py b/homeassistant/components/dhcp/__init__.py index a3de0e51708..0b5f8a49a34 100644 --- a/homeassistant/components/dhcp/__init__.py +++ b/homeassistant/components/dhcp/__init__.py @@ -159,7 +159,7 @@ class WatcherBase: async def async_start(self): """Start the watcher.""" - def process_client(self, ip_address, hostname, mac_address): + def process_client(self, ip_address: str, hostname: str, mac_address: str) -> None: """Process a client.""" return run_callback_threadsafe( self.hass.loop, @@ -170,7 +170,9 @@ class WatcherBase: ).result() @callback - def async_process_client(self, ip_address, hostname, mac_address): + def async_process_client( + self, ip_address: str, hostname: str, mac_address: str + ) -> None: """Process a client.""" made_ip_address = make_ip_address(ip_address) @@ -355,15 +357,15 @@ class DeviceTrackerRegisteredWatcher(WatcherBase): async def async_start(self): """Stop watching for device tracker registrations.""" self._unsub = async_dispatcher_connect( - self.hass, CONNECTED_DEVICE_REGISTERED, self._async_process_device_state + self.hass, CONNECTED_DEVICE_REGISTERED, self._async_process_device_data ) @callback - def _async_process_device_state(self, data: dict[str, Any]) -> None: + def _async_process_device_data(self, data: dict[str, str | None]) -> None: """Process a device tracker state.""" - ip_address = data.get(ATTR_IP) - hostname = data.get(ATTR_HOST_NAME, "") - mac_address = data.get(ATTR_MAC) + ip_address = data[ATTR_IP] + hostname = data[ATTR_HOST_NAME] or "" + mac_address = data[ATTR_MAC] if ip_address is None or mac_address is None: return diff --git a/tests/components/dhcp/test_init.py b/tests/components/dhcp/test_init.py index d1b8d72be67..a809d6eb5ab 100644 --- a/tests/components/dhcp/test_init.py +++ b/tests/components/dhcp/test_init.py @@ -663,6 +663,28 @@ async def test_device_tracker_registered(hass): await hass.async_block_till_done() +async def test_device_tracker_registered_hostname_none(hass): + """Test handle None hostname.""" + with patch.object(hass.config_entries.flow, "async_init") as mock_init: + device_tracker_watcher = dhcp.DeviceTrackerRegisteredWatcher( + hass, + {}, + [{"domain": "mock-domain", "hostname": "connect", "macaddress": "B8B7F1*"}], + ) + await device_tracker_watcher.async_start() + await hass.async_block_till_done() + async_dispatcher_send( + hass, + CONNECTED_DEVICE_REGISTERED, + {"ip": "192.168.210.56", "mac": "b8b7f16db533", "host_name": None}, + ) + await hass.async_block_till_done() + + assert len(mock_init.mock_calls) == 0 + await device_tracker_watcher.async_stop() + await hass.async_block_till_done() + + async def test_device_tracker_hostname_and_macaddress_after_start(hass): """Test matching based on hostname and macaddress after start."""