diff --git a/homeassistant/components/device_tracker/config_entry.py b/homeassistant/components/device_tracker/config_entry.py index 8cdc843f680..16e7d022c92 100644 --- a/homeassistant/components/device_tracker/config_entry.py +++ b/homeassistant/components/device_tracker/config_entry.py @@ -47,21 +47,6 @@ class BaseTrackerEntity(Entity): """Return the source type, eg gps or router, of the device.""" raise NotImplementedError - @property - def ip_address(self) -> str: - """Return the primary ip address of the device.""" - return None - - @property - def mac_address(self) -> str: - """Return the mac address of the device.""" - return None - - @property - def hostname(self) -> str: - """Return hostname of the device.""" - return None - @property def state_attributes(self): """Return the device state attributes.""" @@ -69,12 +54,6 @@ class BaseTrackerEntity(Entity): if self.battery_level: attr[ATTR_BATTERY_LEVEL] = self.battery_level - if self.ip_address is not None: - attr[ATTR_IP] = self.ip_address - if self.ip_address is not None: - attr[ATTR_MAC] = self.mac_address - if self.hostname is not None: - attr[ATTR_HOST_NAME] = self.hostname return attr @@ -151,6 +130,21 @@ class TrackerEntity(BaseTrackerEntity): class ScannerEntity(BaseTrackerEntity): """Represent a tracked device that is on a scanned network.""" + @property + def ip_address(self) -> str: + """Return the primary ip address of the device.""" + return None + + @property + def mac_address(self) -> str: + """Return the mac address of the device.""" + return None + + @property + def hostname(self) -> str: + """Return hostname of the device.""" + return None + @property def state(self): """Return the state of the device.""" @@ -162,3 +156,17 @@ class ScannerEntity(BaseTrackerEntity): def is_connected(self): """Return true if the device is connected to the network.""" raise NotImplementedError + + @property + def state_attributes(self): + """Return the device state attributes.""" + attr = {} + attr.update(super().state_attributes) + if self.ip_address is not None: + attr[ATTR_IP] = self.ip_address + if self.mac_address is not None: + attr[ATTR_MAC] = self.mac_address + if self.hostname is not None: + attr[ATTR_HOST_NAME] = self.hostname + + return attr diff --git a/tests/components/device_tracker/test_entities.py b/tests/components/device_tracker/test_entities.py index 6c8674f97b9..1bc058a1449 100644 --- a/tests/components/device_tracker/test_entities.py +++ b/tests/components/device_tracker/test_entities.py @@ -6,6 +6,9 @@ from homeassistant.components.device_tracker.config_entry import ( ScannerEntity, ) from homeassistant.components.device_tracker.const import ( + ATTR_HOST_NAME, + ATTR_IP, + ATTR_MAC, ATTR_SOURCE_TYPE, DOMAIN, SOURCE_TYPE_ROUTER, @@ -28,6 +31,9 @@ async def test_scanner_entity_device_tracker(hass): assert entity_state.attributes == { ATTR_SOURCE_TYPE: SOURCE_TYPE_ROUTER, ATTR_BATTERY_LEVEL: 100, + ATTR_IP: "0.0.0.0", + ATTR_MAC: "ad:de:ef:be:ed:fe:", + ATTR_HOST_NAME: "test.hostname.org", } assert entity_state.state == STATE_NOT_HOME @@ -49,6 +55,9 @@ def test_scanner_entity(): with pytest.raises(NotImplementedError): assert entity.state == STATE_NOT_HOME assert entity.battery_level is None + assert entity.ip_address is None + assert entity.mac_address is None + assert entity.hostname is None def test_base_tracker_entity(): @@ -59,6 +68,3 @@ def test_base_tracker_entity(): assert entity.battery_level is None with pytest.raises(NotImplementedError): assert entity.state_attributes is None - assert entity.ip_address is None - assert entity.mac_address is None - assert entity.hostname is None diff --git a/tests/testing_config/custom_components/test/device_tracker.py b/tests/testing_config/custom_components/test/device_tracker.py index 59f163d6714..e4853d156ce 100644 --- a/tests/testing_config/custom_components/test/device_tracker.py +++ b/tests/testing_config/custom_components/test/device_tracker.py @@ -16,6 +16,9 @@ class MockScannerEntity(ScannerEntity): def __init__(self): """Init.""" self.connected = False + self._hostname = "test.hostname.org" + self._ip_address = "0.0.0.0" + self._mac_address = "ad:de:ef:be:ed:fe:" @property def source_type(self): @@ -30,6 +33,21 @@ class MockScannerEntity(ScannerEntity): """ return 100 + @property + def ip_address(self) -> str: + """Return the primary ip address of the device.""" + return self._ip_address + + @property + def mac_address(self) -> str: + """Return the mac address of the device.""" + return self._mac_address + + @property + def hostname(self) -> str: + """Return hostname of the device.""" + return self._hostname + @property def is_connected(self): """Return true if the device is connected to the network."""