Skip to content

Commit 26444d8

Browse files
authored
Move Shelly sensor translation logic to base class (home-assistant#157129)
Signed-off-by: David Rapan <[email protected]>
1 parent 554c122 commit 26444d8

File tree

3 files changed

+12
-59
lines changed

3 files changed

+12
-59
lines changed

homeassistant/components/shelly/entity.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from .utils import (
2626
async_remove_shelly_entity,
2727
get_block_device_info,
28-
get_entity_translation_attributes,
2928
get_rpc_channel_name,
3029
get_rpc_device_info,
3130
get_rpc_key,
@@ -598,17 +597,14 @@ def available(self) -> bool:
598597

599598
def configure_translation_attributes(self) -> None:
600599
"""Configure translation attributes."""
601-
translation_placeholders, translation_key = get_entity_translation_attributes(
602-
get_rpc_channel_name(self.coordinator.device, self.key),
603-
self.entity_description.translation_key,
604-
self.entity_description.device_class,
605-
self._default_to_device_class_name(),
606-
)
607-
608-
if translation_placeholders:
609-
self._attr_translation_placeholders = translation_placeholders
610-
if translation_key:
611-
self._attr_translation_key = translation_key
600+
if (
601+
channel_name := get_rpc_channel_name(self.coordinator.device, self.key)
602+
) and (
603+
translation_key := self.entity_description.translation_key
604+
or (self.device_class if self._default_to_device_class_name() else None)
605+
):
606+
self._attr_translation_placeholders = {"channel_name": channel_name}
607+
self._attr_translation_key = f"{translation_key}_with_channel_name"
612608

613609

614610
class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity):

homeassistant/components/shelly/sensor.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@
6363
get_blu_trv_device_info,
6464
get_device_entry_gen,
6565
get_device_uptime,
66-
get_entity_translation_attributes,
67-
get_rpc_channel_name,
6866
get_shelly_air_lamp_life,
6967
get_virtual_component_unit,
7068
is_rpc_wifi_stations_disabled,
@@ -106,27 +104,15 @@ def __init__(
106104
"""Initialize select."""
107105
super().__init__(coordinator, key, attribute, description)
108106

109-
if not description.role:
110-
translation_placeholders, translation_key = (
111-
get_entity_translation_attributes(
112-
get_rpc_channel_name(coordinator.device, key),
113-
description.translation_key,
114-
description.device_class,
115-
self._default_to_device_class_name(),
116-
)
117-
)
118-
119-
if translation_placeholders:
120-
self._attr_translation_placeholders = translation_placeholders
121-
if translation_key:
122-
self._attr_translation_key = translation_key
123-
124107
if self.option_map:
125108
if description.role == ROLE_GENERIC:
126109
self._attr_options = list(self.option_map.values())
127110
else:
128111
self._attr_options = list(self.option_map)
129112

113+
if not description.role:
114+
self.configure_translation_attributes()
115+
130116
@property
131117
def native_value(self) -> StateType:
132118
"""Return value of sensor."""
@@ -1898,19 +1884,7 @@ def __init__(
18981884
self.restored_data: SensorExtraStoredData | None = None
18991885

19001886
if coordinator.device.initialized:
1901-
translation_placeholders, translation_key = (
1902-
get_entity_translation_attributes(
1903-
get_rpc_channel_name(coordinator.device, key),
1904-
description.translation_key,
1905-
description.device_class,
1906-
self._default_to_device_class_name(),
1907-
)
1908-
)
1909-
1910-
if translation_placeholders:
1911-
self._attr_translation_placeholders = translation_placeholders
1912-
if translation_key:
1913-
self._attr_translation_key = translation_key
1887+
self.configure_translation_attributes()
19141888

19151889
async def async_added_to_hass(self) -> None:
19161890
"""Handle entity which will be added."""

homeassistant/components/shelly/utils.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -459,23 +459,6 @@ def get_rpc_sub_device_name(
459459
return f"{device.name} {component.title()} {component_id}"
460460

461461

462-
def get_entity_translation_attributes(
463-
channel_name: str | None,
464-
translation_key: str | None,
465-
device_class: str | None,
466-
default_to_device_class_name: bool,
467-
) -> tuple[dict[str, str] | None, str | None]:
468-
"""Translation attributes for entity with channel name."""
469-
if channel_name is None:
470-
return None, None
471-
472-
key = translation_key
473-
if key is None and default_to_device_class_name:
474-
key = device_class
475-
476-
return {"channel_name": channel_name}, f"{key}_with_channel_name" if key else None
477-
478-
479462
def get_device_entry_gen(entry: ConfigEntry) -> int:
480463
"""Return the device generation from config entry."""
481464
return entry.data.get(CONF_GEN, 1) # type: ignore[no-any-return]

0 commit comments

Comments
 (0)