From f379bb40169830ad6b7c51bb9b5669fa427e6400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Mon, 29 Jul 2019 11:08:49 +0300 Subject: [PATCH] huawei_lte: try unsupported data retrievals only once (#25524) * huawei_lte: try unsupported data retrievals only once Refs https://github.com/home-assistant/home-assistant/pull/23809 * Move huawei_lte_api imports to top level --- .../components/huawei_lte/__init__.py | 38 ++++++++++--------- requirements_test_all.txt | 3 ++ script/gen_requirements_all.py | 1 + 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/huawei_lte/__init__.py b/homeassistant/components/huawei_lte/__init__.py index ed92c747689..ca2b7e6805c 100644 --- a/homeassistant/components/huawei_lte/__init__.py +++ b/homeassistant/components/huawei_lte/__init__.py @@ -3,9 +3,13 @@ from datetime import timedelta from functools import reduce import logging import operator +from typing import Any, Callable import voluptuous as vol import attr +from huawei_lte_api.AuthorizedConnection import AuthorizedConnection +from huawei_lte_api.Client import Client +from huawei_lte_api.exceptions import ResponseErrorNotSupportedException from homeassistant.const import ( CONF_URL, CONF_USERNAME, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP, @@ -81,20 +85,23 @@ class RouterData: def _update(self) -> None: debugging = _LOGGER.isEnabledFor(logging.DEBUG) - if debugging or "device_information" in self._subscriptions: - self.device_information = self.client.device.information() - _LOGGER.debug("device_information=%s", self.device_information) - if debugging or "device_signal" in self._subscriptions: - self.device_signal = self.client.device.signal() - _LOGGER.debug("device_signal=%s", self.device_signal) - if debugging or "monitoring_traffic_statistics" in self._subscriptions: - self.monitoring_traffic_statistics = \ - self.client.monitoring.traffic_statistics() - _LOGGER.debug("monitoring_traffic_statistics=%s", - self.monitoring_traffic_statistics) - if debugging or "wlan_host_list" in self._subscriptions: - self.wlan_host_list = self.client.wlan.host_list() - _LOGGER.debug("wlan_host_list=%s", self.wlan_host_list) + + def get_data(path: str, func: Callable[[None], Any]) -> None: + if debugging or path in self._subscriptions: + try: + setattr(self, path, func()) + except ResponseErrorNotSupportedException as ex: + _LOGGER.warning( + "%s not supported by device", path, exc_info=ex) + self._subscriptions.discard(path) + finally: + _LOGGER.debug("%s=%s", path, getattr(self, path)) + + get_data("device_information", self.client.device.information) + get_data("device_signal", self.client.device.signal) + get_data("monitoring_traffic_statistics", + self.client.monitoring.traffic_statistics) + get_data("wlan_host_list", self.client.wlan.host_list) @attr.s @@ -124,9 +131,6 @@ def setup(hass, config) -> bool: def _setup_lte(hass, lte_config) -> None: """Set up Huawei LTE router.""" - from huawei_lte_api.AuthorizedConnection import AuthorizedConnection - from huawei_lte_api.Client import Client - url = lte_config[CONF_URL] username = lte_config[CONF_USERNAME] password = lte_config[CONF_PASSWORD] diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 8fad875614e..fbc12e7606e 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -177,6 +177,9 @@ homematicip==0.10.9 # homeassistant.components.remember_the_milk httplib2==0.10.3 +# homeassistant.components.huawei_lte +huawei-lte-api==1.2.0 + # homeassistant.components.influxdb influxdb==5.2.0 diff --git a/script/gen_requirements_all.py b/script/gen_requirements_all.py index 61690a65054..b88409f75d1 100755 --- a/script/gen_requirements_all.py +++ b/script/gen_requirements_all.py @@ -87,6 +87,7 @@ TEST_REQUIREMENTS = ( 'homekit[IP]', 'homematicip', 'httplib2', + 'huawei-lte-api', 'influxdb', 'jsonpath', 'libpurecool',