tomaae.homeassistant-mikrot.../custom_components/mikrotik_router/device_tracker.py

152 lines
4.8 KiB
Python
Raw Normal View History

2020-03-21 19:02:28 +03:00
"""Support for the Mikrotik Router device tracker."""
import logging
from typing import Any
2022-02-05 00:56:30 +01:00
from collections.abc import Mapping
from datetime import timedelta
2020-03-21 19:02:28 +03:00
from homeassistant.components.device_tracker.config_entry import ScannerEntity
from homeassistant.components.device_tracker.const import SOURCE_TYPE_ROUTER
from homeassistant.const import STATE_NOT_HOME
from homeassistant.util.dt import get_age, utcnow
from .helper import format_attribute
2020-03-21 19:02:28 +03:00
from .const import (
CONF_TRACK_HOSTS,
DEFAULT_TRACK_HOSTS,
CONF_TRACK_HOSTS_TIMEOUT,
DEFAULT_TRACK_HOST_TIMEOUT,
2020-03-21 19:02:28 +03:00
)
from .model import model_async_setup_entry, MikrotikEntity
from .device_tracker_types import SENSOR_TYPES, SENSOR_SERVICES
2020-03-21 19:02:28 +03:00
_LOGGER = logging.getLogger(__name__)
# ---------------------------
# async_setup_entry
# ---------------------------
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up entry for component"""
dispatcher = {
"MikrotikDeviceTracker": MikrotikDeviceTracker,
"MikrotikHostDeviceTracker": MikrotikHostDeviceTracker,
}
await model_async_setup_entry(
hass,
config_entry,
async_add_entities,
SENSOR_SERVICES,
SENSOR_TYPES,
dispatcher,
2020-03-21 19:02:28 +03:00
)
# ---------------------------
# MikrotikDeviceTracker
2020-03-21 19:02:28 +03: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
@property
2022-02-05 00:56:30 +01:00
def ip_address(self) -> str:
"""Return the primary ip address of the device."""
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]
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]
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."""
return self._data[self.entity_description.data_attribute]
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."""
return SOURCE_TYPE_ROUTER
# ---------------------------
# MikrotikHostDeviceTracker
# ---------------------------
class MikrotikHostDeviceTracker(MikrotikDeviceTracker):
"""Representation of a network device."""
@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)
2022-02-05 00:56:30 +01:00
@property
def name(self) -> str:
"""Return the name."""
return f"{self._data[self.entity_description.data_name]}"
@property
2020-12-25 20:28:36 +01:00
def is_connected(self) -> bool:
"""Return true if the host is connected to the network."""
if not self.option_track_network_hosts:
return False
if self._data["source"] in ["capsman", "wireless"]:
return self._data[self.entity_description.data_attribute]
2022-08-11 17:02:46 +02:00
return bool(
self._data["last-seen"]
2022-08-11 17:02:46 +02:00
and utcnow() - self._data["last-seen"]
< self.option_track_network_hosts_timeout
2022-08-11 17:02:46 +02:00
)
@property
2020-12-25 20:28:36 +01:00
def icon(self) -> str:
"""Return the icon."""
if self._data["source"] in ["capsman", "wireless"]:
if self._data[self.entity_description.data_attribute]:
2022-02-05 00:56:30 +01:00
return self.entity_description.icon_enabled
else:
2022-02-05 00:56:30 +01:00
return self.entity_description.icon_disabled
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
@property
def state(self) -> str:
"""Return the state of the device."""
return self._ctrl.option_zone if self.is_connected else STATE_NOT_HOME
@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