huawei_lte: support out of range values in default sensor formatter (#26052)
* Update sensor.py Change regexp to cope with out of range values, ie "rssi" : ">=-51dBm". * Add tests for format_default * Fix black formattingpull/26075/head
parent
a38bdc4deb
commit
45aec2ea40
|
@ -137,7 +137,9 @@ def format_default(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))
|
||||
match = re.match(
|
||||
r"([>=<]*)(?P<value>.+?)\s*(?P<unit>[a-zA-Z]+)\s*$", str(value)
|
||||
)
|
||||
if match:
|
||||
try:
|
||||
value = float(match.group("value"))
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
"""Huawei LTE sensor tests."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.huawei_lte import sensor
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "expected"),
|
||||
(("-71 dBm", (-71, "dBm")), ("15dB", (15, "dB")), (">=-51dBm", (-51, "dBm"))),
|
||||
)
|
||||
def test_format_default(value, expected):
|
||||
"""Test that default formatter copes with expected values."""
|
||||
assert sensor.format_default(value) == expected
|
Loading…
Reference in New Issue