1 """Support for Keenetic routers as device tracker."""
3 from __future__
import annotations
7 from ndms2_client
import Device
10 DOMAIN
as DEVICE_TRACKER_DOMAIN,
20 from .const
import DOMAIN, ROUTER
21 from .router
import KeeneticRouter
23 _LOGGER = logging.getLogger(__name__)
28 config_entry: ConfigEntry,
29 async_add_entities: AddEntitiesCallback,
31 """Set up device tracker for Keenetic NDMS2 component."""
32 router: KeeneticRouter = hass.data[DOMAIN][config_entry.entry_id][ROUTER]
34 tracked: set[str] = set()
37 def update_from_router():
38 """Update the status of devices."""
43 registry = er.async_get(hass)
46 for entity_entry
in registry.entities.get_entries_for_config_entry_id(
49 if entity_entry.domain == DEVICE_TRACKER_DOMAIN:
50 mac = entity_entry.unique_id.partition(
"_")[0]
51 if mac
not in tracked:
58 name=entity_entry.original_name,
72 def update_items(router: KeeneticRouter, async_add_entities, tracked: set[str]):
73 """Update tracked device state from the hub."""
74 new_tracked: list[KeeneticTracker] = []
75 for mac, device
in router.last_devices.items():
76 if mac
not in tracked:
84 """Representation of network device."""
86 _attr_should_poll =
False
88 def __init__(self, device: Device, router: KeeneticRouter) ->
None:
89 """Initialize the tracked device."""
93 dt_util.utcnow()
if device.mac
in router.last_devices
else None
98 """Return true if the device is connected to the network."""
101 and (dt_util.utcnow() - self.
_last_seen_last_seen)
102 < self.
_router_router.consider_home_interval
107 """Return the name of the device."""
112 """Return a unique identifier for this device."""
113 return f
"{self._device.mac}_{self._router.config_entry.entry_id}"
117 """Return the primary ip address of the device."""
122 """Return the mac address of the device."""
127 """Return if controller is available."""
128 return self.
_router_router.available
132 """Return the device state attributes."""
135 "interface": self.
_device_device.interface,
140 """Client entity created."""
141 _LOGGER.debug(
"New network device tracker %s (%s)", self.
namename, self.
unique_idunique_id)
146 "Updating Keenetic tracked device %s (%s)",
150 new_device = self.
_router_router.last_devices.get(self.
_device_device.mac)
152 self.
_device_device = new_device
155 self.async_write_ha_state()
157 self.async_on_remove(
159 self.hass, self.
_router_router.signal_update, update_device
def extra_state_attributes(self)
None __init__(self, Device device, KeeneticRouter router)
None async_added_to_hass(self)
str|None ip_address(self)
def update_items(KeeneticRouter router, async_add_entities, set[str] tracked)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
str|None update_device(HomeAssistant hass, ConfigEntry config_entry, ConfigType config)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)