Huawei LTE sensor improvements (#17533)
* Sensor value formatting improvements * Make default names more consistent with other sensors * Improve unique id formatting
This commit is contained in:
parent
5088e7ee49
commit
b7b4224429
1 changed files with 42 additions and 15 deletions
|
@ -24,7 +24,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEPENDENCIES = ['huawei_lte']
|
DEPENDENCIES = ['huawei_lte']
|
||||||
|
|
||||||
DEFAULT_NAME_TEMPLATE = 'Huawei {}: {}'
|
DEFAULT_NAME_TEMPLATE = 'Huawei {} {}'
|
||||||
|
|
||||||
DEFAULT_SENSORS = [
|
DEFAULT_SENSORS = [
|
||||||
"device_information.WanIPAddress",
|
"device_information.WanIPAddress",
|
||||||
|
@ -46,6 +46,26 @@ SENSOR_META = {
|
||||||
name="WAN IPv6 address",
|
name="WAN IPv6 address",
|
||||||
icon="mdi:ip",
|
icon="mdi:ip",
|
||||||
),
|
),
|
||||||
|
"device_signal.band": dict(
|
||||||
|
name="Band",
|
||||||
|
),
|
||||||
|
"device_signal.cell_id": dict(
|
||||||
|
name="Cell ID",
|
||||||
|
),
|
||||||
|
"device_signal.lac": dict(
|
||||||
|
name="LAC",
|
||||||
|
),
|
||||||
|
"device_signal.mode": dict(
|
||||||
|
name="Mode",
|
||||||
|
formatter=lambda x: ({
|
||||||
|
'0': '2G',
|
||||||
|
'2': '3G',
|
||||||
|
'7': '4G',
|
||||||
|
}.get(x, 'Unknown'), None),
|
||||||
|
),
|
||||||
|
"device_signal.pci": dict(
|
||||||
|
name="PCI",
|
||||||
|
),
|
||||||
"device_signal.rsrq": dict(
|
"device_signal.rsrq": dict(
|
||||||
name="RSRQ",
|
name="RSRQ",
|
||||||
# http://www.lte-anbieter.info/technik/rsrq.php
|
# http://www.lte-anbieter.info/technik/rsrq.php
|
||||||
|
@ -102,6 +122,22 @@ def setup_platform(
|
||||||
add_entities(sensors, True)
|
add_entities(sensors, True)
|
||||||
|
|
||||||
|
|
||||||
|
def format_default(value):
|
||||||
|
"""Format value."""
|
||||||
|
unit = None
|
||||||
|
if value is not None:
|
||||||
|
# Clean up value and infer unit, e.g. -71dBm, 15 dB
|
||||||
|
match = re.match(
|
||||||
|
r"(?P<value>.+?)\s*(?P<unit>[a-zA-Z]+)\s*$", str(value))
|
||||||
|
if match:
|
||||||
|
try:
|
||||||
|
value = float(match.group("value"))
|
||||||
|
unit = match.group("unit")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
return value, unit
|
||||||
|
|
||||||
|
|
||||||
@attr.s
|
@attr.s
|
||||||
class HuaweiLteSensor(Entity):
|
class HuaweiLteSensor(Entity):
|
||||||
"""Huawei LTE sensor entity."""
|
"""Huawei LTE sensor entity."""
|
||||||
|
@ -117,8 +153,8 @@ class HuaweiLteSensor(Entity):
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
"""Return unique ID for sensor."""
|
"""Return unique ID for sensor."""
|
||||||
return "{}_{}".format(
|
return "{}_{}".format(
|
||||||
self.path,
|
|
||||||
self.data["device_information.SerialNumber"],
|
self.data["device_information.SerialNumber"],
|
||||||
|
".".join(self.path),
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -150,23 +186,14 @@ class HuaweiLteSensor(Entity):
|
||||||
"""Update state."""
|
"""Update state."""
|
||||||
self.data.update()
|
self.data.update()
|
||||||
|
|
||||||
unit = None
|
|
||||||
try:
|
try:
|
||||||
value = self.data[self.path]
|
value = self.data[self.path]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
_LOGGER.warning("%s not in data", self.path)
|
_LOGGER.warning("%s not in data", self.path)
|
||||||
value = None
|
value = None
|
||||||
|
|
||||||
if value is not None:
|
formatter = self.meta.get("formatter")
|
||||||
# Clean up value and infer unit, e.g. -71dBm, 15 dB
|
if not callable(formatter):
|
||||||
match = re.match(
|
formatter = format_default
|
||||||
r"(?P<value>.+?)\s*(?P<unit>[a-zA-Z]+)\s*$", str(value))
|
|
||||||
if match:
|
|
||||||
try:
|
|
||||||
value = float(match.group("value"))
|
|
||||||
unit = match.group("unit")
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self._state = value
|
self._state, self._unit = formatter(value)
|
||||||
self._unit = unit
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue