Guard invalid data sensor significant change (#74369)

pull/74522/head
Paulus Schoutsen 2022-07-03 22:03:13 -07:00
parent ce04480e60
commit bb84484096
2 changed files with 16 additions and 4 deletions

View File

@ -63,13 +63,23 @@ def async_check_significant_change(
absolute_change = 1.0
percentage_change = 2.0
try:
# New state is invalid, don't report it
new_state_f = float(new_state)
except ValueError:
return False
try:
# Old state was invalid, we should report again
old_state_f = float(old_state)
except ValueError:
return True
if absolute_change is not None and percentage_change is not None:
return _absolute_and_relative_change(
float(old_state), float(new_state), absolute_change, percentage_change
old_state_f, new_state_f, absolute_change, percentage_change
)
if absolute_change is not None:
return check_absolute_change(
float(old_state), float(new_state), absolute_change
)
return check_absolute_change(old_state_f, new_state_f, absolute_change)
return None

View File

@ -52,6 +52,8 @@ TEMP_FREEDOM_ATTRS = {
("12.1", "12.2", TEMP_CELSIUS_ATTRS, False),
("70", "71", TEMP_FREEDOM_ATTRS, True),
("70", "70.5", TEMP_FREEDOM_ATTRS, False),
("fail", "70", TEMP_FREEDOM_ATTRS, True),
("70", "fail", TEMP_FREEDOM_ATTRS, False),
],
)
async def test_significant_change_temperature(old_state, new_state, attrs, result):