Fix Mikrotik ARP ping (#25965)

* Reuse ssl_wraper

* Fix arp_ping

* Restore debug log

* Fix attributes
This commit is contained in:
Robert Dunmire III 2019-08-18 11:14:54 -04:00 committed by Martin Hjelmare
parent 10d63e46d7
commit d4981a1143
3 changed files with 14 additions and 9 deletions

View File

@ -18,6 +18,7 @@ from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.discovery import load_platform
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER
from .const import (
NAME,
DOMAIN,
HOSTS,
MTK_LOGIN_PLAIN,
@ -121,6 +122,7 @@ class MikrotikClient:
self._password = password
self._login_method = login_method
self._encoding = encoding
self._ssl_wrapper = None
self.hostname = None
self._client = None
self._connected = False
@ -137,10 +139,12 @@ class MikrotikClient:
}
if self._use_ssl:
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
kwargs["ssl_wrapper"] = ssl_context.wrap_socket
if self._ssl_wrapper is None:
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
self._ssl_wrapper = ssl_context.wrap_socket
kwargs["ssl_wrapper"] = self._ssl_wrapper
try:
self._client = librouteros.connect(
@ -163,7 +167,7 @@ class MikrotikClient:
def get_hostname(self):
"""Return device host name."""
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):
"""Return connected boolean."""

View File

@ -12,6 +12,7 @@ CONF_LOGIN_METHOD = "login_method"
CONF_ENCODING = "encoding"
DEFAULT_ENCODING = "utf-8"
NAME = "name"
INFO = "info"
IDENTITY = "identity"
ARP = "arp"

View File

@ -132,8 +132,9 @@ class MikrotikScanner(DeviceScanner):
if self.arp_ping and self.devices_arp:
if mac not in self.devices_arp:
continue
ip_address = self.devices_arp[mac]["address"]
interface = self.devices_arp[mac]["interface"]
if not self.do_arp_ping(mac, interface):
if not self.do_arp_ping(ip_address, interface):
continue
attrs = {}
@ -148,20 +149,19 @@ class MikrotikScanner(DeviceScanner):
for attr in ATTR_DEVICE_TRACKER:
if attr in device and device[attr] is not None:
attrs[slugify(attr)] = device[attr]
attrs["scanner_type"] = self.method
attrs["scanner_host"] = self.host
attrs["scanner_hostname"] = self.hostname
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."""
params = {
"arp-ping": "yes",
"interval": "100ms",
"count": 3,
"interface": interface,
"address": mac,
"address": ip_address,
}
cmd = "/ping"
data = self.api.command(cmd, params)