Fix dhcp None hostname (#67289)

* Fix dhcp None hostname

* Test handle None hostname
This commit is contained in:
Martin Hjelmare 2022-02-26 19:37:24 +01:00 committed by GitHub
parent f901c61d54
commit d9abd5efea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 7 deletions

View File

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

View File

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