Huawei LTE misc improvements (#25377)
* Rename traffic_statistics to monitoring_traffic_statistics For better consistency with huawei-lte-api. * Add default device name for sensors In case the actual device name cannot be accessed for some reason. * Support device class in sensor metadata * Mark known signal strength sensors as suchpull/25458/head
parent
f1e4153b2c
commit
f07c714c01
|
@ -40,7 +40,7 @@ class RouterData:
|
|||
client = attr.ib()
|
||||
device_information = attr.ib(init=False, factory=dict)
|
||||
device_signal = attr.ib(init=False, factory=dict)
|
||||
traffic_statistics = attr.ib(init=False, factory=dict)
|
||||
monitoring_traffic_statistics = attr.ib(init=False, factory=dict)
|
||||
wlan_host_list = attr.ib(init=False, factory=dict)
|
||||
|
||||
_subscriptions = attr.ib(init=False, factory=set)
|
||||
|
@ -87,10 +87,11 @@ class RouterData:
|
|||
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 = \
|
||||
if debugging or "monitoring_traffic_statistics" in self._subscriptions:
|
||||
self.monitoring_traffic_statistics = \
|
||||
self.client.monitoring.traffic_statistics()
|
||||
_LOGGER.debug("traffic_statistics=%s", self.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)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
"""Support for Huawei LTE sensors."""
|
||||
import logging
|
||||
import re
|
||||
from typing import Optional
|
||||
|
||||
import attr
|
||||
import voluptuous as vol
|
||||
|
@ -8,7 +9,8 @@ import voluptuous as vol
|
|||
from homeassistant.const import (
|
||||
CONF_URL, CONF_MONITORED_CONDITIONS, STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA, DEVICE_CLASS_SIGNAL_STRENGTH)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
|
@ -17,6 +19,7 @@ from . import DATA_KEY, RouterData
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_NAME_TEMPLATE = 'Huawei {} {}'
|
||||
DEFAULT_DEVICE_NAME = 'LTE'
|
||||
|
||||
DEFAULT_SENSORS = [
|
||||
"device_information.WanIPAddress",
|
||||
|
@ -60,6 +63,7 @@ SENSOR_META = {
|
|||
),
|
||||
"device_signal.rsrq": dict(
|
||||
name="RSRQ",
|
||||
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
# http://www.lte-anbieter.info/technik/rsrq.php
|
||||
icon=lambda x:
|
||||
(x is None or x < -11) and "mdi:signal-cellular-outline"
|
||||
|
@ -69,6 +73,7 @@ SENSOR_META = {
|
|||
),
|
||||
"device_signal.rsrp": dict(
|
||||
name="RSRP",
|
||||
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
# http://www.lte-anbieter.info/technik/rsrp.php
|
||||
icon=lambda x:
|
||||
(x is None or x < -110) and "mdi:signal-cellular-outline"
|
||||
|
@ -78,6 +83,7 @@ SENSOR_META = {
|
|||
),
|
||||
"device_signal.rssi": dict(
|
||||
name="RSSI",
|
||||
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
# https://eyesaas.com/wi-fi-signal-strength/
|
||||
icon=lambda x:
|
||||
(x is None or x < -80) and "mdi:signal-cellular-outline"
|
||||
|
@ -87,6 +93,7 @@ SENSOR_META = {
|
|||
),
|
||||
"device_signal.sinr": dict(
|
||||
name="SINR",
|
||||
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
# http://www.lte-anbieter.info/technik/sinr.php
|
||||
icon=lambda x:
|
||||
(x is None or x < 0) and "mdi:signal-cellular-outline"
|
||||
|
@ -109,6 +116,8 @@ def setup_platform(
|
|||
data = hass.data[DATA_KEY].get_data(config)
|
||||
sensors = []
|
||||
for path in config.get(CONF_MONITORED_CONDITIONS):
|
||||
if path == "traffic_statistics": # backwards compatibility
|
||||
path = "monitoring_traffic_statistics"
|
||||
data.subscribe(path)
|
||||
sensors.append(HuaweiLteSensor(data, path, SENSOR_META.get(path, {})))
|
||||
|
||||
|
@ -153,15 +162,24 @@ class HuaweiLteSensor(Entity):
|
|||
@property
|
||||
def name(self) -> str:
|
||||
"""Return sensor name."""
|
||||
dname = self.data["device_information.DeviceName"]
|
||||
try:
|
||||
dname = self.data["device_information.DeviceName"]
|
||||
except KeyError:
|
||||
dname = None
|
||||
vname = self.meta.get("name", self.path)
|
||||
return DEFAULT_NAME_TEMPLATE.format(dname, vname)
|
||||
return DEFAULT_NAME_TEMPLATE.format(
|
||||
dname or DEFAULT_DEVICE_NAME, vname)
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return sensor state."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def device_class(self) -> Optional[str]:
|
||||
"""Return sensor device class."""
|
||||
return self.meta.get("device_class")
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return sensor's unit of measurement."""
|
||||
|
|
Loading…
Reference in New Issue