Added PPP user connection tracker #76

This commit is contained in:
tomaae 2020-12-25 11:48:36 +01:00
parent 8cf577b780
commit 53f2f63a73

View file

@ -24,6 +24,8 @@ from .const import (
DEFAULT_TRACK_HOST_TIMEOUT,
CONF_SENSOR_PORT_TRACKER,
DEFAULT_SENSOR_PORT_TRACKER,
CONF_SENSOR_PPP,
DEFAULT_SENSOR_PPP,
)
_LOGGER = logging.getLogger(__name__)
@ -54,6 +56,15 @@ DEVICE_ATTRIBUTES_HOST = [
"last-seen",
]
DEVICE_ATTRIBUTES_PPP_SECRET = [
"connected",
"service",
"profile",
"comment",
"caller-id",
"encoding",
]
# ---------------------------
# format_attribute
@ -115,15 +126,19 @@ def update_items(inst, config_entry, mikrotik_controller, async_add_entities, tr
# Add switches
for sid, sid_uid, sid_name, sid_ref, sid_func in zip(
# Data point name
["interface", "host"],
["interface", "host", "ppp_secret"],
# Data point unique id
["default-name", "mac-address"],
["default-name", "mac-address", "name"],
# Entry Name
["name", "host-name"],
["name", "host-name", "name"],
# Entry Unique id
["port-mac-address", "mac-address"],
["port-mac-address", "mac-address", "name"],
# Tracker function
[MikrotikControllerPortDeviceTracker, MikrotikControllerHostDeviceTracker],
[
MikrotikControllerPortDeviceTracker,
MikrotikControllerHostDeviceTracker,
MikrotikControllerPPPSecretDeviceTracker,
],
):
if (
sid_func == MikrotikControllerPortDeviceTracker
@ -410,3 +425,74 @@ class MikrotikControllerHostDeviceTracker(MikrotikControllerDeviceTracker):
info["name"] = f"{self._inst} {self._data[self._sid_data['sid_name']]}"
return info
# ---------------------------
# MikrotikControllerPPPSecretDeviceTracker
# ---------------------------
class MikrotikControllerPPPSecretDeviceTracker(MikrotikControllerDeviceTracker):
"""Representation of a network device."""
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 option_sensor_ppp(self):
"""Config entry option to not track ARP."""
return self._config_entry.options.get(CONF_SENSOR_PPP, DEFAULT_SENSOR_PPP)
@property
def name(self):
"""Return the name of the port."""
return f"{self._inst} PPP {self._data['name']}"
@property
def is_connected(self):
"""Return true if the host is connected to the network."""
if not self.option_sensor_ppp:
return False
return self._data["connected"]
@property
def available(self) -> bool:
"""Return if controller is available."""
if not self.option_sensor_ppp:
return False
return self._ctrl.connected()
@property
def unique_id(self):
"""Return a unique identifier for this port."""
return f"{self._inst.lower()}-{self._sid_data['sid']}_tracker-{self._data[self._sid_data['sid_ref']]}"
@property
def icon(self):
"""Return the icon."""
if self._data["connected"]:
return "mdi:account-network-outline"
else:
return "mdi:account-off-outline"
@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
@property
def device_info(self):
"""Return a description for device registry."""
info = {
"connections": {
(CONNECTION_NETWORK_MAC, self._data[self._sid_data["sid_ref"]])
},
"name": self._data[self._sid_data["sid_name"]],
}
return info