Move device tracker scanner attributes to ScannerEntity (#45312)

pull/45339/head
Martin Hjelmare 2021-01-19 18:04:08 +01:00 committed by GitHub
parent 52cfc06e12
commit a9a0f8938f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 24 deletions

View File

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

View File

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

View File

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