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 <erik@montnemery.com>
This commit is contained in:
Phil Bruckner 2024-04-24 09:47:03 -05:00 committed by GitHub
parent bbaa0c16cc
commit 169b9b0bfe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 5 deletions

View File

@ -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

View File

@ -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",