mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Use translations on NumberEntity unit_of_measurement property (#132095)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
2aea738032
commit
f480cc3396
@ -384,6 +384,18 @@ class NumberEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
||||
):
|
||||
return self.hass.config.units.temperature_unit
|
||||
|
||||
if (translation_key := self._unit_of_measurement_translation_key) and (
|
||||
unit_of_measurement
|
||||
:= self.platform.default_language_platform_translations.get(translation_key)
|
||||
):
|
||||
if native_unit_of_measurement is not None:
|
||||
raise ValueError(
|
||||
f"Number entity {type(self)} from integration '{self.platform.platform_name}' "
|
||||
f"has a translation key for unit_of_measurement '{unit_of_measurement}', "
|
||||
f"but also has a native_unit_of_measurement '{native_unit_of_measurement}'"
|
||||
)
|
||||
return unit_of_measurement
|
||||
|
||||
return native_unit_of_measurement
|
||||
|
||||
@cached_property
|
||||
|
@ -504,22 +504,6 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
||||
return self.entity_description.suggested_unit_of_measurement
|
||||
return None
|
||||
|
||||
@cached_property
|
||||
def _unit_of_measurement_translation_key(self) -> str | None:
|
||||
"""Return translation key for unit of measurement."""
|
||||
if self.translation_key is None:
|
||||
return None
|
||||
if self.platform is None:
|
||||
raise ValueError(
|
||||
f"Sensor {type(self)} cannot have a translation key for "
|
||||
"unit of measurement before being added to the entity platform"
|
||||
)
|
||||
platform = self.platform
|
||||
return (
|
||||
f"component.{platform.platform_name}.entity.{platform.domain}"
|
||||
f".{self.translation_key}.unit_of_measurement"
|
||||
)
|
||||
|
||||
@final
|
||||
@property
|
||||
@override
|
||||
|
@ -647,6 +647,22 @@ class Entity(
|
||||
f".{self.translation_key}.name"
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def _unit_of_measurement_translation_key(self) -> str | None:
|
||||
"""Return translation key for unit of measurement."""
|
||||
if self.translation_key is None:
|
||||
return None
|
||||
if self.platform is None:
|
||||
raise ValueError(
|
||||
f"Entity {type(self)} cannot have a translation key for "
|
||||
"unit of measurement before being added to the entity platform"
|
||||
)
|
||||
platform = self.platform
|
||||
return (
|
||||
f"component.{platform.platform_name}.entity.{platform.domain}"
|
||||
f".{self.translation_key}.unit_of_measurement"
|
||||
)
|
||||
|
||||
def _substitute_name_placeholders(self, name: str) -> str:
|
||||
"""Substitute placeholders in entity name."""
|
||||
try:
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
from collections.abc import Generator
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@ -836,6 +836,69 @@ async def test_custom_unit_change(
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == default_unit
|
||||
|
||||
|
||||
async def test_translated_unit(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test translated unit."""
|
||||
|
||||
with patch(
|
||||
"homeassistant.helpers.service.translation.async_get_translations",
|
||||
return_value={
|
||||
"component.test.entity.number.test_translation_key.unit_of_measurement": "Tests"
|
||||
},
|
||||
):
|
||||
entity0 = common.MockNumberEntity(
|
||||
name="Test",
|
||||
native_value=123,
|
||||
unique_id="very_unique",
|
||||
)
|
||||
entity0.entity_description = NumberEntityDescription(
|
||||
"test",
|
||||
translation_key="test_translation_key",
|
||||
)
|
||||
setup_test_component_platform(hass, DOMAIN, [entity0])
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, "number", {"number": {"platform": "test"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_id = entity0.entity_id
|
||||
state = hass.states.get(entity_id)
|
||||
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.number.test_translation_key.unit_of_measurement": "Tests"
|
||||
},
|
||||
):
|
||||
entity0 = common.MockNumberEntity(
|
||||
name="Test",
|
||||
native_value=123,
|
||||
unique_id="very_unique",
|
||||
)
|
||||
entity0.entity_description = NumberEntityDescription(
|
||||
"test",
|
||||
translation_key="test_translation_key",
|
||||
native_unit_of_measurement="bad_unit",
|
||||
)
|
||||
setup_test_component_platform(hass, DOMAIN, [entity0])
|
||||
|
||||
assert await async_setup_component(
|
||||
hass, "number", {"number": {"platform": "test"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
# Setup fails so entity_id is None
|
||||
assert entity0.entity_id is None
|
||||
|
||||
|
||||
def test_device_classes_aligned() -> None:
|
||||
"""Make sure all sensor device classes are also available in NumberDeviceClass."""
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user