From d62cdf3d659563ff568ba054026e89eff12c1d9d Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 23 May 2023 15:54:24 +0200 Subject: [PATCH] Improve error message for sensors with a non-numeric value (#93399) * Improve error message for sensors with a non-numeric value * Address review comment --- homeassistant/components/sensor/__init__.py | 8 ++++---- tests/components/sensor/test_init.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index 9b0a3b5e3d4..f21f57d9d36 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -583,11 +583,11 @@ class SensorEntity(Entity): numerical_value = float(value) # type:ignore[arg-type] except (TypeError, ValueError) as err: raise ValueError( - f"Sensor {self.entity_id} has device class {device_class}, " - f"state class {state_class} unit {unit_of_measurement} and " - f"suggested precision {suggested_precision} thus indicating it " + f"Sensor {self.entity_id} has device class '{device_class}', " + f"state class '{state_class}' unit '{unit_of_measurement}' and " + f"suggested precision '{suggested_precision}' thus indicating it " f"has a numeric value; however, it has the non-numeric value: " - f"{value} ({type(value)})" + f"'{value}' ({type(value)})" ) from err else: numerical_value = value diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index 7c637b0a6f2..adcbb8084b7 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -1842,7 +1842,7 @@ async def test_non_numeric_validation_error( assert ( "thus indicating it has a numeric value; " - f"however, it has the non-numeric value: {native_value}" + f"however, it has the non-numeric value: '{native_value}'" ) in caplog.text