mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-23 20:14:30 +02:00
added sensors for rx/tx traffic per interface
This commit is contained in:
parent
e2c81c950f
commit
f6bcfcb528
3 changed files with 131 additions and 0 deletions
|
@ -62,6 +62,7 @@ class MikrotikControllerData():
|
|||
self.data = {'routerboard': {},
|
||||
'resource': {},
|
||||
'interface': {},
|
||||
'interface_map': {},
|
||||
'arp': {},
|
||||
'nat': {},
|
||||
'fw-update': {},
|
||||
|
@ -193,6 +194,7 @@ class MikrotikControllerData():
|
|||
# ---------------------------
|
||||
def get_interface(self):
|
||||
"""Get all interfaces data from Mikrotik"""
|
||||
interface_list = ""
|
||||
data = self.api.path("/interface")
|
||||
for entry in data:
|
||||
if 'default-name' not in entry:
|
||||
|
@ -215,12 +217,39 @@ class MikrotikControllerData():
|
|||
self.data['interface'][uid]['tx-queue-drop'] = from_entry(entry, 'tx-queue-drop')
|
||||
self.data['interface'][uid]['actual-mtu'] = from_entry(entry, 'actual-mtu')
|
||||
|
||||
self.data['interface_map'][self.data['interface'][uid]['name']] = self.data['interface'][uid]['default-name']
|
||||
|
||||
if interface_list:
|
||||
interface_list += ","
|
||||
|
||||
interface_list += self.data['interface'][uid]['name']
|
||||
|
||||
if 'client-ip-address' not in self.data['interface'][uid]:
|
||||
self.data['interface'][uid]['client-ip-address'] = ""
|
||||
|
||||
if 'client-mac-address' not in self.data['interface'][uid]:
|
||||
self.data['interface'][uid]['client-mac-address'] = ""
|
||||
|
||||
if 'rx-bits-per-second' not in self.data['interface'][uid]:
|
||||
self.data['interface'][uid]['rx-bits-per-second'] = 0
|
||||
|
||||
if 'tx-bits-per-second' not in self.data['interface'][uid]:
|
||||
self.data['interface'][uid]['tx-bits-per-second'] = 0
|
||||
|
||||
self.get_interface_traffic(interface_list)
|
||||
|
||||
return
|
||||
|
||||
# ---------------------------
|
||||
# get_interface_traffic
|
||||
# ---------------------------
|
||||
def get_interface_traffic(self, interface_list):
|
||||
data = self.api.get_traffic(interface_list)
|
||||
for entry in data:
|
||||
uid = self.data['interface_map'][from_entry(entry, 'name')]
|
||||
self.data['interface'][uid]['rx-bits-per-second'] = from_entry(entry, 'rx-bits-per-second', default=0)
|
||||
self.data['interface'][uid]['tx-bits-per-second'] = from_entry(entry, 'tx-bits-per-second', default=0)
|
||||
|
||||
return
|
||||
|
||||
# ---------------------------
|
||||
|
|
|
@ -231,3 +231,36 @@ class MikrotikAPI:
|
|||
raise ApiEntryNotFound(error)
|
||||
|
||||
return True
|
||||
|
||||
# ---------------------------
|
||||
# get_traffic
|
||||
# ---------------------------
|
||||
def get_traffic(self, interfaces):
|
||||
"""Get traffic stats"""
|
||||
traffic = None
|
||||
if not self._connected or not self._connection:
|
||||
if not self.connect():
|
||||
return None
|
||||
|
||||
response = self.path('/interface')
|
||||
if response is None:
|
||||
return None
|
||||
|
||||
args = {'interface': interfaces, 'once': True}
|
||||
try:
|
||||
traffic = response('monitor-traffic', **args)
|
||||
except librouteros.exceptions.ConnectionClosed:
|
||||
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
||||
self._connected = False
|
||||
self._connection = None
|
||||
return None
|
||||
except (
|
||||
librouteros.exceptions.TrapError,
|
||||
librouteros.exceptions.MultiTrapError,
|
||||
librouteros.exceptions.ProtocolError,
|
||||
librouteros.exceptions.FatalError
|
||||
) as api_error:
|
||||
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
|
||||
return None
|
||||
|
||||
return traffic
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
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.helpers.entity import Entity
|
||||
from homeassistant.const import (
|
||||
|
@ -53,6 +54,22 @@ SENSOR_TYPES = {
|
|||
ATTR_PATH: "resource",
|
||||
ATTR_ATTR: "hdd-usage",
|
||||
},
|
||||
'traffic_tx': {
|
||||
ATTR_DEVICE_CLASS: None,
|
||||
ATTR_ICON: "mdi:upload-network-outline",
|
||||
ATTR_LABEL: 'TX bps',
|
||||
ATTR_UNIT: "bps",
|
||||
ATTR_PATH: "interface",
|
||||
ATTR_ATTR: "tx-bits-per-second",
|
||||
},
|
||||
'traffic_rx': {
|
||||
ATTR_DEVICE_CLASS: None,
|
||||
ATTR_ICON: "mdi:download-network-outline",
|
||||
ATTR_LABEL: 'RX bps',
|
||||
ATTR_UNIT: "bps",
|
||||
ATTR_PATH: "interface",
|
||||
ATTR_ATTR: "rx-bits-per-second",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
@ -87,6 +104,9 @@ def update_items(inst, mikrotik_controller, async_add_entities, sensors):
|
|||
new_sensors = []
|
||||
|
||||
for sensor in SENSOR_TYPES:
|
||||
if "traffic_" in sensor:
|
||||
continue
|
||||
|
||||
item_id = "{}-{}".format(inst, sensor)
|
||||
if item_id in sensors:
|
||||
if sensors[item_id].enabled:
|
||||
|
@ -96,6 +116,21 @@ def update_items(inst, mikrotik_controller, async_add_entities, sensors):
|
|||
sensors[item_id] = MikrotikControllerSensor(mikrotik_controller=mikrotik_controller, inst=inst, sensor=sensor)
|
||||
new_sensors.append(sensors[item_id])
|
||||
|
||||
for sensor in SENSOR_TYPES:
|
||||
if "traffic_" not in sensor:
|
||||
continue
|
||||
|
||||
for uid in mikrotik_controller.data['interface']:
|
||||
if mikrotik_controller.data['interface'][uid]['type'] == "ether":
|
||||
item_id = "{}-{}-{}".format(inst, sensor, mikrotik_controller.data['interface'][uid]['default-name'])
|
||||
if item_id in sensors:
|
||||
if sensors[item_id].enabled:
|
||||
sensors[item_id].async_schedule_update_ha_state()
|
||||
continue
|
||||
|
||||
sensors[item_id] = MikrotikControllerTrafficSensor(mikrotik_controller=mikrotik_controller, inst=inst, sensor=sensor, uid=uid)
|
||||
new_sensors.append(sensors[item_id])
|
||||
|
||||
if new_sensors:
|
||||
async_add_entities(new_sensors, True)
|
||||
|
||||
|
@ -187,3 +222,37 @@ class MikrotikControllerSensor(Entity):
|
|||
"""Port entity created."""
|
||||
_LOGGER.debug("New sensor %s (%s)", self._inst, self._sensor)
|
||||
return
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# MikrotikControllerTrafficSensor
|
||||
# ---------------------------
|
||||
class MikrotikControllerTrafficSensor(MikrotikControllerSensor):
|
||||
"""Define an Mikrotik Controller sensor."""
|
||||
|
||||
def __init__(self, mikrotik_controller, inst, sensor, uid):
|
||||
"""Initialize."""
|
||||
super().__init__(mikrotik_controller, inst, sensor)
|
||||
self._uid = uid
|
||||
self._data = mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]][uid]
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name."""
|
||||
return "{} {} {}".format(self._inst, self._data['name'], self._type[ATTR_LABEL])
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique_id for this entity."""
|
||||
return "{}-{}-{}".format(self._inst.lower(), self._sensor.lower(), self._data['name'].lower())
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return a port description for device registry."""
|
||||
info = {
|
||||
"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'],
|
||||
}
|
||||
return info
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue