From 1c147b5c5f0de12044c02fe76053e3736d171862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Fri, 14 Dec 2018 15:51:13 +0200 Subject: [PATCH] huawei_lte: Fetch only required data (#17618) When debug logging is enabled, still fetch everything in order to provide indication of available/supported data to users, as instructed in docs. --- .../components/device_tracker/huawei_lte.py | 5 ++- homeassistant/components/huawei_lte.py | 42 +++++++++++++++---- homeassistant/components/sensor/huawei_lte.py | 1 + 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/device_tracker/huawei_lte.py b/homeassistant/components/device_tracker/huawei_lte.py index 4b4eb3f001a..d30a413898f 100644 --- a/homeassistant/components/device_tracker/huawei_lte.py +++ b/homeassistant/components/device_tracker/huawei_lte.py @@ -23,10 +23,13 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_URL): cv.url, }) +HOSTS_PATH = "wlan_host_list.Hosts" + def get_scanner(hass, config): """Get a Huawei LTE router scanner.""" data = hass.data[DATA_KEY].get_data(config) + data.subscribe(HOSTS_PATH) return HuaweiLteScanner(data) @@ -43,7 +46,7 @@ class HuaweiLteScanner(DeviceScanner): self.data.update() self._hosts = { x["MacAddress"]: x - for x in self.data["wlan_host_list.Hosts.Host"] + for x in self.data[HOSTS_PATH + ".Host"] if x.get("MacAddress") } return list(self._hosts) diff --git a/homeassistant/components/huawei_lte.py b/homeassistant/components/huawei_lte.py index e8d3bc8b3a1..9ed5b9b73c7 100644 --- a/homeassistant/components/huawei_lte.py +++ b/homeassistant/components/huawei_lte.py @@ -50,6 +50,14 @@ class RouterData: traffic_statistics = attr.ib(init=False, factory=dict) wlan_host_list = attr.ib(init=False, factory=dict) + _subscriptions = attr.ib(init=False, factory=set) + + def __attrs_post_init__(self) -> None: + """Fetch device information once, for serial number in @unique_ids.""" + self.subscribe("device_information") + self._update() + self.unsubscribe("device_information") + def __getitem__(self, path: str): """ Get value corresponding to a dotted path. @@ -65,17 +73,34 @@ class RouterData: raise KeyError from err return reduce(operator.getitem, rest, data) + def subscribe(self, path: str) -> None: + """Subscribe to given router data entries.""" + self._subscriptions.add(path.split(".")[0]) + + def unsubscribe(self, path: str) -> None: + """Unsubscribe from given router data entries.""" + self._subscriptions.discard(path.split(".")[0]) + @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self) -> None: """Call API to update data.""" - self.device_information = self.client.device.information() - _LOGGER.debug("device_information=%s", self.device_information) - self.device_signal = self.client.device.signal() - _LOGGER.debug("device_signal=%s", self.device_signal) - self.traffic_statistics = self.client.monitoring.traffic_statistics() - _LOGGER.debug("traffic_statistics=%s", self.traffic_statistics) - self.wlan_host_list = self.client.wlan.host_list() - _LOGGER.debug("wlan_host_list=%s", self.wlan_host_list) + self._update() + + 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 "traffic_statistics" in self._subscriptions: + self.traffic_statistics = \ + self.client.monitoring.traffic_statistics() + _LOGGER.debug("traffic_statistics=%s", self.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) @attr.s @@ -120,7 +145,6 @@ def _setup_lte(hass, lte_config) -> None: client = Client(connection) data = RouterData(client) - data.update() hass.data[DATA_KEY].data[url] = data def cleanup(event): diff --git a/homeassistant/components/sensor/huawei_lte.py b/homeassistant/components/sensor/huawei_lte.py index 5ff5f9b38ae..ae376045544 100644 --- a/homeassistant/components/sensor/huawei_lte.py +++ b/homeassistant/components/sensor/huawei_lte.py @@ -117,6 +117,7 @@ def setup_platform( data = hass.data[DATA_KEY].get_data(config) sensors = [] for path in config.get(CONF_MONITORED_CONDITIONS): + data.subscribe(path) sensors.append(HuaweiLteSensor( data, path, SENSOR_META.get(path, {}))) add_entities(sensors, True)