1
mirror of https://github.com/home-assistant/core synced 2024-07-27 18:58:57 +02:00
ha-core/homeassistant/components/device_tracker/aruba.py

135 lines
4.3 KiB
Python
Raw Normal View History

2015-08-31 11:36:12 +02:00
"""
2016-03-07 18:12:06 +01:00
Support for Aruba Access Points.
2015-08-31 11:36:12 +02:00
2015-10-13 20:45:29 +02:00
For more details about this platform, please refer to the documentation at
2015-11-09 13:12:18 +01:00
https://home-assistant.io/components/device_tracker.aruba/
2015-10-13 20:49:14 +02:00
"""
2015-08-31 11:36:12 +02:00
import logging
import re
import threading
2016-02-19 06:27:50 +01:00
from datetime import timedelta
2016-02-02 22:49:11 +01:00
2016-02-19 06:27:50 +01:00
from homeassistant.components.device_tracker import DOMAIN
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
2015-08-31 11:36:12 +02:00
from homeassistant.helpers import validate_config
from homeassistant.util import Throttle
# Return cached results if last scan was less then this time ago
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
2016-02-02 22:49:11 +01:00
REQUIREMENTS = ['pexpect==4.0.1']
2015-08-31 11:36:12 +02:00
_LOGGER = logging.getLogger(__name__)
_DEVICES_REGEX = re.compile(
r'(?P<name>([^\s]+))\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+')
2015-08-31 11:36:12 +02:00
# pylint: disable=unused-argument
def get_scanner(hass, config):
2016-03-07 21:18:53 +01:00
"""Validate the configuration and return a Aruba scanner."""
2015-08-31 11:36:12 +02:00
if not validate_config(config,
{DOMAIN: [CONF_HOST, CONF_USERNAME, CONF_PASSWORD]},
_LOGGER):
return None
scanner = ArubaDeviceScanner(config[DOMAIN])
return scanner if scanner.success_init else None
class ArubaDeviceScanner(object):
2016-03-07 18:12:06 +01:00
"""This class queries a Aruba Access Point for connected devices."""
2016-03-07 21:18:53 +01:00
2015-08-31 11:36:12 +02:00
def __init__(self, config):
2016-03-07 21:18:53 +01:00
"""Initialize the scanner."""
2015-08-31 11:36:12 +02:00
self.host = config[CONF_HOST]
self.username = config[CONF_USERNAME]
self.password = config[CONF_PASSWORD]
self.lock = threading.Lock()
self.last_results = {}
2016-03-07 21:18:53 +01:00
# Test the router is accessible.
2015-08-31 11:36:12 +02:00
data = self.get_aruba_data()
self.success_init = data is not None
def scan_devices(self):
2016-03-07 21:18:53 +01:00
"""Scan for new devices and return a list with found device IDs."""
2015-08-31 11:36:12 +02:00
self._update_info()
return [client['mac'] for client in self.last_results]
def get_device_name(self, device):
2016-03-07 21:18:53 +01:00
"""Return the name of the given device or None if we don't know."""
2015-08-31 11:36:12 +02:00
if not self.last_results:
return None
for client in self.last_results:
if client['mac'] == device:
return client['name']
return None
@Throttle(MIN_TIME_BETWEEN_SCANS)
def _update_info(self):
2016-03-07 21:18:53 +01:00
"""Ensure the information from the Aruba Access Point is up to date.
Return boolean if scanning successful.
"""
2015-08-31 11:36:12 +02:00
if not self.success_init:
return False
with self.lock:
data = self.get_aruba_data()
if not data:
return False
self.last_results = data.values()
return True
def get_aruba_data(self):
2016-03-07 18:12:06 +01:00
"""Retrieve data from Aruba Access Point and return parsed result."""
2016-02-02 22:49:11 +01:00
import pexpect
connect = "ssh {}@{}"
2016-02-02 23:40:04 +01:00
ssh = pexpect.spawn(connect.format(self.username, self.host))
2016-02-03 00:54:32 +01:00
query = ssh.expect(['password:', pexpect.TIMEOUT, pexpect.EOF,
2016-02-03 01:03:50 +01:00
'continue connecting (yes/no)?',
'Host key verification failed.',
'Connection refused',
'Connection timed out'], timeout=120)
2016-02-02 23:40:04 +01:00
if query == 1:
2016-02-02 22:49:11 +01:00
_LOGGER.error("Timeout")
2015-08-31 11:36:12 +02:00
return
2016-02-02 22:49:11 +01:00
elif query == 2:
_LOGGER.error("Unexpected response from router")
2015-08-31 11:36:12 +02:00
return
2016-02-02 22:49:11 +01:00
elif query == 3:
ssh.sendline('yes')
2016-02-02 23:40:04 +01:00
ssh.expect('password:')
2016-02-02 22:49:11 +01:00
elif query == 4:
_LOGGER.error("Host key Changed")
return
elif query == 5:
2016-02-02 23:40:04 +01:00
_LOGGER.error("Connection refused by server")
2016-02-02 22:49:11 +01:00
return
elif query == 6:
2016-02-02 23:40:04 +01:00
_LOGGER.error("Connection timed out")
2016-02-02 22:49:11 +01:00
return
2016-02-02 23:40:04 +01:00
ssh.sendline(self.password)
ssh.expect('#')
ssh.sendline('show clients')
ssh.expect('#')
2016-02-02 22:49:11 +01:00
devices_result = ssh.before.split(b'\r\n')
2016-02-02 23:40:04 +01:00
ssh.sendline('exit')
2015-08-31 11:36:12 +02:00
devices = {}
for device in devices_result:
match = _DEVICES_REGEX.search(device.decode('utf-8'))
if match:
devices[match.group('ip')] = {
'ip': match.group('ip'),
'mac': match.group('mac').upper(),
'name': match.group('name')
2016-02-02 23:40:04 +01:00
}
2015-08-31 11:36:12 +02:00
return devices