From 6fc517fdbe1e4de847b61b9a79a79ca552478669 Mon Sep 17 00:00:00 2001 From: Leroy Shirto Date: Mon, 20 Apr 2020 19:45:08 +0100 Subject: [PATCH] Update bt_smarthub component making it compatible with smarthub 2 (#31292) * 0.2.0 of the btsmarthub_devicelist package makes it compatable with BT's home hub 2. The API has changed in the new version so this change also makes the component code compatible with the changes to the library. * Update homeassistant/components/bt_smarthub/device_tracker.py Co-Authored-By: Franck Nijhof * Update homeassistant/components/bt_smarthub/device_tracker.py Co-Authored-By: Franck Nijhof * Remove dep on config in BTSmartHubScanner This should make BTSmartHubScanner easier to test as you can pass in a mock smarthub_client * Black format bt_smarthub Co-authored-by: Franck Nijhof --- .../components/bt_smarthub/device_tracker.py | 27 ++++++++++++------- .../components/bt_smarthub/manifest.json | 2 +- requirements_all.txt | 2 +- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/bt_smarthub/device_tracker.py b/homeassistant/components/bt_smarthub/device_tracker.py index 45b18b963c5..383f724decd 100644 --- a/homeassistant/components/bt_smarthub/device_tracker.py +++ b/homeassistant/components/bt_smarthub/device_tracker.py @@ -1,7 +1,7 @@ """Support for BT Smart Hub (Sometimes referred to as BT Home Hub 6).""" import logging -import btsmarthub_devicelist +from btsmarthub_devicelist import BTSmartHub import voluptuous as vol from homeassistant.components.device_tracker import ( @@ -15,15 +15,24 @@ import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) CONF_DEFAULT_IP = "192.168.1.254" +CONF_SMARTHUB_MODEL = "smarthub_model" PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( - {vol.Optional(CONF_HOST, default=CONF_DEFAULT_IP): cv.string} + { + vol.Optional(CONF_HOST, default=CONF_DEFAULT_IP): cv.string, + vol.Optional(CONF_SMARTHUB_MODEL): vol.In([1, 2]), + } ) def get_scanner(hass, config): """Return a BT Smart Hub scanner if successful.""" - scanner = BTSmartHubScanner(config[DOMAIN]) + info = config[DOMAIN] + smarthub_client = BTSmartHub( + router_ip=info[CONF_HOST], smarthub_model=info.get(CONF_SMARTHUB_MODEL) + ) + + scanner = BTSmartHubScanner(smarthub_client) return scanner if scanner.success_init else None @@ -31,10 +40,9 @@ def get_scanner(hass, config): class BTSmartHubScanner(DeviceScanner): """This class queries a BT Smart Hub.""" - def __init__(self, config): + def __init__(self, smarthub_client): """Initialise the scanner.""" - _LOGGER.debug("Initialising BT Smart Hub") - self.host = config[CONF_HOST] + self.smarthub = smarthub_client self.last_results = {} self.success_init = False @@ -43,7 +51,7 @@ class BTSmartHubScanner(DeviceScanner): if data: self.success_init = True else: - _LOGGER.info("Failed to connect to %s", self.host) + _LOGGER.info("Failed to connect to %s", self.smarthub.router_ip) def scan_devices(self): """Scan for new devices and return a list with found device IDs.""" @@ -77,9 +85,8 @@ class BTSmartHubScanner(DeviceScanner): """Retrieve data from BT Smart Hub and return parsed result.""" # Request data from bt smarthub into a list of dicts. - data = btsmarthub_devicelist.get_devicelist( - router_ip=self.host, only_active_devices=True - ) + data = self.smarthub.get_devicelist(only_active_devices=True) + # Renaming keys from parsed result. devices = {} for device in data: diff --git a/homeassistant/components/bt_smarthub/manifest.json b/homeassistant/components/bt_smarthub/manifest.json index 5f677a433c8..81f7098e653 100644 --- a/homeassistant/components/bt_smarthub/manifest.json +++ b/homeassistant/components/bt_smarthub/manifest.json @@ -2,6 +2,6 @@ "domain": "bt_smarthub", "name": "BT Smart Hub", "documentation": "https://www.home-assistant.io/integrations/bt_smarthub", - "requirements": ["btsmarthub_devicelist==0.1.3"], + "requirements": ["btsmarthub_devicelist==0.2.0"], "codeowners": ["@jxwolstenholme"] } diff --git a/requirements_all.txt b/requirements_all.txt index 8207dfb0500..4d3b019998d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -376,7 +376,7 @@ bt_proximity==0.2 bthomehub5-devicelist==0.1.1 # homeassistant.components.bt_smarthub -btsmarthub_devicelist==0.1.3 +btsmarthub_devicelist==0.2.0 # homeassistant.components.buienradar buienradar==1.0.4