mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
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.
This commit is contained in:
parent
9c62149b00
commit
1c147b5c5f
@ -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)
|
||||
|
@ -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):
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user