ha-core/homeassistant/components/aruba/device_tracker.py

136 lines
3.9 KiB
Python
Raw Normal View History

"""Support for Aruba Access Points."""
2015-08-31 11:36:12 +02:00
import logging
import re
2016-02-02 22:49:11 +01:00
2016-09-02 06:28:46 +02:00
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.device_tracker import (
2019-07-31 21:25:30 +02:00
DOMAIN,
PLATFORM_SCHEMA,
DeviceScanner,
)
2016-02-19 06:27:50 +01:00
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
2015-08-31 11:36:12 +02:00
_LOGGER = logging.getLogger(__name__)
2015-08-31 11:36:12 +02:00
_DEVICES_REGEX = re.compile(
2019-07-31 21:25:30 +02:00
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
2019-07-31 21:25:30 +02:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string,
}
)
2016-09-02 06:28:46 +02:00
2015-08-31 11:36:12 +02:00
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
scanner = ArubaDeviceScanner(config[DOMAIN])
return scanner if scanner.success_init else None
class ArubaDeviceScanner(DeviceScanner):
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.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()
2019-07-31 21:25:30 +02:00
return [client["mac"] for client in self.last_results]
2015-08-31 11:36:12 +02:00
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:
2019-07-31 21:25:30 +02:00
if client["mac"] == device:
return client["name"]
2015-08-31 11:36:12 +02:00
return None
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
data = self.get_aruba_data()
if not data:
return False
2015-08-31 11:36:12 +02:00
self.last_results = data.values()
return True
2015-08-31 11:36:12 +02:00
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
2019-07-31 21:25:30 +02:00
connect = "ssh {}@{}"
2016-02-02 23:40:04 +01:00
ssh = pexpect.spawn(connect.format(self.username, self.host))
2019-07-31 21:25:30 +02:00
query = ssh.expect(
[
"password:",
pexpect.TIMEOUT,
pexpect.EOF,
"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:
_LOGGER.error("Timeout")
2015-08-31 11:36:12 +02:00
return
if query == 2:
_LOGGER.error("Unexpected response from router")
2015-08-31 11:36:12 +02:00
return
if query == 3:
2019-07-31 21:25:30 +02:00
ssh.sendline("yes")
ssh.expect("password:")
2016-02-02 22:49:11 +01:00
elif query == 4:
_LOGGER.error("Host key changed")
2016-02-02 22:49:11 +01:00
return
elif query == 5:
_LOGGER.error("Connection refused by server")
2016-02-02 22:49:11 +01:00
return
elif query == 6:
_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)
2019-07-31 21:25:30 +02:00
ssh.expect("#")
ssh.sendline("show clients")
ssh.expect("#")
devices_result = ssh.before.split(b"\r\n")
ssh.sendline("exit")
2016-02-02 23:40:04 +01:00
2015-08-31 11:36:12 +02:00
devices = {}
for device in devices_result:
2019-07-31 21:25:30 +02:00
match = _DEVICES_REGEX.search(device.decode("utf-8"))
2015-08-31 11:36:12 +02:00
if match:
2019-07-31 21:25:30 +02:00
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