1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add default device class display precision for Sensor (#145013)

* Add default device class display precision for Sensor

* Renaming, docstrings, cleanup

* Simplify units list

* Fix tests

* Fix missing precision when suggested is specified

* Update snapshots

* Fix when unit of measurement is not valid

* Fix tests

* Fix deprecated unit usage

* Fix goalzero tests

The sensor native_value method was accessing the data dict and trowing,
since the mock did not have any data for the sensors.

Since now the precision is always specified (it was missing for those
sensors), the throw was hitting async_update_entity_options in _update_suggested_precision.
Previously, async_update_entity_options was not called since it had no
precision.

* Fix metoffice

* Fix smartthings

* Add default sensor data for Tesla Wall Connector tests

* Update snapshots

* Revert spaces

* Update smartthings snapshots

* Add missing sensor mock for tesla wall connector

* Address review comments

* Add doc comment

* Add cap to doc comment

* Update comment

* Update snapshots

* Update comment
This commit is contained in:
Abílio Costa
2025-05-26 18:40:29 +01:00
committed by GitHub
parent b15989f2bf
commit b626204f63
123 changed files with 4523 additions and 377 deletions

View File

@@ -1959,3 +1959,28 @@ def get_schema_suggested_value(schema: vol.Schema, key: str) -> Any | None:
return None
return schema_key.description["suggested_value"]
return None
def get_sensor_display_state(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entity_id: str
) -> str:
"""Return the state rounded for presentation."""
state = hass.states.get(entity_id)
assert state
value = state.state
entity_entry = entity_registry.async_get(entity_id)
if entity_entry is None:
return value
if (
precision := entity_entry.options.get("sensor", {}).get(
"suggested_display_precision"
)
) is None:
return value
with suppress(TypeError, ValueError):
numerical_value = float(value)
value = f"{numerical_value:z.{precision}f}"
return value