1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Attach Ping device tracker to Ping device (#155399)

This commit is contained in:
Jan-Philipp Benecke
2025-10-29 14:31:51 +01:00
committed by GitHub
parent 24219dd8f7
commit dbda31f6d5
2 changed files with 18 additions and 3 deletions

View File

@@ -10,11 +10,12 @@ from homeassistant.components.device_tracker import (
ScannerEntity,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util
from .const import CONF_IMPORTED_BY
from .const import CONF_IMPORTED_BY, DOMAIN
from .coordinator import PingConfigEntry, PingUpdateCoordinator
@@ -24,7 +25,7 @@ async def async_setup_entry(
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up a Ping config entry."""
async_add_entities([PingDeviceTracker(entry, entry.runtime_data)])
async_add_entities([PingDeviceTracker(hass, entry, entry.runtime_data)])
class PingDeviceTracker(CoordinatorEntity[PingUpdateCoordinator], ScannerEntity):
@@ -33,7 +34,10 @@ class PingDeviceTracker(CoordinatorEntity[PingUpdateCoordinator], ScannerEntity)
_last_seen: datetime | None = None
def __init__(
self, config_entry: PingConfigEntry, coordinator: PingUpdateCoordinator
self,
hass: HomeAssistant,
config_entry: PingConfigEntry,
coordinator: PingUpdateCoordinator,
) -> None:
"""Initialize the Ping device tracker."""
super().__init__(coordinator)
@@ -46,6 +50,13 @@ class PingDeviceTracker(CoordinatorEntity[PingUpdateCoordinator], ScannerEntity)
)
)
if (
device := dr.async_get(hass).async_get_device(
identifiers={(DOMAIN, config_entry.entry_id)}
)
) is not None:
self.device_entry = device
@property
def ip_address(self) -> str:
"""Return the primary ip address of the device."""

View File

@@ -38,6 +38,10 @@ async def test_setup_and_update(
assert entry.disabled
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
# Verify that the device_tracker and binary_sensor entities are linked to the same device
binary_sensor = entity_registry.async_get("binary_sensor.10_10_10_10")
assert entry.device_id == binary_sensor.device_id
# check device tracker state is not there
state = hass.states.get("device_tracker.10_10_10_10")
assert state is None