Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 41 additions & 55 deletions homeassistant/components/device_tracker/mikrotik.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
from homeassistant.components.device_tracker import (
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_PORT)
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_PORT, CONF_METHOD)

REQUIREMENTS = ['librouteros==2.1.1']

MTK_DEFAULT_API_PORT = '8728'

_LOGGER = logging.getLogger(__name__)

MTK_DEFAULT_API_PORT = '8728'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_PORT, default=MTK_DEFAULT_API_PORT): cv.port
vol.Optional(CONF_METHOD): cv.string,
vol.Optional(CONF_PORT, default=MTK_DEFAULT_API_PORT): cv.port,
})


Expand All @@ -45,6 +46,7 @@ def __init__(self, config):
self.port = config[CONF_PORT]
self.username = config[CONF_USERNAME]
self.password = config[CONF_PASSWORD]
self.method = config[CONF_METHOD]

self.connected = False
self.success_init = False
Expand All @@ -53,28 +55,18 @@ def __init__(self, config):
self.success_init = self.connect_to_device()

if self.success_init:
_LOGGER.info(
"Start polling Mikrotik (%s) router...",
self.host
)
_LOGGER.info("Start polling Mikrotik (%s) router...", self.host)
self._update_info()
else:
_LOGGER.error(
"Connection to Mikrotik (%s) failed",
self.host
)
_LOGGER.error("Connection to Mikrotik (%s) failed", self.host)

def connect_to_device(self):
"""Connect to Mikrotik method."""
import librouteros
try:
self.client = librouteros.connect(
self.host,
self.username,
self.password,
port=int(self.port),
encoding='utf-8'
)
self.host, self.username, self.password, port=int(self.port),
encoding='utf-8')

try:
routerboard_info = self.client(
Expand All @@ -86,44 +78,43 @@ def connect_to_device(self):
raise

if routerboard_info:
_LOGGER.info("Connected to Mikrotik %s with IP %s",
routerboard_info[0].get('model', 'Router'),
self.host)
_LOGGER.info(
"Connected to Mikrotik %s with IP %s",
routerboard_info[0].get('model', 'Router'), self.host)

self.connected = True

try:
self.capsman_exist = self.client(
cmd='/caps-man/interface/getall'
)
cmd='/caps-man/interface/getall')
except (librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
librouteros.exceptions.ConnectionError):
self.capsman_exist = False

if not self.capsman_exist:
_LOGGER.info(
'Mikrotik %s: Not a CAPSman controller. Trying '
'local interfaces ',
self.host
)
"Mikrotik %s: Not a CAPSman controller. Trying "
"local interfaces", self.host)

try:
self.wireless_exist = self.client(
cmd='/interface/wireless/getall'
)
cmd='/interface/wireless/getall')
except (librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
librouteros.exceptions.ConnectionError):
self.wireless_exist = False

if not self.wireless_exist:
if not self.wireless_exist or self.method == 'ip':
_LOGGER.info(
"Mikrotik %s: Wireless adapters not found. Try to "
"use DHCP lease table as presence tracker source. "
"Please decrease lease time as much as possible",
self.host)
if self.method:
_LOGGER.info(
'Mikrotik %s: Wireless adapters not found. Try to '
'use DHCP lease table as presence tracker source. '
'Please decrease lease time as much as possible.',
self.host
)
"Mikrotik %s: Manually selected polling method %s",
self.host, self.method)

except (librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
Expand All @@ -143,50 +134,45 @@ def get_device_name(self, device):

def _update_info(self):
"""Retrieve latest information from the Mikrotik box."""
if self.capsman_exist:
devices_tracker = 'capsman'
elif self.wireless_exist:
devices_tracker = 'wireless'
if self.method:
devices_tracker = self.method
else:
devices_tracker = 'ip'
if self.capsman_exist:
devices_tracker = 'capsman'
elif self.wireless_exist:
devices_tracker = 'wireless'
else:
devices_tracker = 'ip'

_LOGGER.info(
"Loading %s devices from Mikrotik (%s) ...",
devices_tracker,
self.host
)
devices_tracker, self.host)

device_names = self.client(cmd='/ip/dhcp-server/lease/getall')
if devices_tracker == 'capsman':
devices = self.client(
cmd='/caps-man/registration-table/getall'
)
cmd='/caps-man/registration-table/getall')
elif devices_tracker == 'wireless':
devices = self.client(
cmd='/interface/wireless/registration-table/getall'
)
cmd='/interface/wireless/registration-table/getall')
else:
devices = device_names

if device_names is None and devices is None:
return False

mac_names = {device.get('mac-address'): device.get('host-name')
for device in device_names
if device.get('mac-address')}
for device in device_names if device.get('mac-address')}

if self.wireless_exist or self.capsman_exist:
if devices_tracker in ('wireless', 'capsman'):
self.last_results = {
device.get('mac-address'):
mac_names.get(device.get('mac-address'))
for device in devices
}
for device in devices}
else:
self.last_results = {
device.get('mac-address'):
mac_names.get(device.get('mac-address'))
for device in device_names
if device.get('active-address')
}
for device in device_names if device.get('active-address')}

return True