From 5f0d983df19f2833d28a9fd633b02aa45239d4ba Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Apr 2023 16:32:56 -1000 Subject: [PATCH] Make the device_tracker more forgiving when passed an empty ip address string (#91101) This has come up over and over and over again fixes #87165 fixes #51980 --- .../components/device_tracker/config_entry.py | 4 +- .../device_tracker/test_config_entry.py | 53 +++++++++++++------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/device_tracker/config_entry.py b/homeassistant/components/device_tracker/config_entry.py index 6a7133b9483..10725cd0392 100644 --- a/homeassistant/components/device_tracker/config_entry.py +++ b/homeassistant/components/device_tracker/config_entry.py @@ -348,7 +348,7 @@ class ScannerEntity(BaseTrackerEntity): self.mac_address, self.unique_id, ) - if self.is_connected: + if self.is_connected and self.ip_address: _async_connected_device_registered( hass, self.mac_address, @@ -405,7 +405,7 @@ class ScannerEntity(BaseTrackerEntity): """Return the device state attributes.""" attr: dict[str, StateType] = {} attr.update(super().state_attributes) - if self.ip_address is not None: + if self.ip_address: attr[ATTR_IP] = self.ip_address if self.mac_address is not None: attr[ATTR_MAC] = self.mac_address diff --git a/tests/components/device_tracker/test_config_entry.py b/tests/components/device_tracker/test_config_entry.py index 7fa72ac5c13..f9c259a00f4 100644 --- a/tests/components/device_tracker/test_config_entry.py +++ b/tests/components/device_tracker/test_config_entry.py @@ -195,21 +195,6 @@ async def test_connected_device_registered( class MockDisconnectedScannerEntity(MockScannerEntity): """Mock a disconnected scanner entity.""" - @property - def mac_address(self) -> str: - return "aa:bb:cc:dd:ee:ff" - - @property - def is_connected(self) -> bool: - return True - - @property - def hostname(self) -> str: - return "connected" - - class MockConnectedScannerEntity(MockScannerEntity): - """Mock a disconnected scanner entity.""" - @property def mac_address(self) -> str: return "aa:bb:cc:dd:ee:00" @@ -222,10 +207,44 @@ async def test_connected_device_registered( def hostname(self) -> str: return "disconnected" + class MockConnectedScannerEntity(MockScannerEntity): + """Mock a disconnected scanner entity.""" + + @property + def mac_address(self) -> str: + return "aa:bb:cc:dd:ee:ff" + + @property + def is_connected(self) -> bool: + return True + + @property + def hostname(self) -> str: + return "connected" + + class MockConnectedScannerEntityBadIPAddress(MockConnectedScannerEntity): + """Mock a disconnected scanner entity.""" + + @property + def mac_address(self) -> str: + return "aa:bb:cc:dd:ee:01" + + @property + def ip_address(self) -> str: + return "" + + @property + def hostname(self) -> str: + return "connected_bad_ip" + async def async_setup_entry(hass, config_entry, async_add_entities): """Mock setup entry method.""" async_add_entities( - [MockConnectedScannerEntity(), MockDisconnectedScannerEntity()] + [ + MockConnectedScannerEntity(), + MockDisconnectedScannerEntity(), + MockConnectedScannerEntityBadIPAddress(), + ] ) return True @@ -240,7 +259,7 @@ async def test_connected_device_registered( full_name = f"{entity_platform.domain}.{config_entry.domain}" assert full_name in hass.config.components assert len(hass.states.async_entity_ids()) == 0 # should be disabled - assert len(entity_registry.entities) == 2 + assert len(entity_registry.entities) == 3 assert ( entity_registry.entities["test_domain.test_aa_bb_cc_dd_ee_ff"].config_entry_id == "super-mock-id"