Skip to content
Snippets Groups Projects
Unverified Commit 865190f0 authored by Richard Kroegel's avatar Richard Kroegel Committed by GitHub
Browse files

Get suggested_unit_of_measurement via unit converter's UNIT_CLASS (#119196)

parent 0e0a3395
No related branches found
No related tags found
No related merge requests found
...@@ -394,11 +394,20 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): ...@@ -394,11 +394,20 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
suggested_unit_of_measurement = self.suggested_unit_of_measurement suggested_unit_of_measurement = self.suggested_unit_of_measurement
if suggested_unit_of_measurement is None: if suggested_unit_of_measurement is None:
# Fallback to suggested by the unit conversion rules # Fallback to unit suggested by the unit conversion rules from device class
suggested_unit_of_measurement = self.hass.config.units.get_converted_unit( suggested_unit_of_measurement = self.hass.config.units.get_converted_unit(
self.device_class, self.native_unit_of_measurement self.device_class, self.native_unit_of_measurement
) )
if suggested_unit_of_measurement is None and (
unit_converter := UNIT_CONVERTERS.get(self.device_class)
):
# If the device class is not known by the unit system but has a unit converter,
# fall back to the unit suggested by the unit converter's unit class.
suggested_unit_of_measurement = self.hass.config.units.get_converted_unit(
unit_converter.UNIT_CLASS, self.native_unit_of_measurement
)
if suggested_unit_of_measurement is None: if suggested_unit_of_measurement is None:
return UNDEFINED return UNDEFINED
......
...@@ -942,7 +942,21 @@ async def test_custom_unit_change( ...@@ -942,7 +942,21 @@ async def test_custom_unit_change(
"1000000", "1000000",
"1093613", "1093613",
SensorDeviceClass.DISTANCE, SensorDeviceClass.DISTANCE,
) ),
# Volume Storage (subclass of Volume)
(
US_CUSTOMARY_SYSTEM,
UnitOfVolume.LITERS,
UnitOfVolume.GALLONS,
UnitOfVolume.GALLONS,
UnitOfVolume.FLUID_OUNCES,
1000,
"1000",
"264",
"264",
"33814",
SensorDeviceClass.VOLUME_STORAGE,
),
], ],
) )
async def test_unit_conversion_priority( async def test_unit_conversion_priority(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment