From 169b9b0bfe65aaec56d3d3c111e5ddcc2cf04218 Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Wed, 24 Apr 2024 09:47:03 -0500 Subject: [PATCH] Fix removing suggested_display_precision from entity registry (#110671) * Fix removing suggested_display_precision from entity registry * Fix tests * Update homeassistant/components/sensor/__init__.py --------- Co-authored-by: Erik --- homeassistant/components/sensor/__init__.py | 5 --- tests/components/sensor/test_init.py | 35 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index ad6b3454ea9..a955e861c20 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -786,11 +786,6 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): ratio_log = floor(ratio_log) if ratio_log > 0 else ceil(ratio_log) display_precision = max(0, display_precision + ratio_log) - if display_precision is None and ( - DOMAIN not in self.registry_entry.options - or "suggested_display_precision" not in self.registry_entry.options - ): - return sensor_options: Mapping[str, Any] = self.registry_entry.options.get(DOMAIN, {}) if ( "suggested_display_precision" in sensor_options diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index 74fd81188cd..079984476b0 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -1618,6 +1618,41 @@ async def test_suggested_precision_option_update( } +async def test_suggested_precision_option_removal( + hass: HomeAssistant, +) -> None: + """Test suggested precision stored in the registry is removed.""" + + entity_registry = er.async_get(hass) + + # Pre-register entities + entry = entity_registry.async_get_or_create("sensor", "test", "very_unique") + entity_registry.async_update_entity_options( + entry.entity_id, + "sensor", + { + "suggested_display_precision": 1, + }, + ) + + entity0 = MockSensor( + name="Test", + device_class=SensorDeviceClass.DURATION, + native_unit_of_measurement=UnitOfTime.HOURS, + native_value="1.5", + suggested_display_precision=None, + unique_id="very_unique", + ) + setup_test_component_platform(hass, sensor.DOMAIN, [entity0]) + + assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) + await hass.async_block_till_done() + + # Assert the suggested precision is no longer stored in the registry + entry = entity_registry.async_get(entity0.entity_id) + assert entry.options.get("sensor", {}).get("suggested_display_precision") is None + + @pytest.mark.parametrize( ( "unit_system",