mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Move device tracker scanner attributes to ScannerEntity (#45312)
This commit is contained in:
parent
52cfc06e12
commit
a9a0f8938f
@ -47,21 +47,6 @@ class BaseTrackerEntity(Entity):
|
|||||||
"""Return the source type, eg gps or router, of the device."""
|
"""Return the source type, eg gps or router, of the device."""
|
||||||
raise NotImplementedError
|
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
|
@property
|
||||||
def state_attributes(self):
|
def state_attributes(self):
|
||||||
"""Return the device state attributes."""
|
"""Return the device state attributes."""
|
||||||
@ -69,12 +54,6 @@ class BaseTrackerEntity(Entity):
|
|||||||
|
|
||||||
if self.battery_level:
|
if self.battery_level:
|
||||||
attr[ATTR_BATTERY_LEVEL] = 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
|
return attr
|
||||||
|
|
||||||
@ -151,6 +130,21 @@ class TrackerEntity(BaseTrackerEntity):
|
|||||||
class ScannerEntity(BaseTrackerEntity):
|
class ScannerEntity(BaseTrackerEntity):
|
||||||
"""Represent a tracked device that is on a scanned network."""
|
"""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
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
@ -162,3 +156,17 @@ class ScannerEntity(BaseTrackerEntity):
|
|||||||
def is_connected(self):
|
def is_connected(self):
|
||||||
"""Return true if the device is connected to the network."""
|
"""Return true if the device is connected to the network."""
|
||||||
raise NotImplementedError
|
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
|
||||||
|
@ -6,6 +6,9 @@ from homeassistant.components.device_tracker.config_entry import (
|
|||||||
ScannerEntity,
|
ScannerEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.components.device_tracker.const import (
|
from homeassistant.components.device_tracker.const import (
|
||||||
|
ATTR_HOST_NAME,
|
||||||
|
ATTR_IP,
|
||||||
|
ATTR_MAC,
|
||||||
ATTR_SOURCE_TYPE,
|
ATTR_SOURCE_TYPE,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SOURCE_TYPE_ROUTER,
|
SOURCE_TYPE_ROUTER,
|
||||||
@ -28,6 +31,9 @@ async def test_scanner_entity_device_tracker(hass):
|
|||||||
assert entity_state.attributes == {
|
assert entity_state.attributes == {
|
||||||
ATTR_SOURCE_TYPE: SOURCE_TYPE_ROUTER,
|
ATTR_SOURCE_TYPE: SOURCE_TYPE_ROUTER,
|
||||||
ATTR_BATTERY_LEVEL: 100,
|
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
|
assert entity_state.state == STATE_NOT_HOME
|
||||||
|
|
||||||
@ -49,6 +55,9 @@ def test_scanner_entity():
|
|||||||
with pytest.raises(NotImplementedError):
|
with pytest.raises(NotImplementedError):
|
||||||
assert entity.state == STATE_NOT_HOME
|
assert entity.state == STATE_NOT_HOME
|
||||||
assert entity.battery_level is None
|
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():
|
def test_base_tracker_entity():
|
||||||
@ -59,6 +68,3 @@ def test_base_tracker_entity():
|
|||||||
assert entity.battery_level is None
|
assert entity.battery_level is None
|
||||||
with pytest.raises(NotImplementedError):
|
with pytest.raises(NotImplementedError):
|
||||||
assert entity.state_attributes is None
|
assert entity.state_attributes is None
|
||||||
assert entity.ip_address is None
|
|
||||||
assert entity.mac_address is None
|
|
||||||
assert entity.hostname is None
|
|
||||||
|
@ -16,6 +16,9 @@ class MockScannerEntity(ScannerEntity):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Init."""
|
"""Init."""
|
||||||
self.connected = False
|
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
|
@property
|
||||||
def source_type(self):
|
def source_type(self):
|
||||||
@ -30,6 +33,21 @@ class MockScannerEntity(ScannerEntity):
|
|||||||
"""
|
"""
|
||||||
return 100
|
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
|
@property
|
||||||
def is_connected(self):
|
def is_connected(self):
|
||||||
"""Return true if the device is connected to the network."""
|
"""Return true if the device is connected to the network."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user