Use voluptuous for Aruba (#3119)

pull/3132/head
Johann Kellerman 2016-09-02 06:28:46 +02:00 committed by Teagan Glenn
parent 9e38255c26
commit a571271c39
1 changed files with 16 additions and 13 deletions

View File

@ -9,9 +9,11 @@ import re
import threading import threading
from datetime import timedelta from datetime import timedelta
from homeassistant.components.device_tracker import DOMAIN import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.device_tracker import DOMAIN, PLATFORM_SCHEMA
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers import validate_config
from homeassistant.util import Throttle from homeassistant.util import Throttle
# Return cached results if last scan was less then this time ago # Return cached results if last scan was less then this time ago
@ -25,15 +27,16 @@ _DEVICES_REGEX = re.compile(
r'(?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3})\s+' + r'(?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3})\s+' +
r'(?P<mac>(([0-9a-f]{2}[:-]){5}([0-9a-f]{2})))\s+') r'(?P<mac>(([0-9a-f]{2}[:-]){5}([0-9a-f]{2})))\s+')
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string
})
# pylint: disable=unused-argument # pylint: disable=unused-argument
def get_scanner(hass, config): def get_scanner(hass, config):
"""Validate the configuration and return a Aruba scanner.""" """Validate the configuration and return a Aruba scanner."""
if not validate_config(config,
{DOMAIN: [CONF_HOST, CONF_USERNAME, CONF_PASSWORD]},
_LOGGER):
return None
scanner = ArubaDeviceScanner(config[DOMAIN]) scanner = ArubaDeviceScanner(config[DOMAIN])
return scanner if scanner.success_init else None return scanner if scanner.success_init else None
@ -90,7 +93,7 @@ class ArubaDeviceScanner(object):
def get_aruba_data(self): def get_aruba_data(self):
"""Retrieve data from Aruba Access Point and return parsed result.""" """Retrieve data from Aruba Access Point and return parsed result."""
import pexpect import pexpect
connect = "ssh {}@{}" connect = 'ssh {}@{}'
ssh = pexpect.spawn(connect.format(self.username, self.host)) ssh = pexpect.spawn(connect.format(self.username, self.host))
query = ssh.expect(['password:', pexpect.TIMEOUT, pexpect.EOF, query = ssh.expect(['password:', pexpect.TIMEOUT, pexpect.EOF,
'continue connecting (yes/no)?', 'continue connecting (yes/no)?',
@ -98,22 +101,22 @@ class ArubaDeviceScanner(object):
'Connection refused', 'Connection refused',
'Connection timed out'], timeout=120) 'Connection timed out'], timeout=120)
if query == 1: if query == 1:
_LOGGER.error("Timeout") _LOGGER.error('Timeout')
return return
elif query == 2: elif query == 2:
_LOGGER.error("Unexpected response from router") _LOGGER.error('Unexpected response from router')
return return
elif query == 3: elif query == 3:
ssh.sendline('yes') ssh.sendline('yes')
ssh.expect('password:') ssh.expect('password:')
elif query == 4: elif query == 4:
_LOGGER.error("Host key Changed") _LOGGER.error('Host key Changed')
return return
elif query == 5: elif query == 5:
_LOGGER.error("Connection refused by server") _LOGGER.error('Connection refused by server')
return return
elif query == 6: elif query == 6:
_LOGGER.error("Connection timed out") _LOGGER.error('Connection timed out')
return return
ssh.sendline(self.password) ssh.sendline(self.password)
ssh.expect('#') ssh.expect('#')