2019-12-02 01:13:28 +01:00
|
|
|
"""Support for the Mikrotik Router device tracker."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
from homeassistant.components.device_tracker.config_entry import ScannerEntity
|
|
|
|
from homeassistant.components.device_tracker.const import SOURCE_TYPE_ROUTER
|
2019-12-02 04:19:40 +01:00
|
|
|
from homeassistant.const import (
|
2019-12-02 18:13:55 +01:00
|
|
|
CONF_NAME,
|
|
|
|
ATTR_ATTRIBUTION,
|
2019-12-02 04:19:40 +01:00
|
|
|
)
|
2019-12-02 03:19:07 +01:00
|
|
|
from .const import (
|
2019-12-02 18:13:55 +01:00
|
|
|
DOMAIN,
|
|
|
|
DATA_CLIENT,
|
|
|
|
ATTRIBUTION,
|
2019-12-02 03:19:07 +01:00
|
|
|
)
|
|
|
|
|
2019-12-02 01:13:28 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEVICE_ATTRIBUTES = [
|
2019-12-02 18:13:55 +01: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",
|
2019-12-02 01:13:28 +01:00
|
|
|
]
|
|
|
|
|
2019-12-07 20:50:00 +01:00
|
|
|
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
|
|
|
|
|
2019-12-02 03:19:07 +01:00
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
# async_setup_entry
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Set up device tracker for Mikrotik Router component."""
|
2019-12-06 01:22:34 +01:00
|
|
|
inst = config_entry.data[CONF_NAME]
|
2019-12-02 18:13:55 +01:00
|
|
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
|
|
|
tracked = {}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@callback
|
|
|
|
def update_controller():
|
|
|
|
"""Update the values of the controller."""
|
2019-12-06 01:22:34 +01:00
|
|
|
update_items(inst, mikrotik_controller, async_add_entities, tracked)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
mikrotik_controller.listeners.append(
|
|
|
|
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
|
|
|
)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
update_controller()
|
|
|
|
return
|
2019-12-02 01:13:28 +01:00
|
|
|
|
2019-12-02 03:19:07 +01:00
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
# update_items
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
@callback
|
2019-12-06 01:22:34 +01:00
|
|
|
def update_items(inst, mikrotik_controller, async_add_entities, tracked):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Update tracked device state from the controller."""
|
|
|
|
new_tracked = []
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
for uid in mikrotik_controller.data['interface']:
|
|
|
|
if mikrotik_controller.data['interface'][uid]['type'] == "ether":
|
2019-12-06 01:22:34 +01:00
|
|
|
item_id = "{}-{}".format(inst, mikrotik_controller.data['interface'][uid]['default-name'])
|
2019-12-02 18:13:55 +01:00
|
|
|
if item_id in tracked:
|
|
|
|
if tracked[item_id].enabled:
|
|
|
|
tracked[item_id].async_schedule_update_ha_state()
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:22:34 +01:00
|
|
|
tracked[item_id] = MikrotikControllerPortDeviceTracker(inst, uid, mikrotik_controller)
|
2019-12-02 18:13:55 +01:00
|
|
|
new_tracked.append(tracked[item_id])
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
if new_tracked:
|
|
|
|
async_add_entities(new_tracked)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return
|
2019-12-02 01:13:28 +01:00
|
|
|
|
2019-12-02 03:19:07 +01:00
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
# MikrotikControllerPortDeviceTracker
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
class MikrotikControllerPortDeviceTracker(ScannerEntity):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Representation of a network port."""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:22:34 +01:00
|
|
|
def __init__(self, inst, uid, mikrotik_controller):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Set up tracked port."""
|
2019-12-06 01:22:34 +01:00
|
|
|
self._inst = inst
|
|
|
|
self._ctrl = mikrotik_controller
|
|
|
|
self._data = mikrotik_controller.data['interface'][uid]
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
self._attrs = {
|
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
|
|
|
}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@property
|
|
|
|
def entity_registry_enabled_default(self):
|
|
|
|
"""Return if the entity should be enabled when first added to the entity registry."""
|
|
|
|
return True
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Port entity created."""
|
2019-12-06 01:22:34 +01:00
|
|
|
_LOGGER.debug("New port tracker %s (%s)", self._inst, self._data['port-mac-address'])
|
2019-12-02 18:13:55 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
async def async_update(self):
|
|
|
|
"""Synchronize state with controller."""
|
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@property
|
|
|
|
def is_connected(self):
|
|
|
|
"""Return true if the port is connected to the network."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return self._data['running']
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@property
|
|
|
|
def source_type(self):
|
|
|
|
"""Return the source type of the port."""
|
|
|
|
return SOURCE_TYPE_ROUTER
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@property
|
2019-12-06 01:22:34 +01:00
|
|
|
def name(self):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Return the name of the port."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return "{} {}".format(self._inst, self._data['default-name'])
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@property
|
2019-12-06 01:22:34 +01:00
|
|
|
def unique_id(self):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Return a unique identifier for this port."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return "{}-{}".format(self._inst.lower(), self._data['port-mac-address'])
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@property
|
2019-12-06 01:22:34 +01:00
|
|
|
def available(self):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Return if controller is available."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return self._ctrl.connected()
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
2019-12-06 01:22:34 +01:00
|
|
|
if self._data['running']:
|
2019-12-02 19:22:34 +01:00
|
|
|
icon = 'mdi:lan-connect'
|
2019-12-02 18:13:55 +01:00
|
|
|
else:
|
2019-12-02 19:22:34 +01:00
|
|
|
icon = 'mdi:lan-pending'
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:22:34 +01:00
|
|
|
if not self._data['enabled']:
|
2019-12-03 01:47:07 +01:00
|
|
|
icon = 'mdi:lan-disconnect'
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 19:22:34 +01:00
|
|
|
return icon
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a port description for device registry."""
|
|
|
|
info = {
|
2019-12-06 01:22:34 +01:00
|
|
|
"connections": {(CONNECTION_NETWORK_MAC, self._data['port-mac-address'])},
|
|
|
|
"manufacturer": self._ctrl.data['resource']['platform'],
|
|
|
|
"model": self._ctrl.data['resource']['board-name'],
|
|
|
|
"name": self._data['default-name'],
|
2019-12-02 18:13:55 +01:00
|
|
|
}
|
|
|
|
return info
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the port state attributes."""
|
|
|
|
attributes = self._attrs
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
for variable in DEVICE_ATTRIBUTES:
|
2019-12-06 01:22:34 +01:00
|
|
|
if variable in self._data:
|
2019-12-07 20:50:00 +01:00
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return attributes
|