Raise error if sensor has translated and hardcoded unit (#131657)

This commit is contained in:
Abílio Costa
2024-11-27 16:45:53 +00:00
committed by GitHub
parent 3485ce9c71
commit e4e9d76b45
2 changed files with 37 additions and 1 deletions

View File

@@ -504,7 +504,6 @@ async def test_translated_unit(
entity0.entity_description = SensorEntityDescription(
"test",
translation_key="test_translation_key",
native_unit_of_measurement="ignored_unit",
)
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
@@ -518,6 +517,37 @@ async def test_translated_unit(
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "Tests"
async def test_translated_unit_with_native_unit_raises(
hass: HomeAssistant,
) -> None:
"""Test that translated unit."""
with patch(
"homeassistant.helpers.service.translation.async_get_translations",
return_value={
"component.test.entity.sensor.test_translation_key.unit_of_measurement": "Tests"
},
):
entity0 = MockSensor(
name="Test",
native_value="123",
unique_id="very_unique",
)
entity0.entity_description = SensorEntityDescription(
"test",
translation_key="test_translation_key",
native_unit_of_measurement="bad_unit",
)
setup_test_component_platform(hass, sensor.DOMAIN, [entity0])
assert await async_setup_component(
hass, "sensor", {"sensor": {"platform": "test"}}
)
await hass.async_block_till_done()
# Setup fails so entity_id is None
assert entity0.entity_id is None
@pytest.mark.parametrize(
(
"device_class",