Skip to content

Commit

Permalink
Merge pull request #316 from L2jLiga/master
Browse files Browse the repository at this point in the history
feat: netwatch binary sensor
  • Loading branch information
tomaae authored Oct 23, 2023
2 parents 2cdd645 + 13e765c commit 593aca5
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ Track availability of all network devices. All devices visible to Mikrotik devic

![Host tracker](https://raw.githubusercontent.com/tomaae/homeassistant-mikrotik_router/master/docs/assets/images/ui/host_tracker.png)

## Netwatch Tracking
Track netwatch status.

![Netwatch](https://raw.githubusercontent.com/tomaae/homeassistant-mikrotik_router/master/docs/assets/images/ui/netwatch_tracker.png)

## Scripts
Execute Mikrotik Router scripts.
You can execute scripts by automatically created switches or using services.
Expand Down
3 changes: 3 additions & 0 deletions custom_components/mikrotik_router/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
DEVICE_ATTRIBUTES_IFACE_ETHER,
DEVICE_ATTRIBUTES_IFACE_SFP,
DEVICE_ATTRIBUTES_IFACE_WIRELESS,
DEVICE_ATTRIBUTES_NETWATCH,
)
from .const import (
CONF_SENSOR_PPP,
DEFAULT_SENSOR_PPP,
CONF_SENSOR_PORT_TRACKER,
DEFAULT_SENSOR_PORT_TRACKER,
CONF_SENSOR_NETWATCH_TRACKER,
DEFAULT_SENSOR_NETWATCH_TRACKER,
)
from .entity import MikrotikEntity, async_add_entities
from .helper import format_attribute
Expand Down
27 changes: 27 additions & 0 deletions custom_components/mikrotik_router/binary_sensor_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@
"hid-self-test",
]

DEVICE_ATTRIBUTES_NETWATCH = [
"host",
"type",
"interval",
"port",
"http-codes",
"status",
"comment",
]

@dataclass
class MikrotikBinarySensorEntityDescription(BinarySensorEntityDescription):
Expand Down Expand Up @@ -177,6 +186,24 @@ class MikrotikBinarySensorEntityDescription(BinarySensorEntityDescription):
data_attributes_list=DEVICE_ATTRIBUTES_IFACE,
func="MikrotikPortBinarySensor",
),
MikrotikBinarySensorEntityDescription(
key="netwatch",
name="Netwatch",
icon_enabled="mdi:lan-connect",
icon_disabled="mdi:lan-pending",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
ha_group="Netwatch",
ha_connection=DOMAIN,
ha_connection_value="Netwatch",
data_path="netwatch",
data_attribute="status",
data_name="host",
data_name_comment=True,
data_uid="host",
data_reference="host",
data_attributes_list=DEVICE_ATTRIBUTES_NETWATCH,
func="MikrotikBinarySensor",
),
)

SENSOR_SERVICES = {}
8 changes: 8 additions & 0 deletions custom_components/mikrotik_router/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
DEFAULT_PORT,
DEFAULT_DEVICE_NAME,
DEFAULT_SSL,
DEFAULT_SENSOR_NETWATCH_TRACKER,
CONF_SENSOR_NETWATCH_TRACKER,
)
from .mikrotikapi import MikrotikAPI

Expand Down Expand Up @@ -280,6 +282,12 @@ async def async_step_sensor_select(self, user_input=None):
CONF_SENSOR_KIDCONTROL, DEFAULT_SENSOR_KIDCONTROL
),
): bool,
vol.Optional(
CONF_SENSOR_NETWATCH_TRACKER,
default=self.config_entry.options.get(
CONF_SENSOR_NETWATCH_TRACKER, DEFAULT_SENSOR_NETWATCH_TRACKER
),
): bool,
vol.Optional(
CONF_SENSOR_PPP,
default=self.config_entry.options.get(
Expand Down
2 changes: 2 additions & 0 deletions custom_components/mikrotik_router/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
DEFAULT_SENSOR_SCRIPTS = False
CONF_SENSOR_ENVIRONMENT = "sensor_environment"
DEFAULT_SENSOR_ENVIRONMENT = False
CONF_SENSOR_NETWATCH_TRACKER = "sensor_netwatch_tracker"
DEFAULT_SENSOR_NETWATCH_TRACKER = False

TO_REDACT = {
"ip-address",
Expand Down
40 changes: 39 additions & 1 deletion custom_components/mikrotik_router/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
CONF_SENSOR_SCRIPTS,
DEFAULT_SENSOR_SCRIPTS,
CONF_SENSOR_ENVIRONMENT,
DEFAULT_SENSOR_ENVIRONMENT,
DEFAULT_SENSOR_ENVIRONMENT, CONF_SENSOR_NETWATCH_TRACKER, DEFAULT_SENSOR_NETWATCH_TRACKER,
)
from .exceptions import ApiEntryNotFound
from .apiparser import parse_api
Expand Down Expand Up @@ -258,6 +258,7 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry):
"environment": {},
"ups": {},
"gps": {},
"netwatch": {}
}

