mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix Mikrotik ARP ping (#25965)
* Reuse ssl_wraper * Fix arp_ping * Restore debug log * Fix attributes
This commit is contained in:
parent
10d63e46d7
commit
d4981a1143
@ -18,6 +18,7 @@ from homeassistant.helpers import config_validation as cv
|
|||||||
from homeassistant.helpers.discovery import load_platform
|
from homeassistant.helpers.discovery import load_platform
|
||||||
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER
|
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER
|
||||||
from .const import (
|
from .const import (
|
||||||
|
NAME,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
HOSTS,
|
HOSTS,
|
||||||
MTK_LOGIN_PLAIN,
|
MTK_LOGIN_PLAIN,
|
||||||
@ -121,6 +122,7 @@ class MikrotikClient:
|
|||||||
self._password = password
|
self._password = password
|
||||||
self._login_method = login_method
|
self._login_method = login_method
|
||||||
self._encoding = encoding
|
self._encoding = encoding
|
||||||
|
self._ssl_wrapper = None
|
||||||
self.hostname = None
|
self.hostname = None
|
||||||
self._client = None
|
self._client = None
|
||||||
self._connected = False
|
self._connected = False
|
||||||
@ -137,10 +139,12 @@ class MikrotikClient:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if self._use_ssl:
|
if self._use_ssl:
|
||||||
ssl_context = ssl.create_default_context()
|
if self._ssl_wrapper is None:
|
||||||
ssl_context.check_hostname = False
|
ssl_context = ssl.create_default_context()
|
||||||
ssl_context.verify_mode = ssl.CERT_NONE
|
ssl_context.check_hostname = False
|
||||||
kwargs["ssl_wrapper"] = ssl_context.wrap_socket
|
ssl_context.verify_mode = ssl.CERT_NONE
|
||||||
|
self._ssl_wrapper = ssl_context.wrap_socket
|
||||||
|
kwargs["ssl_wrapper"] = self._ssl_wrapper
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._client = librouteros.connect(
|
self._client = librouteros.connect(
|
||||||
@ -163,7 +167,7 @@ class MikrotikClient:
|
|||||||
def get_hostname(self):
|
def get_hostname(self):
|
||||||
"""Return device host name."""
|
"""Return device host name."""
|
||||||
data = self.command(MIKROTIK_SERVICES[IDENTITY])
|
data = self.command(MIKROTIK_SERVICES[IDENTITY])
|
||||||
return data[0]["name"] if data else None
|
return data[0][NAME] if data else None
|
||||||
|
|
||||||
def connected(self):
|
def connected(self):
|
||||||
"""Return connected boolean."""
|
"""Return connected boolean."""
|
||||||
|
@ -12,6 +12,7 @@ CONF_LOGIN_METHOD = "login_method"
|
|||||||
CONF_ENCODING = "encoding"
|
CONF_ENCODING = "encoding"
|
||||||
DEFAULT_ENCODING = "utf-8"
|
DEFAULT_ENCODING = "utf-8"
|
||||||
|
|
||||||
|
NAME = "name"
|
||||||
INFO = "info"
|
INFO = "info"
|
||||||
IDENTITY = "identity"
|
IDENTITY = "identity"
|
||||||
ARP = "arp"
|
ARP = "arp"
|
||||||
|
@ -132,8 +132,9 @@ class MikrotikScanner(DeviceScanner):
|
|||||||
if self.arp_ping and self.devices_arp:
|
if self.arp_ping and self.devices_arp:
|
||||||
if mac not in self.devices_arp:
|
if mac not in self.devices_arp:
|
||||||
continue
|
continue
|
||||||
|
ip_address = self.devices_arp[mac]["address"]
|
||||||
interface = self.devices_arp[mac]["interface"]
|
interface = self.devices_arp[mac]["interface"]
|
||||||
if not self.do_arp_ping(mac, interface):
|
if not self.do_arp_ping(ip_address, interface):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
attrs = {}
|
attrs = {}
|
||||||
@ -148,20 +149,19 @@ class MikrotikScanner(DeviceScanner):
|
|||||||
for attr in ATTR_DEVICE_TRACKER:
|
for attr in ATTR_DEVICE_TRACKER:
|
||||||
if attr in device and device[attr] is not None:
|
if attr in device and device[attr] is not None:
|
||||||
attrs[slugify(attr)] = device[attr]
|
attrs[slugify(attr)] = device[attr]
|
||||||
|
|
||||||
attrs["scanner_type"] = self.method
|
attrs["scanner_type"] = self.method
|
||||||
attrs["scanner_host"] = self.host
|
attrs["scanner_host"] = self.host
|
||||||
attrs["scanner_hostname"] = self.hostname
|
attrs["scanner_hostname"] = self.hostname
|
||||||
self.device_tracker[mac] = attrs
|
self.device_tracker[mac] = attrs
|
||||||
|
|
||||||
def do_arp_ping(self, mac, interface):
|
def do_arp_ping(self, ip_address, interface):
|
||||||
"""Attempt to arp ping MAC address via interface."""
|
"""Attempt to arp ping MAC address via interface."""
|
||||||
params = {
|
params = {
|
||||||
"arp-ping": "yes",
|
"arp-ping": "yes",
|
||||||
"interval": "100ms",
|
"interval": "100ms",
|
||||||
"count": 3,
|
"count": 3,
|
||||||
"interface": interface,
|
"interface": interface,
|
||||||
"address": mac,
|
"address": ip_address,
|
||||||
}
|
}
|
||||||
cmd = "/ping"
|
cmd = "/ping"
|
||||||
data = self.api.command(cmd, params)
|
data = self.api.command(cmd, params)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user