Move sensor rounding to frontend (#87330)
* Move sensor rounding to frontend * Update integrations * Add comment
This commit is contained in:
parent
ee6773236e
commit
bcc1aa03b4
8 changed files with 231 additions and 307 deletions
|
@ -518,195 +518,6 @@ async def test_custom_unit(
|
|||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == state_unit
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"device_class,native_unit,custom_unit,native_value,native_precision,default_state,custom_state",
|
||||
[
|
||||
(
|
||||
SensorDeviceClass.ATMOSPHERIC_PRESSURE,
|
||||
UnitOfPressure.HPA,
|
||||
UnitOfPressure.INHG,
|
||||
1000.0,
|
||||
2,
|
||||
"1000.00", # Native precision is 2
|
||||
"29.530", # One digit of precision added when converting
|
||||
),
|
||||
(
|
||||
SensorDeviceClass.ATMOSPHERIC_PRESSURE,
|
||||
UnitOfPressure.INHG,
|
||||
UnitOfPressure.HPA,
|
||||
29.9211,
|
||||
3,
|
||||
"29.921", # Native precision is 3
|
||||
"1013.24", # One digit of precision removed when converting
|
||||
),
|
||||
(
|
||||
SensorDeviceClass.ATMOSPHERIC_PRESSURE,
|
||||
UnitOfPressure.INHG,
|
||||
UnitOfPressure.HPA,
|
||||
-0.0001,
|
||||
3,
|
||||
"0.000", # Native precision is 3
|
||||
"0.00", # One digit of precision removed when converting
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_native_precision_scaling(
|
||||
hass,
|
||||
enable_custom_integrations,
|
||||
device_class,
|
||||
native_unit,
|
||||
custom_unit,
|
||||
native_value,
|
||||
native_precision,
|
||||
default_state,
|
||||
custom_state,
|
||||
):
|
||||
"""Test native precision is influenced by unit conversion."""
|
||||
entity_registry = er.async_get(hass)
|
||||
|
||||
entry = entity_registry.async_get_or_create("sensor", "test", "very_unique")
|
||||
platform = getattr(hass.components, "test.sensor")
|
||||
platform.init(empty=True)
|
||||
platform.ENTITIES["0"] = platform.MockSensor(
|
||||
name="Test",
|
||||
native_value=str(native_value),
|
||||
native_precision=native_precision,
|
||||
native_unit_of_measurement=native_unit,
|
||||
device_class=device_class,
|
||||
unique_id="very_unique",
|
||||
)
|
||||
|
||||
entity0 = platform.ENTITIES["0"]
|
||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity0.entity_id)
|
||||
assert state.state == default_state
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == native_unit
|
||||
|
||||
entity_registry.async_update_entity_options(
|
||||
entry.entity_id, "sensor", {"unit_of_measurement": custom_unit}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity0.entity_id)
|
||||
assert state.state == custom_state
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == custom_unit
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"device_class,native_unit,custom_precision,native_value,default_state,custom_state",
|
||||
[
|
||||
(
|
||||
SensorDeviceClass.ATMOSPHERIC_PRESSURE,
|
||||
UnitOfPressure.HPA,
|
||||
4,
|
||||
1000.0,
|
||||
"1000.000",
|
||||
"1000.0000",
|
||||
),
|
||||
(
|
||||
SensorDeviceClass.DISTANCE,
|
||||
UnitOfLength.KILOMETERS,
|
||||
1,
|
||||
-0.04,
|
||||
"-0.040",
|
||||
"0.0", # Make sure minus is dropped
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_custom_precision_native_precision(
|
||||
hass,
|
||||
enable_custom_integrations,
|
||||
device_class,
|
||||
native_unit,
|
||||
custom_precision,
|
||||
native_value,
|
||||
default_state,
|
||||
custom_state,
|
||||
):
|
||||
"""Test custom precision."""
|
||||
entity_registry = er.async_get(hass)
|
||||
|
||||
entry = entity_registry.async_get_or_create("sensor", "test", "very_unique")
|
||||
platform = getattr(hass.components, "test.sensor")
|
||||
platform.init(empty=True)
|
||||
platform.ENTITIES["0"] = platform.MockSensor(
|
||||
name="Test",
|
||||
native_value=str(native_value),
|
||||
native_precision=3,
|
||||
native_unit_of_measurement=native_unit,
|
||||
device_class=device_class,
|
||||
unique_id="very_unique",
|
||||
)
|
||||
|
||||
entity0 = platform.ENTITIES["0"]
|
||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity0.entity_id)
|
||||
assert state.state == default_state
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == native_unit
|
||||
|
||||
entity_registry.async_update_entity_options(
|
||||
entry.entity_id, "sensor", {"precision": custom_precision}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity0.entity_id)
|
||||
assert state.state == custom_state
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == native_unit
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"device_class,native_unit,custom_precision,native_value,custom_state",
|
||||
[
|
||||
(
|
||||
SensorDeviceClass.ATMOSPHERIC_PRESSURE,
|
||||
UnitOfPressure.HPA,
|
||||
4,
|
||||
1000.0,
|
||||
"1000.0000",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_custom_precision_no_native_precision(
|
||||
hass,
|
||||
enable_custom_integrations,
|
||||
device_class,
|
||||
native_unit,
|
||||
custom_precision,
|
||||
native_value,
|
||||
custom_state,
|
||||
):
|
||||
"""Test custom precision."""
|
||||
entity_registry = er.async_get(hass)
|
||||
|
||||
entry = entity_registry.async_get_or_create("sensor", "test", "very_unique")
|
||||
entity_registry.async_update_entity_options(
|
||||
entry.entity_id, "sensor", {"precision": custom_precision}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
platform = getattr(hass.components, "test.sensor")
|
||||
platform.init(empty=True)
|
||||
platform.ENTITIES["0"] = platform.MockSensor(
|
||||
name="Test",
|
||||
native_value=str(native_value),
|
||||
native_unit_of_measurement=native_unit,
|
||||
device_class=device_class,
|
||||
unique_id="very_unique",
|
||||
)
|
||||
|
||||
entity0 = platform.ENTITIES["0"]
|
||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity0.entity_id)
|
||||
assert state.state == custom_state
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == native_unit
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"native_unit, custom_unit, state_unit, native_value, native_state, custom_state, device_class",
|
||||
[
|
||||
|
@ -1188,6 +999,121 @@ async def test_unit_conversion_priority_suggested_unit_change(
|
|||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == original_unit
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"native_unit, suggested_precision, native_value, device_class",
|
||||
[
|
||||
# Distance
|
||||
(
|
||||
UnitOfLength.KILOMETERS,
|
||||
4,
|
||||
1000,
|
||||
SensorDeviceClass.DISTANCE,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_suggested_precision_option(
|
||||
hass,
|
||||
enable_custom_integrations,
|
||||
native_unit,
|
||||
suggested_precision,
|
||||
native_value,
|
||||
device_class,
|
||||
):
|
||||
"""Test suggested precision is stored in the registry."""
|
||||
|
||||
entity_registry = er.async_get(hass)
|
||||
platform = getattr(hass.components, "test.sensor")
|
||||
platform.init(empty=True)
|
||||
|
||||
platform.ENTITIES["0"] = platform.MockSensor(
|
||||
name="Test",
|
||||
device_class=device_class,
|
||||
native_unit_of_measurement=native_unit,
|
||||
native_value=str(native_value),
|
||||
suggested_display_precision=suggested_precision,
|
||||
unique_id="very_unique",
|
||||
)
|
||||
entity0 = platform.ENTITIES["0"]
|
||||
|
||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Assert the suggested precision is stored in the registry
|
||||
entry = entity_registry.async_get(entity0.entity_id)
|
||||
assert entry.options == {
|
||||
"sensor": {"suggested_display_precision": suggested_precision}
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"native_unit, old_precision, new_precision, native_value, device_class",
|
||||
[
|
||||
(
|
||||
UnitOfLength.KILOMETERS,
|
||||
4,
|
||||
1,
|
||||
1000,
|
||||
SensorDeviceClass.DISTANCE,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_suggested_precision_option_update(
|
||||
hass,
|
||||
enable_custom_integrations,
|
||||
native_unit,
|
||||
old_precision,
|
||||
new_precision,
|
||||
native_value,
|
||||
device_class,
|
||||
):
|
||||
"""Test suggested precision stored in the registry is updated."""
|
||||
|
||||
entity_registry = er.async_get(hass)
|
||||
platform = getattr(hass.components, "test.sensor")
|
||||
platform.init(empty=True)
|
||||
|
||||
# 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": old_precision,
|
||||
},
|
||||
)
|
||||
entity_registry.async_update_entity_options(
|
||||
entry.entity_id,
|
||||
"sensor.private",
|
||||
{
|
||||
"suggested_unit_of_measurement": native_unit,
|
||||
},
|
||||
)
|
||||
|
||||
platform.ENTITIES["0"] = platform.MockSensor(
|
||||
name="Test",
|
||||
device_class=device_class,
|
||||
native_unit_of_measurement=native_unit,
|
||||
native_value=str(native_value),
|
||||
suggested_display_precision=new_precision,
|
||||
unique_id="very_unique",
|
||||
)
|
||||
entity0 = platform.ENTITIES["0"]
|
||||
|
||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Assert the suggested precision is stored in the registry
|
||||
entry = entity_registry.async_get(entity0.entity_id)
|
||||
assert entry.options == {
|
||||
"sensor": {
|
||||
"suggested_display_precision": new_precision,
|
||||
},
|
||||
"sensor.private": {
|
||||
"suggested_unit_of_measurement": native_unit,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"unit_system, native_unit, original_unit, native_value, original_value, device_class",
|
||||
[
|
||||
|
@ -1519,10 +1445,10 @@ async def test_non_numeric_validation_raise(
|
|||
platform.ENTITIES["0"] = platform.MockSensor(
|
||||
name="Test",
|
||||
device_class=device_class,
|
||||
native_precision=precision,
|
||||
native_unit_of_measurement=unit,
|
||||
native_value=native_value,
|
||||
state_class=state_class,
|
||||
suggested_display_precision=precision,
|
||||
)
|
||||
entity0 = platform.ENTITIES["0"]
|
||||
|
||||
|
@ -1650,7 +1576,7 @@ async def test_device_classes_with_invalid_state_class(
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"device_class,state_class,native_unit_of_measurement,native_precision,is_numeric",
|
||||
"device_class,state_class,native_unit_of_measurement,suggested_precision,is_numeric",
|
||||
[
|
||||
(SensorDeviceClass.ENUM, None, None, None, False),
|
||||
(SensorDeviceClass.DATE, None, None, None, False),
|
||||
|
@ -1669,7 +1595,7 @@ async def test_numeric_state_expected_helper(
|
|||
device_class: SensorDeviceClass | None,
|
||||
state_class: SensorStateClass | None,
|
||||
native_unit_of_measurement: str | None,
|
||||
native_precision: int | None,
|
||||
suggested_precision: int | None,
|
||||
is_numeric: bool,
|
||||
) -> None:
|
||||
"""Test numeric_state_expected helper."""
|
||||
|
@ -1681,7 +1607,7 @@ async def test_numeric_state_expected_helper(
|
|||
device_class=device_class,
|
||||
state_class=state_class,
|
||||
native_unit_of_measurement=native_unit_of_measurement,
|
||||
native_precision=native_precision,
|
||||
suggested_display_precision=suggested_precision,
|
||||
)
|
||||
|
||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue