mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
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 such
This commit is contained in:
parent
f1e4153b2c
commit
f07c714c01
@ -40,7 +40,7 @@ class RouterData:
|
|||||||
client = attr.ib()
|
client = attr.ib()
|
||||||
device_information = attr.ib(init=False, factory=dict)
|
device_information = attr.ib(init=False, factory=dict)
|
||||||
device_signal = 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)
|
wlan_host_list = attr.ib(init=False, factory=dict)
|
||||||
|
|
||||||
_subscriptions = attr.ib(init=False, factory=set)
|
_subscriptions = attr.ib(init=False, factory=set)
|
||||||
@ -87,10 +87,11 @@ class RouterData:
|
|||||||
if debugging or "device_signal" in self._subscriptions:
|
if debugging or "device_signal" in self._subscriptions:
|
||||||
self.device_signal = self.client.device.signal()
|
self.device_signal = self.client.device.signal()
|
||||||
_LOGGER.debug("device_signal=%s", self.device_signal)
|
_LOGGER.debug("device_signal=%s", self.device_signal)
|
||||||
if debugging or "traffic_statistics" in self._subscriptions:
|
if debugging or "monitoring_traffic_statistics" in self._subscriptions:
|
||||||
self.traffic_statistics = \
|
self.monitoring_traffic_statistics = \
|
||||||
self.client.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:
|
if debugging or "wlan_host_list" in self._subscriptions:
|
||||||
self.wlan_host_list = self.client.wlan.host_list()
|
self.wlan_host_list = self.client.wlan.host_list()
|
||||||
_LOGGER.debug("wlan_host_list=%s", self.wlan_host_list)
|
_LOGGER.debug("wlan_host_list=%s", self.wlan_host_list)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Support for Huawei LTE sensors."""
|
"""Support for Huawei LTE sensors."""
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -8,7 +9,8 @@ import voluptuous as vol
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_URL, CONF_MONITORED_CONDITIONS, STATE_UNKNOWN,
|
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
|
from homeassistant.helpers.entity import Entity
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
@ -17,6 +19,7 @@ from . import DATA_KEY, RouterData
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_NAME_TEMPLATE = 'Huawei {} {}'
|
DEFAULT_NAME_TEMPLATE = 'Huawei {} {}'
|
||||||
|
DEFAULT_DEVICE_NAME = 'LTE'
|
||||||
|
|
||||||
DEFAULT_SENSORS = [
|
DEFAULT_SENSORS = [
|
||||||
"device_information.WanIPAddress",
|
"device_information.WanIPAddress",
|
||||||
@ -60,6 +63,7 @@ SENSOR_META = {
|
|||||||
),
|
),
|
||||||
"device_signal.rsrq": dict(
|
"device_signal.rsrq": dict(
|
||||||
name="RSRQ",
|
name="RSRQ",
|
||||||
|
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
# http://www.lte-anbieter.info/technik/rsrq.php
|
# http://www.lte-anbieter.info/technik/rsrq.php
|
||||||
icon=lambda x:
|
icon=lambda x:
|
||||||
(x is None or x < -11) and "mdi:signal-cellular-outline"
|
(x is None or x < -11) and "mdi:signal-cellular-outline"
|
||||||
@ -69,6 +73,7 @@ SENSOR_META = {
|
|||||||
),
|
),
|
||||||
"device_signal.rsrp": dict(
|
"device_signal.rsrp": dict(
|
||||||
name="RSRP",
|
name="RSRP",
|
||||||
|
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
# http://www.lte-anbieter.info/technik/rsrp.php
|
# http://www.lte-anbieter.info/technik/rsrp.php
|
||||||
icon=lambda x:
|
icon=lambda x:
|
||||||
(x is None or x < -110) and "mdi:signal-cellular-outline"
|
(x is None or x < -110) and "mdi:signal-cellular-outline"
|
||||||
@ -78,6 +83,7 @@ SENSOR_META = {
|
|||||||
),
|
),
|
||||||
"device_signal.rssi": dict(
|
"device_signal.rssi": dict(
|
||||||
name="RSSI",
|
name="RSSI",
|
||||||
|
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
# https://eyesaas.com/wi-fi-signal-strength/
|
# https://eyesaas.com/wi-fi-signal-strength/
|
||||||
icon=lambda x:
|
icon=lambda x:
|
||||||
(x is None or x < -80) and "mdi:signal-cellular-outline"
|
(x is None or x < -80) and "mdi:signal-cellular-outline"
|
||||||
@ -87,6 +93,7 @@ SENSOR_META = {
|
|||||||
),
|
),
|
||||||
"device_signal.sinr": dict(
|
"device_signal.sinr": dict(
|
||||||
name="SINR",
|
name="SINR",
|
||||||
|
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
# http://www.lte-anbieter.info/technik/sinr.php
|
# http://www.lte-anbieter.info/technik/sinr.php
|
||||||
icon=lambda x:
|
icon=lambda x:
|
||||||
(x is None or x < 0) and "mdi:signal-cellular-outline"
|
(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)
|
data = hass.data[DATA_KEY].get_data(config)
|
||||||
sensors = []
|
sensors = []
|
||||||
for path in config.get(CONF_MONITORED_CONDITIONS):
|
for path in config.get(CONF_MONITORED_CONDITIONS):
|
||||||
|
if path == "traffic_statistics": # backwards compatibility
|
||||||
|
path = "monitoring_traffic_statistics"
|
||||||
data.subscribe(path)
|
data.subscribe(path)
|
||||||
sensors.append(HuaweiLteSensor(data, path, SENSOR_META.get(path, {})))
|
sensors.append(HuaweiLteSensor(data, path, SENSOR_META.get(path, {})))
|
||||||
|
|
||||||
@ -153,15 +162,24 @@ class HuaweiLteSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return sensor name."""
|
"""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)
|
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
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return sensor state."""
|
"""Return sensor state."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self) -> Optional[str]:
|
||||||
|
"""Return sensor device class."""
|
||||||
|
return self.meta.get("device_class")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return sensor's unit of measurement."""
|
"""Return sensor's unit of measurement."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user