Fix BT Smarthub device tracker (#44813)

This commit is contained in:
Henco Appel 2021-02-08 14:24:18 +00:00 committed by GitHub
parent 2811e39c5c
commit b1ffe429cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
"""Support for BT Smart Hub (Sometimes referred to as BT Home Hub 6).""" """Support for BT Smart Hub (Sometimes referred to as BT Home Hub 6)."""
from collections import namedtuple
import logging import logging
from btsmarthub_devicelist import BTSmartHub from btsmarthub_devicelist import BTSmartHub
@ -31,19 +32,30 @@ def get_scanner(hass, config):
smarthub_client = BTSmartHub( smarthub_client = BTSmartHub(
router_ip=info[CONF_HOST], smarthub_model=info.get(CONF_SMARTHUB_MODEL) router_ip=info[CONF_HOST], smarthub_model=info.get(CONF_SMARTHUB_MODEL)
) )
scanner = BTSmartHubScanner(smarthub_client) scanner = BTSmartHubScanner(smarthub_client)
return scanner if scanner.success_init else None return scanner if scanner.success_init else None
def _create_device(data):
"""Create new device from the dict."""
ip_address = data.get("IPAddress")
mac = data.get("PhysAddress")
host = data.get("UserHostName")
status = data.get("Active")
name = data.get("name")
return _Device(ip_address, mac, host, status, name)
_Device = namedtuple("_Device", ["ip_address", "mac", "host", "status", "name"])
class BTSmartHubScanner(DeviceScanner): class BTSmartHubScanner(DeviceScanner):
"""This class queries a BT Smart Hub.""" """This class queries a BT Smart Hub."""
def __init__(self, smarthub_client): def __init__(self, smarthub_client):
"""Initialise the scanner.""" """Initialise the scanner."""
self.smarthub = smarthub_client self.smarthub = smarthub_client
self.last_results = {} self.last_results = []
self.success_init = False self.success_init = False
# Test the router is accessible # Test the router is accessible
@ -56,15 +68,15 @@ class BTSmartHubScanner(DeviceScanner):
def scan_devices(self): def scan_devices(self):
"""Scan for new devices and return a list with found device IDs.""" """Scan for new devices and return a list with found device IDs."""
self._update_info() self._update_info()
return [client["mac"] for client in self.last_results] return [device.mac for device in self.last_results]
def get_device_name(self, device): def get_device_name(self, device):
"""Return the name of the given device or None if we don't know.""" """Return the name of the given device or None if we don't know."""
if not self.last_results: if not self.last_results:
return None return None
for client in self.last_results: for result_device in self.last_results:
if client["mac"] == device: if result_device.mac == device:
return client["host"] return result_device.name or result_device.host
return None return None
def _update_info(self): def _update_info(self):
@ -77,26 +89,10 @@ class BTSmartHubScanner(DeviceScanner):
if not data: if not data:
_LOGGER.warning("Error scanning devices") _LOGGER.warning("Error scanning devices")
return return
self.last_results = data
clients = list(data.values())
self.last_results = clients
def get_bt_smarthub_data(self): def get_bt_smarthub_data(self):
"""Retrieve data from BT Smart Hub and return parsed result.""" """Retrieve data from BT Smart Hub and return parsed result."""
# Request data from bt smarthub into a list of dicts. # Request data from bt smarthub into a list of dicts.
data = self.smarthub.get_devicelist(only_active_devices=True) data = self.smarthub.get_devicelist(only_active_devices=True)
return [_create_device(d) for d in data if d.get("PhysAddress")]
# Renaming keys from parsed result.
devices = {}
for device in data:
try:
devices[device["UserHostName"]] = {
"ip": device["IPAddress"],
"mac": device["PhysAddress"],
"host": device["UserHostName"],
"status": device["Active"],
}
except KeyError:
pass
return devices