2020-03-21 19:02:28 +03:00
|
|
|
"""Support for the Mikrotik Router device tracker."""
|
|
|
|
|
|
|
|
import logging
|
2020-04-10 00:03:26 +02:00
|
|
|
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 (
|
|
|
|
CONF_NAME,
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
)
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2020-04-10 00:03:26 +02:00
|
|
|
from homeassistant.util.dt import get_age, utcnow
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
DOMAIN,
|
|
|
|
DATA_CLIENT,
|
|
|
|
ATTRIBUTION,
|
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
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-04-07 03:55:38 +02:00
|
|
|
DEVICE_ATTRIBUTES_IFACE = [
|
2020-03-21 19:02:28 +03:00
|
|
|
"running",
|
|
|
|
"enabled",
|
|
|
|
"comment",
|
|
|
|
"client-ip-address",
|
|
|
|
"client-mac-address",
|
|
|
|
"port-mac-address",
|
|
|
|
"last-link-down-time",
|
|
|
|
"last-link-up-time",
|
|
|
|
"link-downs",
|
|
|
|
"actual-mtu",
|
|
|
|
"type",
|
|
|
|
"name",
|
|
|
|
"default-name",
|
2020-12-02 09:28:56 +01:00
|
|
|
"poe-out",
|
2020-03-21 19:02:28 +03:00
|
|
|
]
|
|
|
|
|
2020-04-07 03:55:38 +02:00
|
|
|
DEVICE_ATTRIBUTES_HOST = [
|
2020-04-08 16:53:20 +02:00
|
|
|
"host-name",
|
2020-04-07 04:15:23 +02:00
|
|
|
"address",
|
|
|
|
"mac-address",
|
|
|
|
"interface",
|
2020-04-10 00:03:26 +02:00
|
|
|
"source",
|
2020-04-07 03:55:38 +02:00
|
|
|
"last-seen",
|
|
|
|
]
|
|
|
|
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# format_attribute
|
|
|
|
# ---------------------------
|
|
|
|
def format_attribute(attr):
|
|
|
|
res = attr.replace("-", " ")
|
|
|
|
res = res.capitalize()
|
|
|
|
res = res.replace(" ip ", " IP ")
|
|
|
|
res = res.replace(" mac ", " MAC ")
|
|
|
|
res = res.replace(" mtu", " MTU")
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2020-04-10 00:03:26 +02:00
|
|
|
# ---------------------------
|
|
|
|
# format_value
|
|
|
|
# ---------------------------
|
|
|
|
def format_value(res):
|
|
|
|
res = res.replace("dhcp", "DHCP")
|
|
|
|
res = res.replace("dns", "DNS")
|
|
|
|
res = res.replace("capsman", "CAPsMAN")
|
|
|
|
res = res.replace("wireless", "Wireless")
|
|
|
|
res = res.replace("restored", "Restored")
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2020-03-21 19:02:28 +03:00
|
|
|
# ---------------------------
|
|
|
|
# async_setup_entry
|
|
|
|
# ---------------------------
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up device tracker for Mikrotik Router component."""
|
|
|
|
inst = config_entry.data[CONF_NAME]
|
|
|
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
|
|
|
tracked = {}
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_controller():
|
|
|
|
"""Update the values of the controller."""
|
2020-04-11 05:45:36 +02:00
|
|
|
update_items(
|
|
|
|
inst, config_entry, mikrotik_controller, async_add_entities, tracked
|
|
|
|
)
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
mikrotik_controller.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
hass, mikrotik_controller.signal_update, update_controller
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
update_controller()
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# update_items
|
|
|
|
# ---------------------------
|
|
|
|
@callback
|
2020-04-10 00:03:26 +02:00
|
|
|
def update_items(inst, config_entry, mikrotik_controller, async_add_entities, tracked):
|
2020-03-21 19:02:28 +03:00
|
|
|
"""Update tracked device state from the controller."""
|
|
|
|
new_tracked = []
|
|
|
|
|
2020-04-07 03:55:38 +02:00
|
|
|
# Add switches
|
2020-04-20 07:51:37 +02:00
|
|
|
for sid, sid_uid, sid_name, sid_ref, sid_func in zip(
|
|
|
|
# Data point name
|
2020-04-08 09:44:00 +02:00
|
|
|
["interface", "host"],
|
2020-04-20 07:51:37 +02:00
|
|
|
# Data point unique id
|
2020-04-07 03:55:38 +02:00
|
|
|
["default-name", "mac-address"],
|
2020-04-20 07:51:37 +02:00
|
|
|
# Entry Name
|
2020-04-20 08:34:21 +02:00
|
|
|
["name", "host-name"],
|
2020-04-20 07:51:37 +02:00
|
|
|
# Entry Unique id
|
|
|
|
["port-mac-address", "mac-address"],
|
|
|
|
# Tracker function
|
2020-04-11 05:45:36 +02:00
|
|
|
[MikrotikControllerPortDeviceTracker, MikrotikControllerHostDeviceTracker],
|
2020-04-07 03:55:38 +02:00
|
|
|
):
|
|
|
|
for uid in mikrotik_controller.data[sid]:
|
2020-04-13 08:13:26 +02:00
|
|
|
if (
|
2020-04-20 07:51:37 +02:00
|
|
|
# Skip if interface is wlan
|
2020-04-13 08:13:26 +02:00
|
|
|
sid == "interface"
|
|
|
|
and mikrotik_controller.data[sid][uid]["type"] == "wlan"
|
2020-04-20 07:51:37 +02:00
|
|
|
) or (
|
|
|
|
# Skip if host tracking is disabled
|
|
|
|
sid == "host"
|
|
|
|
and not config_entry.options.get(CONF_TRACK_HOSTS, DEFAULT_TRACK_HOSTS)
|
2020-04-13 08:13:26 +02:00
|
|
|
):
|
|
|
|
continue
|
|
|
|
|
2020-04-20 07:51:37 +02:00
|
|
|
# Update entity
|
2020-04-07 03:55:38 +02:00
|
|
|
item_id = f"{inst}-{sid}-{mikrotik_controller.data[sid][uid][sid_uid]}"
|
2020-04-04 19:46:40 +02:00
|
|
|
_LOGGER.debug("Updating device_tracker %s", item_id)
|
2020-03-21 19:02:28 +03:00
|
|
|
if item_id in tracked:
|
|
|
|
if tracked[item_id].enabled:
|
|
|
|
tracked[item_id].async_schedule_update_ha_state()
|
|
|
|
continue
|
|
|
|
|
2020-04-20 07:51:37 +02:00
|
|
|
# Create new entity
|
|
|
|
sid_data = {
|
|
|
|
"sid": sid,
|
|
|
|
"sid_uid": sid_uid,
|
|
|
|
"sid_name": sid_name,
|
|
|
|
"sid_ref": sid_ref,
|
|
|
|
}
|
|
|
|
tracked[item_id] = sid_func(
|
|
|
|
inst, uid, mikrotik_controller, config_entry, sid_data
|
|
|
|
)
|
2020-03-21 19:02:28 +03:00
|
|
|
new_tracked.append(tracked[item_id])
|
|
|
|
|
2020-04-20 07:51:37 +02:00
|
|
|
# Register new entities
|
2020-03-21 19:02:28 +03:00
|
|
|
if new_tracked:
|
|
|
|
async_add_entities(new_tracked)
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
2020-04-20 07:51:37 +02:00
|
|
|
# MikrotikControllerDeviceTracker
|
2020-03-21 19:02:28 +03:00
|
|
|
# ---------------------------
|
2020-04-20 07:51:37 +02:00
|
|
|
class MikrotikControllerDeviceTracker(ScannerEntity):
|
|
|
|
"""Representation of a device tracker."""
|
2020-03-21 19:02:28 +03:00
|
|
|
|
2020-04-20 07:51:37 +02:00
|
|
|
def __init__(self, inst, uid, mikrotik_controller, config_entry, sid_data):
|
|
|
|
"""Set up a device tracker."""
|
2020-04-20 08:03:02 +02:00
|
|
|
self._sid_data = sid_data
|
2020-03-21 19:02:28 +03:00
|
|
|
self._inst = inst
|
|
|
|
self._ctrl = mikrotik_controller
|
2020-04-20 08:03:02 +02:00
|
|
|
self._data = mikrotik_controller.data[self._sid_data["sid"]][uid]
|
2020-04-20 07:51:37 +02:00
|
|
|
self._config_entry = config_entry
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
self._attrs = {
|
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_registry_enabled_default(self):
|
|
|
|
"""Return if the entity should be enabled when first added to the entity registry."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
2020-04-20 07:51:37 +02:00
|
|
|
"""Device tracker entity created."""
|
2020-03-21 19:02:28 +03:00
|
|
|
_LOGGER.debug(
|
2020-04-20 07:51:37 +02:00
|
|
|
"New device tracker %s (%s %s)",
|
2020-03-21 19:02:28 +03:00
|
|
|
self._inst,
|
2020-04-20 08:03:02 +02:00
|
|
|
self._sid_data["sid"],
|
|
|
|
self._data[self._sid_data["sid_uid"]],
|
2020-03-21 19:02:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Synchronize state with controller."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_type(self):
|
|
|
|
"""Return the source type of the port."""
|
|
|
|
return SOURCE_TYPE_ROUTER
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the port."""
|
2020-04-20 08:03:02 +02:00
|
|
|
if self._sid_data["sid"] == "interface":
|
|
|
|
return f"{self._inst} {self._data[self._sid_data['sid_name']]}"
|
2020-04-20 07:51:37 +02:00
|
|
|
|
2020-04-20 08:03:02 +02:00
|
|
|
return f"{self._data[self._sid_data['sid_name']]}"
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique identifier for this port."""
|
2020-04-20 08:03:02 +02:00
|
|
|
return f"{self._inst.lower()}-{self._sid_data['sid']}-{self._data[self._sid_data['sid_ref']]}"
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if controller is available."""
|
|
|
|
return self._ctrl.connected()
|
|
|
|
|
2020-04-20 07:51:37 +02:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a description for device registry."""
|
|
|
|
info = {
|
|
|
|
"connections": {
|
2020-04-20 08:03:02 +02:00
|
|
|
(CONNECTION_NETWORK_MAC, self._data[self._sid_data["sid_ref"]])
|
2020-04-20 07:51:37 +02:00
|
|
|
},
|
|
|
|
"manufacturer": self._ctrl.data["resource"]["platform"],
|
|
|
|
"model": self._ctrl.data["resource"]["board-name"],
|
2020-04-20 08:34:21 +02:00
|
|
|
"name": self._data[self._sid_data["sid_name"]],
|
2020-04-20 07:51:37 +02:00
|
|
|
}
|
2020-04-20 08:34:21 +02:00
|
|
|
if self._sid_data["sid"] == "interface":
|
|
|
|
info["name"] = f"{self._inst} {self._data[self._sid_data['sid_name']]}"
|
2020-04-20 07:51:37 +02:00
|
|
|
return info
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the port state attributes."""
|
|
|
|
attributes = self._attrs
|
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# MikrotikControllerPortDeviceTracker
|
|
|
|
# ---------------------------
|
|
|
|
class MikrotikControllerPortDeviceTracker(MikrotikControllerDeviceTracker):
|
|
|
|
"""Representation of a network port."""
|
|
|
|
|
|
|
|
def __init__(self, inst, uid, mikrotik_controller, config_entry, sid_data):
|
|
|
|
"""Set up tracked port."""
|
|
|
|
super().__init__(inst, uid, mikrotik_controller, config_entry, sid_data)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_connected(self):
|
|
|
|
"""Return true if the port is connected to the network."""
|
|
|
|
return self._data["running"]
|
|
|
|
|
2020-12-02 11:43:10 +01:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique identifier for this port."""
|
|
|
|
return f"{self._inst.lower()}-{self._sid_data['sid']}-{self._data['port-mac-address']}_{self._data['default-name']}"
|
|
|
|
|
2020-03-21 19:02:28 +03:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
if self._data["running"]:
|
|
|
|
icon = "mdi:lan-connect"
|
|
|
|
else:
|
|
|
|
icon = "mdi:lan-pending"
|
|
|
|
|
|
|
|
if not self._data["enabled"]:
|
|
|
|
icon = "mdi:lan-disconnect"
|
|
|
|
|
|
|
|
return icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the port state attributes."""
|
|
|
|
attributes = self._attrs
|
2020-04-07 03:55:38 +02:00
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# MikrotikControllerHostDeviceTracker
|
|
|
|
# ---------------------------
|
2020-04-20 07:51:37 +02:00
|
|
|
class MikrotikControllerHostDeviceTracker(MikrotikControllerDeviceTracker):
|
2020-04-07 03:55:38 +02:00
|
|
|
"""Representation of a network device."""
|
|
|
|
|
2020-04-20 07:51:37 +02:00
|
|
|
def __init__(self, inst, uid, mikrotik_controller, config_entry, sid_data):
|
2020-04-07 03:55:38 +02:00
|
|
|
"""Set up tracked port."""
|
2020-04-20 07:51:37 +02:00
|
|
|
super().__init__(inst, uid, mikrotik_controller, config_entry, sid_data)
|
2020-04-07 03:55:38 +02:00
|
|
|
|
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
|
|
|
|
def is_connected(self):
|
|
|
|
"""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"]:
|
|
|
|
return self._data["available"]
|
|
|
|
|
|
|
|
if (
|
|
|
|
self._data["last-seen"]
|
|
|
|
and (utcnow() - self._data["last-seen"])
|
|
|
|
< self.option_track_network_hosts_timeout
|
|
|
|
):
|
|
|
|
return True
|
|
|
|
return False
|
2020-04-07 03:55:38 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if controller is available."""
|
2020-04-10 00:03:26 +02:00
|
|
|
if not self.option_track_network_hosts:
|
|
|
|
return False
|
|
|
|
|
2020-04-07 03:55:38 +02:00
|
|
|
return self._ctrl.connected()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
2020-04-10 00:03:26 +02:00
|
|
|
if self._data["source"] in ["capsman", "wireless"]:
|
|
|
|
if self._data["available"]:
|
|
|
|
return "mdi:lan-connect"
|
|
|
|
else:
|
|
|
|
return "mdi:lan-disconnect"
|
|
|
|
|
|
|
|
if (
|
|
|
|
self._data["last-seen"]
|
|
|
|
and (utcnow() - self._data["last-seen"])
|
|
|
|
< self.option_track_network_hosts_timeout
|
|
|
|
):
|
|
|
|
return "mdi:lan-connect"
|
|
|
|
return "mdi:lan-disconnect"
|
2020-04-07 03:55:38 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the host state attributes."""
|
|
|
|
attributes = self._attrs
|
|
|
|
for variable in DEVICE_ATTRIBUTES_HOST:
|
2020-04-20 07:51:37 +02:00
|
|
|
if variable not in self._data:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if variable == "last-seen":
|
|
|
|
if self._data[variable]:
|
|
|
|
attributes[format_attribute(variable)] = get_age(
|
|
|
|
self._data[variable]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
attributes[format_attribute(variable)] = "unknown"
|
|
|
|
else:
|
|
|
|
if self._data[variable] in [
|
|
|
|
"dhcp",
|
|
|
|
"dns",
|
|
|
|
"capsman",
|
|
|
|
"wireless",
|
|
|
|
"restored",
|
|
|
|
]:
|
|
|
|
attributes[format_attribute(variable)] = format_value(
|
|
|
|
self._data[variable]
|
|
|
|
)
|
2020-04-08 09:48:16 +02:00
|
|
|
else:
|
2020-04-20 07:51:37 +02:00
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
return attributes
|
2020-12-02 14:57:30 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a description for device registry."""
|
|
|
|
info = {
|
|
|
|
"connections": {
|
|
|
|
(CONNECTION_NETWORK_MAC, self._data[self._sid_data["sid_ref"]])
|
|
|
|
},
|
|
|
|
"default_name": self._data[self._sid_data["sid_name"]],
|
|
|
|
}
|
2020-12-03 16:41:06 +01:00
|
|
|
if self._data["manufacturer"] != "":
|
|
|
|
info["manufacturer"] = self._data["manufacturer"]
|
|
|
|
|
2020-12-02 14:57:30 +01:00
|
|
|
if self._sid_data["sid"] == "interface":
|
|
|
|
info["name"] = f"{self._inst} {self._data[self._sid_data['sid_name']]}"
|
2020-12-03 16:41:06 +01:00
|
|
|
|
2020-12-02 15:38:17 +01:00
|
|
|
return info
|