diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index 1d06e1a24c4..ad6b3454ea9 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -747,13 +747,15 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): return value - def _suggested_precision_or_none(self) -> int | None: - """Return suggested display precision, or None if not set.""" + def _display_precision_or_none(self) -> int | None: + """Return display precision, or None if not set.""" assert self.registry_entry - if (sensor_options := self.registry_entry.options.get(DOMAIN)) and ( - precision := sensor_options.get("suggested_display_precision") - ) is not None: - return cast(int, precision) + if not (sensor_options := self.registry_entry.options.get(DOMAIN)): + return None + + for option in ("display_precision", "suggested_display_precision"): + if (precision := sensor_options.get(option)) is not None: + return cast(int, precision) return None def _update_suggested_precision(self) -> None: @@ -835,7 +837,7 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): Called when the entity registry entry has been updated and before the sensor is added to the state machine. """ - self._sensor_option_display_precision = self._suggested_precision_or_none() + self._sensor_option_display_precision = self._display_precision_or_none() assert self.registry_entry if ( sensor_options := self.registry_entry.options.get(f"{DOMAIN}.private") diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index 9e8e401ea46..74fd81188cd 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -1146,6 +1146,14 @@ async def test_unit_conversion_priority_precision( suggested_display_precision=suggested_precision, suggested_unit_of_measurement=suggested_unit, ) + entity4 = MockSensor( + name="Test", + device_class=device_class, + native_unit_of_measurement=native_unit, + native_value=str(native_value), + suggested_display_precision=None, + unique_id="very_unique_4", + ) setup_test_component_platform( hass, sensor.DOMAIN, @@ -1154,6 +1162,7 @@ async def test_unit_conversion_priority_precision( entity1, entity2, entity3, + entity4, ], ) @@ -1230,6 +1239,21 @@ async def test_unit_conversion_priority_precision( round(custom_state, 4) ) + # Set a display_precision without having suggested_display_precision + entity_registry.async_update_entity_options( + entity4.entity_id, + "sensor", + {"display_precision": 4}, + ) + entry4 = entity_registry.async_get(entity4.entity_id) + assert "suggested_display_precision" not in entry4.options["sensor"] + assert entry4.options["sensor"]["display_precision"] == 4 + await hass.async_block_till_done() + state = hass.states.get(entity4.entity_id) + assert float(async_rounded_state(hass, entity4.entity_id, state)) == pytest.approx( + round(automatic_state, 4) + ) + @pytest.mark.parametrize( (