Sensor port traffic selection #68

This commit is contained in:
tomaae 2020-12-12 12:19:40 +01:00
parent 2786ed6d3f
commit f63c069940
2 changed files with 52 additions and 3 deletions

View file

@ -33,6 +33,12 @@ from .const import (
DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL,
DEFAULT_UNIT_OF_MEASUREMENT, DEFAULT_UNIT_OF_MEASUREMENT,
CONF_TRACK_HOSTS_TIMEOUT, CONF_TRACK_HOSTS_TIMEOUT,
CONF_SENSOR_PORT_TRAFFIC,
DEFAULT_SENSOR_PORT_TRAFFIC,
CONF_SENSOR_CLIENT_TRAFFIC,
DEFAULT_SENSOR_CLIENT_TRAFFIC,
CONF_SENSOR_SIMPLE_QUEUES,
DEFAULT_SENSOR_SIMPLE_QUEUES,
) )
from .exceptions import ApiEntryNotFound from .exceptions import ApiEntryNotFound
from .helper import parse_api from .helper import parse_api
@ -143,6 +149,36 @@ class MikrotikControllerData:
"""Config entry option to not track ARP.""" """Config entry option to not track ARP."""
return self.config_entry.options.get(CONF_TRACK_HOSTS, DEFAULT_TRACK_HOSTS) return self.config_entry.options.get(CONF_TRACK_HOSTS, DEFAULT_TRACK_HOSTS)
# ---------------------------
# option_sensor_port_traffic
# ---------------------------
@property
def option_sensor_port_traffic(self):
"""Config entry option to not track ARP."""
return self.config_entry.options.get(
CONF_SENSOR_PORT_TRAFFIC, DEFAULT_SENSOR_PORT_TRAFFIC
)
# ---------------------------
# option_sensor_client_traffic
# ---------------------------
@property
def option_sensor_client_traffic(self):
"""Config entry option to not track ARP."""
return self.config_entry.options.get(
CONF_SENSOR_CLIENT_TRAFFIC, DEFAULT_SENSOR_CLIENT_TRAFFIC
)
# ---------------------------
# option_sensor_simple_queues
# ---------------------------
@property
def option_sensor_simple_queues(self):
"""Config entry option to not track ARP."""
return self.config_entry.options.get(
CONF_SENSOR_SIMPLE_QUEUES, DEFAULT_SENSOR_SIMPLE_QUEUES
)
# --------------------------- # ---------------------------
# option_scan_interval # option_scan_interval
# --------------------------- # ---------------------------
@ -411,7 +447,7 @@ class MikrotikControllerData:
if self.api.connected(): if self.api.connected():
await self.async_process_host() await self.async_process_host()
if self.api.connected(): if self.api.connected() and self.option_sensor_port_traffic:
await self.hass.async_add_executor_job(self.get_interface_traffic) await self.hass.async_add_executor_job(self.get_interface_traffic)
if self.api.connected(): if self.api.connected():

View file

@ -8,6 +8,12 @@ from homeassistant.const import (
ATTR_DEVICE_CLASS, ATTR_DEVICE_CLASS,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from .const import (
CONF_SENSOR_PORT_TRAFFIC,
DEFAULT_SENSOR_PORT_TRAFFIC,
)
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -155,7 +161,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
@callback @callback
def update_controller(): def update_controller():
"""Update the values of the controller.""" """Update the values of the controller."""
update_items(inst, mikrotik_controller, async_add_entities, sensors) update_items(
inst, config_entry, mikrotik_controller, async_add_entities, sensors
)
mikrotik_controller.listeners.append( mikrotik_controller.listeners.append(
async_dispatcher_connect( async_dispatcher_connect(
@ -170,7 +178,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
# update_items # update_items
# --------------------------- # ---------------------------
@callback @callback
def update_items(inst, mikrotik_controller, async_add_entities, sensors): def update_items(inst, config_entry, mikrotik_controller, async_add_entities, sensors):
"""Update sensor state from the controller.""" """Update sensor state from the controller."""
new_sensors = [] new_sensors = []
@ -189,6 +197,11 @@ def update_items(inst, mikrotik_controller, async_add_entities, sensors):
new_sensors.append(sensors[item_id]) new_sensors.append(sensors[item_id])
if "traffic_" in sensor: if "traffic_" in sensor:
if not config_entry.options.get(
CONF_SENSOR_PORT_TRAFFIC, DEFAULT_SENSOR_PORT_TRAFFIC
):
continue
for uid in mikrotik_controller.data["interface"]: for uid in mikrotik_controller.data["interface"]:
if mikrotik_controller.data["interface"][uid]["type"] != "bridge": if mikrotik_controller.data["interface"][uid]["type"] != "bridge":
item_id = f"{inst}-{sensor}-{mikrotik_controller.data['interface'][uid]['default-name']}" item_id = f"{inst}-{sensor}-{mikrotik_controller.data['interface'][uid]['default-name']}"