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
pull/91079/head^2
J. Nick Koston 2023-04-08 16:32:56 -10:00 committed by GitHub
parent 69f751703b
commit 5f0d983df1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 19 deletions

View File

@ -348,7 +348,7 @@ class ScannerEntity(BaseTrackerEntity):
self.mac_address, self.mac_address,
self.unique_id, self.unique_id,
) )
if self.is_connected: if self.is_connected and self.ip_address:
_async_connected_device_registered( _async_connected_device_registered(
hass, hass,
self.mac_address, self.mac_address,
@ -405,7 +405,7 @@ class ScannerEntity(BaseTrackerEntity):
"""Return the device state attributes.""" """Return the device state attributes."""
attr: dict[str, StateType] = {} attr: dict[str, StateType] = {}
attr.update(super().state_attributes) attr.update(super().state_attributes)
if self.ip_address is not None: if self.ip_address:
attr[ATTR_IP] = self.ip_address attr[ATTR_IP] = self.ip_address
if self.mac_address is not None: if self.mac_address is not None:
attr[ATTR_MAC] = self.mac_address attr[ATTR_MAC] = self.mac_address

View File

@ -195,21 +195,6 @@ async def test_connected_device_registered(
class MockDisconnectedScannerEntity(MockScannerEntity): class MockDisconnectedScannerEntity(MockScannerEntity):
"""Mock a disconnected scanner entity.""" """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 @property
def mac_address(self) -> str: def mac_address(self) -> str:
return "aa:bb:cc:dd:ee:00" return "aa:bb:cc:dd:ee:00"
@ -222,10 +207,44 @@ async def test_connected_device_registered(
def hostname(self) -> str: def hostname(self) -> str:
return "disconnected" 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): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Mock setup entry method.""" """Mock setup entry method."""
async_add_entities( async_add_entities(
[MockConnectedScannerEntity(), MockDisconnectedScannerEntity()] [
MockConnectedScannerEntity(),
MockDisconnectedScannerEntity(),
MockConnectedScannerEntityBadIPAddress(),
]
) )
return True return True
@ -240,7 +259,7 @@ async def test_connected_device_registered(
full_name = f"{entity_platform.domain}.{config_entry.domain}" full_name = f"{entity_platform.domain}.{config_entry.domain}"
assert full_name in hass.config.components assert full_name in hass.config.components
assert len(hass.states.async_entity_ids()) == 0 # should be disabled assert len(hass.states.async_entity_ids()) == 0 # should be disabled
assert len(entity_registry.entities) == 2 assert len(entity_registry.entities) == 3
assert ( assert (
entity_registry.entities["test_domain.test_aa_bb_cc_dd_ee_ff"].config_entry_id entity_registry.entities["test_domain.test_aa_bb_cc_dd_ee_ff"].config_entry_id
== "super-mock-id" == "super-mock-id"