Skip to content

Commit

Permalink
huawei_lte: try unsupported data retrievals only once (home-assistant…
Browse files Browse the repository at this point in the history
…#25524)

* huawei_lte: try unsupported data retrievals only once

Refs home-assistant#23809

* Move huawei_lte_api imports to top level
  • Loading branch information
scop authored and MartinHjelmare committed Jul 29, 2019
1 parent 1f9f201 commit f379bb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
38 changes: 21 additions & 17 deletions homeassistant/components/huawei_lte/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
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,
Expand Down Expand Up @@ -81,20 +85,23 @@ def update(self) -> None:

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
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions script/gen_requirements_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
'homekit[IP]',
'homematicip',
'httplib2',
'huawei-lte-api',
'influxdb',
'jsonpath',
'libpurecool',
Expand Down

0 comments on commit f379bb4

Please sign in to comment.