self.notified_flags = []
Expand Down Expand Up @@ -386,6 +387,14 @@ def option_sensor_kidcontrol(self):
CONF_SENSOR_KIDCONTROL, DEFAULT_SENSOR_KIDCONTROL
)

# ---------------------------
# option_sensor_netwatch
# ---------------------------
@property
def option_sensor_netwatch(self):
"""Config entry option to not track ARP."""
return self.config_entry.options.get(CONF_SENSOR_NETWATCH_TRACKER, DEFAULT_SENSOR_NETWATCH_TRACKER)

# ---------------------------
# option_sensor_ppp
# ---------------------------
Expand Down Expand Up @@ -627,6 +636,9 @@ async def _async_update_data(self):
if self.api.connected() and self.option_sensor_filter:
await self.hass.async_add_executor_job(self.get_filter)

if self.api.connected() and self.option_sensor_netwatch:
await self.hass.async_add_executor_job(self.get_netwatch)

if self.api.connected() and self.support_ppp and self.option_sensor_ppp:
await self.hass.async_add_executor_job(self.get_ppp)

Expand Down Expand Up @@ -1310,6 +1322,32 @@ def get_ppp(self) -> None:
self.ds["ppp_secret"][uid]["address"] = "not connected"
self.ds["ppp_secret"][uid]["encoding"] = "not connected"

# ---------------------------
# get_netwatch
# ---------------------------
def get_netwatch(self) -> None:
"""Get netwatch data from Mikrotik"""
self.ds["netwatch"] = parse_api(
data=self.ds["netwatch"],
source=self.api.query("/tool/netwatch"),
key="host",
vals=[
{"name": "host"},
{"name": "type"},
{"name": "interval"},
{"name": "port"},
{"name": "http-codes"},
{"name": "status", "type": "bool", "default": "unknown"},
{"name": "comment"},
{
"name": "enabled",
"source": "disabled",
"type": "bool",
"reverse": True,
},
],
)

# ---------------------------
# get_system_routerboard
# ---------------------------
Expand Down
10 changes: 10 additions & 0 deletions custom_components/mikrotik_router/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
DEFAULT_TRACK_HOSTS,
CONF_SENSOR_PORT_TRACKER,
DEFAULT_SENSOR_PORT_TRACKER,
CONF_SENSOR_NETWATCH_TRACKER,
DEFAULT_SENSOR_NETWATCH_TRACKER,
)
from .coordinator import MikrotikCoordinator, MikrotikTrackerCoordinator
from .helper import format_attribute
Expand Down Expand Up @@ -70,6 +72,14 @@ def _skip_sensor(config_entry, entity_description, data, uid) -> bool:
):
return True

if (
entity_description.data_path == "netwatch"
and not config_entry.options.get(
CONF_SENSOR_NETWATCH_TRACKER, DEFAULT_SENSOR_NETWATCH_TRACKER
)
):
return True

# Device Tracker
if (
# Skip if host tracking is disabled
Expand Down
1 change: 1 addition & 0 deletions custom_components/mikrotik_router/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"data": {
"track_network_hosts": "Track network devices",
"sensor_port_tracker": "Port tracker sensors",
"sensor_netwatch_tracker": "Netwatch tracker sensors",
"sensor_port_traffic": "Port traffic sensors",
"sensor_client_traffic": "Client traffic sensors",
"sensor_client_captive": "Captive portal data",
Expand Down
Binary file added docs/assets/images/ui/netwatch_tracker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 593aca5

Please sign in to comment.