Fix dhcp None hostname (#67289)
* Fix dhcp None hostname * Test handle None hostname
This commit is contained in:
parent
f901c61d54
commit
d9abd5efea
2 changed files with 31 additions and 7 deletions
|
@ -159,7 +159,7 @@ class WatcherBase:
|
||||||
async def async_start(self):
|
async def async_start(self):
|
||||||
"""Start the watcher."""
|
"""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."""
|
"""Process a client."""
|
||||||
return run_callback_threadsafe(
|
return run_callback_threadsafe(
|
||||||
self.hass.loop,
|
self.hass.loop,
|
||||||
|
@ -170,7 +170,9 @@ class WatcherBase:
|
||||||
).result()
|
).result()
|
||||||
|
|
||||||
@callback
|
@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."""
|
"""Process a client."""
|
||||||
made_ip_address = make_ip_address(ip_address)
|
made_ip_address = make_ip_address(ip_address)
|
||||||
|
|
||||||
|
@ -355,15 +357,15 @@ class DeviceTrackerRegisteredWatcher(WatcherBase):
|
||||||
async def async_start(self):
|
async def async_start(self):
|
||||||
"""Stop watching for device tracker registrations."""
|
"""Stop watching for device tracker registrations."""
|
||||||
self._unsub = async_dispatcher_connect(
|
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
|
@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."""
|
"""Process a device tracker state."""
|
||||||
ip_address = data.get(ATTR_IP)
|
ip_address = data[ATTR_IP]
|
||||||
hostname = data.get(ATTR_HOST_NAME, "")
|
hostname = data[ATTR_HOST_NAME] or ""
|
||||||
mac_address = data.get(ATTR_MAC)
|
mac_address = data[ATTR_MAC]
|
||||||
|
|
||||||
if ip_address is None or mac_address is None:
|
if ip_address is None or mac_address is None:
|
||||||
return
|
return
|
||||||
|
|
|
@ -663,6 +663,28 @@ async def test_device_tracker_registered(hass):
|
||||||
await hass.async_block_till_done()
|
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):
|
async def test_device_tracker_hostname_and_macaddress_after_start(hass):
|
||||||
"""Test matching based on hostname and macaddress after start."""
|
"""Test matching based on hostname and macaddress after start."""
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue