mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-15 11:54:30 +02:00
Moved port tracker to binary sensors
This commit is contained in:
parent
e4926b9a67
commit
7379199d40
2 changed files with 128 additions and 88 deletions
|
@ -12,6 +12,7 @@ from homeassistant.const import (
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
@ -19,6 +20,8 @@ from .const import (
|
||||||
ATTRIBUTION,
|
ATTRIBUTION,
|
||||||
CONF_SENSOR_PPP,
|
CONF_SENSOR_PPP,
|
||||||
DEFAULT_SENSOR_PPP,
|
DEFAULT_SENSOR_PPP,
|
||||||
|
CONF_SENSOR_PORT_TRACKER,
|
||||||
|
DEFAULT_SENSOR_PORT_TRACKER,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -37,6 +40,23 @@ SENSOR_TYPES = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEVICE_ATTRIBUTES_IFACE = [
|
||||||
|
"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",
|
||||||
|
"poe-out",
|
||||||
|
]
|
||||||
|
|
||||||
DEVICE_ATTRIBUTES_PPP_SECRET = [
|
DEVICE_ATTRIBUTES_PPP_SECRET = [
|
||||||
"connected",
|
"connected",
|
||||||
"service",
|
"service",
|
||||||
|
@ -106,19 +126,33 @@ def update_items(inst, config_entry, mikrotik_controller, async_add_entities, se
|
||||||
# Add switches
|
# Add switches
|
||||||
for sid, sid_uid, sid_name, sid_ref, sid_func in zip(
|
for sid, sid_uid, sid_name, sid_ref, sid_func in zip(
|
||||||
# Data point name
|
# Data point name
|
||||||
["ppp_secret"],
|
["ppp_secret", "interface"],
|
||||||
# Data point unique id
|
# Data point unique id
|
||||||
["name"],
|
["name", "default-name"],
|
||||||
# Entry Name
|
# Entry Name
|
||||||
["name"],
|
["name", "name"],
|
||||||
# Entry Unique id
|
# Entry Unique id
|
||||||
["name"],
|
["name", "port-mac-address"],
|
||||||
# Tracker function
|
# Tracker function
|
||||||
[
|
[
|
||||||
MikrotikControllerPPPSecretBinarySensor,
|
MikrotikControllerPPPSecretBinarySensor,
|
||||||
|
MikrotikControllerPortBinarySensor,
|
||||||
],
|
],
|
||||||
):
|
):
|
||||||
|
if (
|
||||||
|
sid_func == MikrotikControllerPortBinarySensor
|
||||||
|
and not config_entry.options.get(
|
||||||
|
CONF_SENSOR_PORT_TRACKER, DEFAULT_SENSOR_PORT_TRACKER
|
||||||
|
)
|
||||||
|
):
|
||||||
|
continue
|
||||||
for uid in mikrotik_controller.data[sid]:
|
for uid in mikrotik_controller.data[sid]:
|
||||||
|
if (
|
||||||
|
# Skip if interface is wlan
|
||||||
|
sid == "interface"
|
||||||
|
and mikrotik_controller.data[sid][uid]["type"] == "wlan"
|
||||||
|
):
|
||||||
|
continue
|
||||||
# Update entity
|
# Update entity
|
||||||
item_id = f"{inst}-{sid}-{mikrotik_controller.data[sid][uid][sid_uid]}"
|
item_id = f"{inst}-{sid}-{mikrotik_controller.data[sid][uid][sid_uid]}"
|
||||||
_LOGGER.debug("Updating binary_sensor %s", item_id)
|
_LOGGER.debug("Updating binary_sensor %s", item_id)
|
||||||
|
@ -318,3 +352,89 @@ class MikrotikControllerPPPSecretBinarySensor(MikrotikControllerBinarySensor):
|
||||||
"name": f"{self._inst} PPP",
|
"name": f"{self._inst} PPP",
|
||||||
}
|
}
|
||||||
return info
|
return info
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------
|
||||||
|
# MikrotikControllerPortBinarySensor
|
||||||
|
# ---------------------------
|
||||||
|
class MikrotikControllerPortBinarySensor(MikrotikControllerBinarySensor):
|
||||||
|
"""Representation of a network port."""
|
||||||
|
|
||||||
|
def __init__(self, inst, uid, mikrotik_controller, config_entry, sid_data):
|
||||||
|
"""Set up tracked port."""
|
||||||
|
super().__init__(mikrotik_controller, inst, uid)
|
||||||
|
self._sid_data = sid_data
|
||||||
|
self._data = mikrotik_controller.data[self._sid_data["sid"]][uid]
|
||||||
|
self._config_entry = config_entry
|
||||||
|
|
||||||
|
@property
|
||||||
|
def option_sensor_port_tracker(self):
|
||||||
|
"""Config entry option to not track ARP."""
|
||||||
|
return self._config_entry.options.get(
|
||||||
|
CONF_SENSOR_PORT_TRACKER, DEFAULT_SENSOR_PORT_TRACKER
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
"""Return the name of the port."""
|
||||||
|
return f"{self._inst} {self._data[self._sid_data['sid_name']]}"
|
||||||
|
|
||||||
|
@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']}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self):
|
||||||
|
"""Return true if the port is connected to the network."""
|
||||||
|
return self._data["running"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self) -> str:
|
||||||
|
"""Return the device class."""
|
||||||
|
return DEVICE_CLASS_CONNECTIVITY
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return if controller is available."""
|
||||||
|
if not self.option_sensor_port_tracker:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return self._ctrl.connected()
|
||||||
|
|
||||||
|
@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
|
||||||
|
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"]])
|
||||||
|
},
|
||||||
|
"manufacturer": self._ctrl.data["resource"]["platform"],
|
||||||
|
"model": self._ctrl.data["resource"]["board-name"],
|
||||||
|
"name": f"{self._inst} {self._data[self._sid_data['sid_name']]}",
|
||||||
|
}
|
||||||
|
|
||||||
|
return info
|
||||||
|
|
|
@ -22,29 +22,10 @@ from .const import (
|
||||||
DEFAULT_TRACK_HOSTS,
|
DEFAULT_TRACK_HOSTS,
|
||||||
CONF_TRACK_HOSTS_TIMEOUT,
|
CONF_TRACK_HOSTS_TIMEOUT,
|
||||||
DEFAULT_TRACK_HOST_TIMEOUT,
|
DEFAULT_TRACK_HOST_TIMEOUT,
|
||||||
CONF_SENSOR_PORT_TRACKER,
|
|
||||||
DEFAULT_SENSOR_PORT_TRACKER,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEVICE_ATTRIBUTES_IFACE = [
|
|
||||||
"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",
|
|
||||||
"poe-out",
|
|
||||||
]
|
|
||||||
|
|
||||||
DEVICE_ATTRIBUTES_HOST = [
|
DEVICE_ATTRIBUTES_HOST = [
|
||||||
"host-name",
|
"host-name",
|
||||||
"address",
|
"address",
|
||||||
|
@ -115,33 +96,20 @@ def update_items(inst, config_entry, mikrotik_controller, async_add_entities, tr
|
||||||
# Add switches
|
# Add switches
|
||||||
for sid, sid_uid, sid_name, sid_ref, sid_func in zip(
|
for sid, sid_uid, sid_name, sid_ref, sid_func in zip(
|
||||||
# Data point name
|
# Data point name
|
||||||
["interface", "host"],
|
["host"],
|
||||||
# Data point unique id
|
# Data point unique id
|
||||||
["default-name", "mac-address"],
|
["mac-address"],
|
||||||
# Entry Name
|
# Entry Name
|
||||||
["name", "host-name"],
|
["host-name"],
|
||||||
# Entry Unique id
|
# Entry Unique id
|
||||||
["port-mac-address", "mac-address"],
|
["mac-address"],
|
||||||
# Tracker function
|
# Tracker function
|
||||||
[
|
[
|
||||||
MikrotikControllerPortDeviceTracker,
|
|
||||||
MikrotikControllerHostDeviceTracker,
|
MikrotikControllerHostDeviceTracker,
|
||||||
],
|
],
|
||||||
):
|
):
|
||||||
if (
|
|
||||||
sid_func == MikrotikControllerPortDeviceTracker
|
|
||||||
and not config_entry.options.get(
|
|
||||||
CONF_SENSOR_PORT_TRACKER, DEFAULT_SENSOR_PORT_TRACKER
|
|
||||||
)
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
|
|
||||||
for uid in mikrotik_controller.data[sid]:
|
for uid in mikrotik_controller.data[sid]:
|
||||||
if (
|
if (
|
||||||
# Skip if interface is wlan
|
|
||||||
sid == "interface"
|
|
||||||
and mikrotik_controller.data[sid][uid]["type"] == "wlan"
|
|
||||||
) or (
|
|
||||||
# Skip if host tracking is disabled
|
# Skip if host tracking is disabled
|
||||||
sid == "host"
|
sid == "host"
|
||||||
and not config_entry.options.get(CONF_TRACK_HOSTS, DEFAULT_TRACK_HOSTS)
|
and not config_entry.options.get(CONF_TRACK_HOSTS, DEFAULT_TRACK_HOSTS)
|
||||||
|
@ -250,54 +218,6 @@ class MikrotikControllerDeviceTracker(ScannerEntity):
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the port state attributes."""
|
"""Return the port state attributes."""
|
||||||
attributes = self._attrs
|
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"]
|
|
||||||
|
|
||||||
@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']}"
|
|
||||||
|
|
||||||
@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
|
|
||||||
for variable in DEVICE_ATTRIBUTES_IFACE:
|
|
||||||
if variable in self._data:
|
|
||||||
attributes[format_attribute(variable)] = self._data[variable]
|
|
||||||
|
|
||||||
return attributes
|
return attributes
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue