From d0279653048703b6b8ce8e7eba3172bd7480ef33 Mon Sep 17 00:00:00 2001 From: Finbarr Brady Date: Sat, 23 Feb 2019 15:28:41 +0000 Subject: [PATCH] Update luci device tracker (#21321) * * bump pip module version. * moved named tuple into the module. * pass SSL bool into the object init. * support get_extra_attributes * Update homeassistant/components/device_tracker/luci.py --- .../components/device_tracker/luci.py | 34 +++++++++++-------- requirements_all.txt | 2 +- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/device_tracker/luci.py b/homeassistant/components/device_tracker/luci.py index 27fbf7c9f44..f60e8edd8c4 100644 --- a/homeassistant/components/device_tracker/luci.py +++ b/homeassistant/components/device_tracker/luci.py @@ -4,7 +4,6 @@ Support for OpenWRT (luci) routers. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/device_tracker.luci/ """ -from collections import namedtuple import logging import voluptuous as vol @@ -14,7 +13,7 @@ from homeassistant.components.device_tracker import ( from homeassistant.const import ( CONF_HOST, CONF_USERNAME, CONF_PASSWORD, CONF_SSL) -REQUIREMENTS = ['openwrt-luci-rpc==0.3.0'] +REQUIREMENTS = ['openwrt-luci-rpc==1.0.5'] _LOGGER = logging.getLogger(__name__) @@ -35,23 +34,17 @@ def get_scanner(hass, config): return scanner if scanner.success_init else None -Device = namedtuple('Device', ['mac', 'name']) - - class LuciDeviceScanner(DeviceScanner): """This class scans for devices connected to an OpenWrt router.""" def __init__(self, config): """Initialize the scanner.""" - host = config[CONF_HOST] - protocol = 'http' if not config[CONF_SSL] else 'https' - host_url = '{}://{}'.format(protocol, host) - from openwrt_luci_rpc import OpenWrtRpc - self.router = OpenWrtRpc(host_url, + self.router = OpenWrtRpc(config[CONF_HOST], config[CONF_USERNAME], - config[CONF_PASSWORD]) + config[CONF_PASSWORD], + config[CONF_SSL]) self.last_results = {} self.success_init = self.router.is_logged_in() @@ -65,10 +58,24 @@ class LuciDeviceScanner(DeviceScanner): def get_device_name(self, device): """Return the name of the given device or None if we don't know.""" name = next(( - result.name for result in self.last_results + result.hostname for result in self.last_results if result.mac == device), None) return name + def get_extra_attributes(self, device): + """ + Get extra attributes of a device. + + Some known extra attributes that may be returned in the device tuple + include Mac Address (mac), Network Device (dev), Ip Address + (ip), reachable status (reachable), Associated router + (host), Hostname if known (hostname) among others. + """ + device = next(( + result for result in self.last_results + if result.mac == device), None) + return device._asdict() + def _update_info(self): """Check the Luci router for devices.""" result = self.router.get_all_connected_devices( @@ -79,7 +86,6 @@ class LuciDeviceScanner(DeviceScanner): last_results = [] for device in result: - last_results.append( - Device(device['macaddress'], device['hostname'])) + last_results.append(device) self.last_results = last_results diff --git a/requirements_all.txt b/requirements_all.txt index 2dc3d8c5be8..4142421b78d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -776,7 +776,7 @@ openhomedevice==0.4.2 opensensemap-api==0.1.4 # homeassistant.components.device_tracker.luci -openwrt-luci-rpc==0.3.0 +openwrt-luci-rpc==1.0.5 # homeassistant.components.switch.orvibo orvibo==1.1.1