diff --git a/homeassistant/components/device_tracker/mikrotik.py b/homeassistant/components/device_tracker/mikrotik.py index d6b965cbadf..5b69c13afa6 100644 --- a/homeassistant/components/device_tracker/mikrotik.py +++ b/homeassistant/components/device_tracker/mikrotik.py @@ -6,26 +6,30 @@ https://home-assistant.io/components/device_tracker.mikrotik/ """ import logging +import ssl + import voluptuous as vol import homeassistant.helpers.config_validation as cv from homeassistant.components.device_tracker import ( DOMAIN, PLATFORM_SCHEMA, DeviceScanner) from homeassistant.const import ( - CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_PORT, CONF_METHOD) + CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_PORT, CONF_SSL, CONF_METHOD) REQUIREMENTS = ['librouteros==2.1.1'] _LOGGER = logging.getLogger(__name__) MTK_DEFAULT_API_PORT = '8728' +MTK_DEFAULT_API_SSL_PORT = '8729' 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_METHOD): cv.string, - vol.Optional(CONF_PORT, default=MTK_DEFAULT_API_PORT): cv.port, + vol.Optional(CONF_PORT): cv.port, + vol.Optional(CONF_SSL, default=False): cv.boolean }) @@ -43,7 +47,14 @@ class MikrotikScanner(DeviceScanner): self.last_results = {} self.host = config[CONF_HOST] - self.port = config[CONF_PORT] + self.ssl = config[CONF_SSL] + try: + self.port = config[CONF_PORT] + except KeyError: + if self.ssl: + self.port = MTK_DEFAULT_API_SSL_PORT + else: + self.port = MTK_DEFAULT_API_PORT self.username = config[CONF_USERNAME] self.password = config[CONF_PASSWORD] self.method = config.get(CONF_METHOD) @@ -64,9 +75,21 @@ class MikrotikScanner(DeviceScanner): """Connect to Mikrotik method.""" import librouteros try: + kwargs = { + 'port': self.port, + 'encoding': 'utf-8' + } + if self.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 self.client = librouteros.connect( - self.host, self.username, self.password, port=int(self.port), - encoding='utf-8') + self.host, + self.username, + self.password, + **kwargs + ) try: routerboard_info = self.client(