Ensure numeric sensors have a valid value (#85605)

* Ensure numeric sensors have a valid value

* Flake8
This commit is contained in:
epenet 2023-03-31 14:12:51 +02:00 committed by GitHub
parent 2e26b6e0cc
commit ab699d17a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 37 deletions

View File

@ -167,7 +167,6 @@ class SensorEntity(Entity):
_attr_unit_of_measurement: None = ( _attr_unit_of_measurement: None = (
None # Subclasses of SensorEntity should not set this None # Subclasses of SensorEntity should not set this
) )
_invalid_numeric_value_reported = False
_invalid_state_class_reported = False _invalid_state_class_reported = False
_invalid_unit_of_measurement_reported = False _invalid_unit_of_measurement_reported = False
_last_reset_reported = False _last_reset_reported = False
@ -463,7 +462,7 @@ class SensorEntity(Entity):
@final @final
@property @property
def state(self) -> Any: # noqa: C901 def state(self) -> Any:
"""Return the state of the sensor and perform unit conversions, if needed.""" """Return the state of the sensor and perform unit conversions, if needed."""
native_unit_of_measurement = self.native_unit_of_measurement native_unit_of_measurement = self.native_unit_of_measurement
unit_of_measurement = self.unit_of_measurement unit_of_measurement = self.unit_of_measurement
@ -581,8 +580,6 @@ class SensorEntity(Entity):
else: else:
numerical_value = float(value) # type:ignore[arg-type] numerical_value = float(value) # type:ignore[arg-type]
except (TypeError, ValueError) as err: except (TypeError, ValueError) as err:
# Raise if precision is not None, for other cases log a warning
if suggested_precision is not None:
raise ValueError( raise ValueError(
f"Sensor {self.entity_id} has device class {device_class}, " f"Sensor {self.entity_id} has device class {device_class}, "
f"state class {state_class} unit {unit_of_measurement} and " f"state class {state_class} unit {unit_of_measurement} and "
@ -590,24 +587,6 @@ class SensorEntity(Entity):
f"has a numeric value; however, it has the non-numeric value: " f"has a numeric value; however, it has the non-numeric value: "
f"{value} ({type(value)})" f"{value} ({type(value)})"
) from err ) from err
# This should raise in Home Assistant Core 2023.4
if not self._invalid_numeric_value_reported:
self._invalid_numeric_value_reported = True
report_issue = self._suggest_report_issue()
_LOGGER.warning(
"Sensor %s has device class %s, state class %s and unit %s "
"thus indicating it has a numeric value; however, it has the "
"non-numeric value: %s (%s); Please update your configuration "
"if your entity is manually configured, otherwise %s",
self.entity_id,
device_class,
state_class,
unit_of_measurement,
value,
type(value),
report_issue,
)
return value
else: else:
numerical_value = value numerical_value = value

View File

@ -1803,20 +1803,20 @@ async def test_device_classes_with_invalid_unit_of_measurement(
], ],
) )
@pytest.mark.parametrize( @pytest.mark.parametrize(
("native_value", "expected"), "native_value",
[ [
("abc", "abc"), "",
("13.7.1", "13.7.1"), "abc",
(datetime(2012, 11, 10, 7, 35, 1), "2012-11-10 07:35:01"), "13.7.1",
(date(2012, 11, 10), "2012-11-10"), datetime(2012, 11, 10, 7, 35, 1),
date(2012, 11, 10),
], ],
) )
async def test_non_numeric_validation_warn( async def test_non_numeric_validation_error(
hass: HomeAssistant, hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
enable_custom_integrations: None, enable_custom_integrations: None,
native_value: Any, native_value: Any,
expected: str,
device_class: SensorDeviceClass | None, device_class: SensorDeviceClass | None,
state_class: SensorStateClass | None, state_class: SensorStateClass | None,
unit: str | None, unit: str | None,
@ -1837,7 +1837,7 @@ async def test_non_numeric_validation_warn(
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(entity0.entity_id) state = hass.states.get(entity0.entity_id)
assert state.state == expected assert state is None
assert ( assert (
"thus indicating it has a numeric value; " "thus indicating it has a numeric value; "