Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion homeassistant/components/device_tracker/huawei_lte.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
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)


Expand All @@ -43,7 +46,7 @@ def scan_devices(self) -> List[str]:
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)
Expand Down
42 changes: 33 additions & 9 deletions homeassistant/components/huawei_lte.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -65,17 +73,34 @@ def __getitem__(self, path: str):
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
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/sensor/huawei_lte.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down