Skip to content

Commit

Permalink
Use translations on NumberEntity unit_of_measurement property (home-a…
Browse files Browse the repository at this point in the history
…ssistant#132095)

Co-authored-by: Martin Hjelmare <[email protected]>
  • Loading branch information
abmantis and MartinHjelmare authored Dec 3, 2024
1 parent 13e4c51 commit 208b14d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 17 deletions.
12 changes: 12 additions & 0 deletions homeassistant/components/number/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,18 @@ def unit_of_measurement(self) -> str | None:
):
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
Expand Down
16 changes: 0 additions & 16 deletions homeassistant/components/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,6 @@ def suggested_unit_of_measurement(self) -> str | None:
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
Expand Down
16 changes: 16 additions & 0 deletions homeassistant/helpers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,22 @@ def _name_translation_key(self) -> str | None:
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:
Expand Down
65 changes: 64 additions & 1 deletion tests/components/number/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."""

Expand Down

0 comments on commit 208b14d

Please sign in to comment.