Skip to content

Commit dbda31f

Browse files
authored
Attach Ping device tracker to Ping device (#155399)
1 parent 24219dd commit dbda31f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

homeassistant/components/ping/device_tracker.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
ScannerEntity,
1111
)
1212
from homeassistant.core import HomeAssistant
13+
from homeassistant.helpers import device_registry as dr
1314
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1415
from homeassistant.helpers.update_coordinator import CoordinatorEntity
1516
from homeassistant.util import dt as dt_util
1617

17-
from .const import CONF_IMPORTED_BY
18+
from .const import CONF_IMPORTED_BY, DOMAIN
1819
from .coordinator import PingConfigEntry, PingUpdateCoordinator
1920

2021

@@ -24,7 +25,7 @@ async def async_setup_entry(
2425
async_add_entities: AddConfigEntryEntitiesCallback,
2526
) -> None:
2627
"""Set up a Ping config entry."""
27-
async_add_entities([PingDeviceTracker(entry, entry.runtime_data)])
28+
async_add_entities([PingDeviceTracker(hass, entry, entry.runtime_data)])
2829

2930

3031
class PingDeviceTracker(CoordinatorEntity[PingUpdateCoordinator], ScannerEntity):
@@ -33,7 +34,10 @@ class PingDeviceTracker(CoordinatorEntity[PingUpdateCoordinator], ScannerEntity)
3334
_last_seen: datetime | None = None
3435

3536
def __init__(
36-
self, config_entry: PingConfigEntry, coordinator: PingUpdateCoordinator
37+
self,
38+
hass: HomeAssistant,
39+
config_entry: PingConfigEntry,
40+
coordinator: PingUpdateCoordinator,
3741
) -> None:
3842
"""Initialize the Ping device tracker."""
3943
super().__init__(coordinator)
@@ -46,6 +50,13 @@ def __init__(
4650
)
4751
)
4852

53+
if (
54+
device := dr.async_get(hass).async_get_device(
55+
identifiers={(DOMAIN, config_entry.entry_id)}
56+
)
57+
) is not None:
58+
self.device_entry = device
59+
4960
@property
5061
def ip_address(self) -> str:
5162
"""Return the primary ip address of the device."""

tests/components/ping/test_device_tracker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ async def test_setup_and_update(
3838
assert entry.disabled
3939
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
4040

41+
# Verify that the device_tracker and binary_sensor entities are linked to the same device
42+
binary_sensor = entity_registry.async_get("binary_sensor.10_10_10_10")
43+
assert entry.device_id == binary_sensor.device_id
44+
4145
# check device tracker state is not there
4246
state = hass.states.get("device_tracker.10_10_10_10")
4347
assert state is None

0 commit comments

Comments
 (0)