2020-03-21 19:02:28 +03:00
|
|
|
"""Support for the Mikrotik Router device tracker."""
|
2025-04-25 21:49:43 +02:00
|
|
|
|
2023-08-09 10:19:04 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from logging import getLogger
|
2022-02-05 00:56:30 +01:00
|
|
|
from collections.abc import Mapping
|
2020-04-10 00:03:26 +02:00
|
|
|
from datetime import timedelta
|
2023-09-12 11:13:53 +02:00
|
|
|
from typing import Any, Callable
|
2023-08-09 10:19:04 +02:00
|
|
|
|
2020-03-21 19:02:28 +03:00
|
|
|
from homeassistant.components.device_tracker.config_entry import ScannerEntity
|
2023-08-09 10:19:04 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-08-09 23:00:00 +02:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-03-27 03:29:36 +02:00
|
|
|
from homeassistant.const import STATE_NOT_HOME
|
2023-08-09 23:00:00 +02:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
entity_platform as ep,
|
|
|
|
entity_registry as er,
|
|
|
|
)
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2023-08-09 10:19:04 +02:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-08-09 23:00:00 +02:00
|
|
|
from homeassistant.util import slugify
|
2022-08-18 09:30:11 +02:00
|
|
|
from homeassistant.util.dt import utcnow
|
2023-08-09 10:19:04 +02:00
|
|
|
|
|
|
|
from homeassistant.components.device_tracker.const import SourceType
|
|
|
|
|
|
|
|
from .device_tracker_types import SENSOR_TYPES, SENSOR_SERVICES
|
2023-09-12 12:00:42 +02:00
|
|
|
from .coordinator import MikrotikCoordinator
|
2023-08-10 02:03:11 +02:00
|
|
|
from .entity import _skip_sensor, MikrotikEntity
|
2022-03-27 03:29:36 +02:00
|
|
|
from .helper import format_attribute
|
2020-03-21 19:02:28 +03:00
|
|
|
from .const import (
|
2023-08-09 23:00:00 +02:00
|
|
|
DOMAIN,
|
2020-04-10 00:03:26 +02:00
|
|
|
CONF_TRACK_HOSTS,
|
|
|
|
DEFAULT_TRACK_HOSTS,
|
|
|
|
CONF_TRACK_HOSTS_TIMEOUT,
|
|
|
|
DEFAULT_TRACK_HOST_TIMEOUT,
|
2020-03-21 19:02:28 +03:00
|
|
|
)
|
|
|
|
|
2023-08-09 10:19:04 +02:00
|
|
|
_LOGGER = getLogger(__name__)
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
|
2023-08-09 23:00:00 +02:00
|
|
|
async def async_add_entities(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry, dispatcher: dict[str, Callable]
|
|
|
|
):
|
|
|
|
"""Add entities."""
|
|
|
|
platform = ep.async_get_current_platform()
|
|
|
|
services = platform.platform.SENSOR_SERVICES
|
|
|
|
descriptions = platform.platform.SENSOR_TYPES
|
|
|
|
|
|
|
|
for service in services:
|
|
|
|
platform.async_register_entity_service(service[0], service[1], service[2])
|
|
|
|
|
|
|
|
@callback
|
|
|
|
async def async_update_controller(coordinator):
|
|
|
|
"""Update the values of the controller."""
|
2023-09-12 11:22:01 +02:00
|
|
|
if coordinator.data is None:
|
|
|
|
return
|
2023-08-09 23:00:00 +02:00
|
|
|
|
|
|
|
async def async_check_exist(obj, coordinator, uid: None) -> None:
|
|
|
|
"""Check entity exists."""
|
|
|
|
entity_registry = er.async_get(hass)
|
|
|
|
if uid:
|
|
|
|
unique_id = f"{obj._inst.lower()}-{obj.entity_description.key}-{slugify(str(obj._data[obj.entity_description.data_reference]).lower())}"
|
|
|
|
else:
|
|
|
|
unique_id = f"{obj._inst.lower()}-{obj.entity_description.key}"
|
|
|
|
|
|
|
|
entity_id = entity_registry.async_get_entity_id(
|
|
|
|
platform.domain, DOMAIN, unique_id
|
|
|
|
)
|
|
|
|
entity = entity_registry.async_get(entity_id)
|
|
|
|
if entity is None or (
|
|
|
|
(entity_id not in platform.entities) and (entity.disabled is False)
|
|
|
|
):
|
|
|
|
_LOGGER.debug("Add entity %s", entity_id)
|
|
|
|
await platform.async_add_entities([obj])
|
|
|
|
|
|
|
|
for entity_description in descriptions:
|
|
|
|
data = coordinator.data[entity_description.data_path]
|
|
|
|
if not entity_description.data_reference:
|
|
|
|
if data.get(entity_description.data_attribute) is None:
|
|
|
|
continue
|
|
|
|
obj = dispatcher[entity_description.func](
|
|
|
|
coordinator, entity_description
|
|
|
|
)
|
|
|
|
await async_check_exist(obj, coordinator, None)
|
|
|
|
else:
|
|
|
|
for uid in data:
|
|
|
|
if _skip_sensor(config_entry, entity_description, data, uid):
|
|
|
|
continue
|
|
|
|
obj = dispatcher[entity_description.func](
|
|
|
|
coordinator, entity_description, uid
|
|
|
|
)
|
|
|
|
await async_check_exist(obj, coordinator, uid)
|
|
|
|
|
|
|
|
await async_update_controller(
|
|
|
|
hass.data[DOMAIN][config_entry.entry_id].tracker_coordinator
|
|
|
|
)
|
|
|
|
|
|
|
|
unsub = async_dispatcher_connect(hass, "update_sensors", async_update_controller)
|
|
|
|
config_entry.async_on_unload(unsub)
|
|
|
|
|
|
|
|
|
2020-03-21 19:02:28 +03:00
|
|
|
# ---------------------------
|
|
|
|
# async_setup_entry
|
|
|
|
# ---------------------------
|
2023-08-09 10:19:04 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
_async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2022-03-27 03:29:36 +02:00
|
|
|
"""Set up entry for component"""
|
|
|
|
dispatcher = {
|
|
|
|
"MikrotikDeviceTracker": MikrotikDeviceTracker,
|
|
|
|
"MikrotikHostDeviceTracker": MikrotikHostDeviceTracker,
|
|
|
|
}
|
2023-08-09 10:19:04 +02:00
|
|
|
await async_add_entities(hass, config_entry, dispatcher)
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikDeviceTracker
|
2020-03-21 19:02:28 +03:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikDeviceTracker(MikrotikEntity, ScannerEntity):
|
2020-04-20 07:51:37 +02:00
|
|
|
"""Representation of a device tracker."""
|
2020-03-21 19:02:28 +03:00
|
|
|
|
2023-09-12 12:00:42 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: MikrotikCoordinator,
|
|
|
|
entity_description,
|
|
|
|
uid: str | None = None,
|
|
|
|
):
|
|
|
|
"""Initialize entity"""
|
|
|
|
super().__init__(coordinator, entity_description, uid)
|
|
|
|
self._attr_name = None
|
|
|
|
|
2020-03-21 19:02:28 +03:00
|
|
|
@property
|
2022-02-05 00:56:30 +01:00
|
|
|
def ip_address(self) -> str:
|
|
|
|
"""Return the primary ip address of the device."""
|
2022-03-27 03:29:36 +02:00
|
|
|
return self._data["address"] if "address" in self._data else None
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
@property
|
2022-02-05 00:56:30 +01:00
|
|
|
def mac_address(self) -> str:
|
|
|
|
"""Return the mac address of the device."""
|
|
|
|
if self.entity_description.data_reference in self._data:
|
|
|
|
return self._data[self.entity_description.data_reference]
|
|
|
|
|
2022-03-27 03:29:36 +02:00
|
|
|
return ""
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
@property
|
2022-02-05 00:56:30 +01:00
|
|
|
def hostname(self) -> str:
|
|
|
|
"""Return hostname of the device."""
|
|
|
|
if self.entity_description.data_name in self._data:
|
|
|
|
return self._data[self.entity_description.data_name]
|
|
|
|
|
2022-03-27 03:29:36 +02:00
|
|
|
return ""
|
2020-03-21 19:02:28 +03:00
|
|
|
|
2020-04-20 07:51:37 +02:00
|
|
|
@property
|
2022-02-05 00:56:30 +01:00
|
|
|
def is_connected(self) -> bool:
|
|
|
|
"""Return true if device is connected."""
|
2022-03-27 03:29:36 +02:00
|
|
|
return self._data[self.entity_description.data_attribute]
|
2020-04-07 03:55:38 +02:00
|
|
|
|
2020-12-25 23:31:51 +01:00
|
|
|
@property
|
2022-02-05 00:56:30 +01:00
|
|
|
def source_type(self) -> str:
|
|
|
|
"""Return the source type of the port."""
|
2023-08-09 10:19:04 +02:00
|
|
|
return SourceType.ROUTER
|
2022-02-05 00:56:30 +01:00
|
|
|
|
2020-04-07 03:55:38 +02:00
|
|
|
|
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikHostDeviceTracker
|
2020-04-07 03:55:38 +02:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikHostDeviceTracker(MikrotikDeviceTracker):
|
2020-04-07 03:55:38 +02:00
|
|
|
"""Representation of a network device."""
|
|
|
|
|
2020-04-10 00:03:26 +02:00
|
|
|
@property
|
|
|
|
def option_track_network_hosts(self):
|
|
|
|
"""Config entry option to not track ARP."""
|
|
|
|
return self._config_entry.options.get(CONF_TRACK_HOSTS, DEFAULT_TRACK_HOSTS)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def option_track_network_hosts_timeout(self):
|
|
|
|
"""Config entry option scan interval."""
|
|
|
|
track_network_hosts_timeout = self._config_entry.options.get(
|
|
|
|
CONF_TRACK_HOSTS_TIMEOUT, DEFAULT_TRACK_HOST_TIMEOUT
|
|
|
|
)
|
|
|
|
return timedelta(seconds=track_network_hosts_timeout)
|
|
|
|
|
2020-04-07 03:55:38 +02:00
|
|
|
@property
|
2020-12-25 20:28:36 +01:00
|
|
|
def is_connected(self) -> bool:
|
2020-04-07 03:55:38 +02:00
|
|
|
"""Return true if the host is connected to the network."""
|
2020-04-10 12:30:07 +02:00
|
|
|
if not self.option_track_network_hosts:
|
|
|
|
return False
|
|
|
|
|
2020-04-10 00:03:26 +02:00
|
|
|
if self._data["source"] in ["capsman", "wireless"]:
|
2022-03-27 03:29:36 +02:00
|
|
|
return self._data[self.entity_description.data_attribute]
|
2020-04-10 00:03:26 +02:00
|
|
|
|
2022-08-11 17:02:46 +02:00
|
|
|
return bool(
|
2020-04-10 00:03:26 +02:00
|
|
|
self._data["last-seen"]
|
2022-08-11 17:02:46 +02:00
|
|
|
and utcnow() - self._data["last-seen"]
|
2020-04-10 00:03:26 +02:00
|
|
|
< self.option_track_network_hosts_timeout
|
2022-08-11 17:02:46 +02:00
|
|
|
)
|
2020-04-07 03:55:38 +02:00
|
|
|
|
|
|
|
@property
|
2020-12-25 20:28:36 +01:00
|
|
|
def icon(self) -> str:
|
2020-04-07 03:55:38 +02:00
|
|
|
"""Return the icon."""
|
2020-04-10 00:03:26 +02:00
|
|
|
if self._data["source"] in ["capsman", "wireless"]:
|
2022-03-27 03:29:36 +02:00
|
|
|
if self._data[self.entity_description.data_attribute]:
|
2022-02-05 00:56:30 +01:00
|
|
|
return self.entity_description.icon_enabled
|
2020-04-10 00:03:26 +02:00
|
|
|
else:
|
2022-02-05 00:56:30 +01:00
|
|
|
return self.entity_description.icon_disabled
|
2020-04-10 00:03:26 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
self._data["last-seen"]
|
|
|
|
and (utcnow() - self._data["last-seen"])
|
|
|
|
< self.option_track_network_hosts_timeout
|
|
|
|
):
|
2022-02-05 00:56:30 +01:00
|
|
|
return self.entity_description.icon_enabled
|
|
|
|
return self.entity_description.icon_disabled
|
2020-04-07 03:55:38 +02:00
|
|
|
|
2021-12-16 22:45:29 +01:00
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
"""Return the state of the device."""
|
2023-08-09 10:19:04 +02:00
|
|
|
return self.coordinator.option_zone if self.is_connected else STATE_NOT_HOME
|
2022-05-26 10:06:06 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
|
|
|
"""Return the state attributes."""
|
|
|
|
attributes = super().extra_state_attributes
|
|
|
|
if self.is_connected:
|
|
|
|
attributes[format_attribute("last-seen")] = "Now"
|
|
|
|
|
|
|
|
if not attributes[format_attribute("last-seen")]:
|
|
|
|
attributes[format_attribute("last-seen")] = "Unknown"
|
|
|
|
|
|
|
|
return attributes
|