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 formatting
pull/26075/head
flebourse 2019-08-19 19:34:33 +02:00 committed by Ville Skyttä
parent a38bdc4deb
commit 45aec2ea40
2 changed files with 17 additions and 1 deletions

View File

@ -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"))

View File

@ -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