Move Shelly sensor translation logic to base class (#157129)

Signed-off-by: David Rapan <david@rapan.cz>
This commit is contained in:
David Rapan
2025-11-26 09:43:16 +01:00
committed by GitHub
parent 554c122a37
commit 26444d8d34
3 changed files with 12 additions and 59 deletions

View File

@@ -25,7 +25,6 @@ from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoo
from .utils import (
async_remove_shelly_entity,
get_block_device_info,
get_entity_translation_attributes,
get_rpc_channel_name,
get_rpc_device_info,
get_rpc_key,
@@ -598,17 +597,14 @@ class ShellyRpcAttributeEntity(ShellyRpcEntity, Entity):
def configure_translation_attributes(self) -> None:
"""Configure translation attributes."""
translation_placeholders, translation_key = get_entity_translation_attributes(
get_rpc_channel_name(self.coordinator.device, self.key),
self.entity_description.translation_key,
self.entity_description.device_class,
self._default_to_device_class_name(),
)
if translation_placeholders:
self._attr_translation_placeholders = translation_placeholders
if translation_key:
self._attr_translation_key = translation_key
if (
channel_name := get_rpc_channel_name(self.coordinator.device, self.key)
) and (
translation_key := self.entity_description.translation_key
or (self.device_class if self._default_to_device_class_name() else None)
):
self._attr_translation_placeholders = {"channel_name": channel_name}
self._attr_translation_key = f"{translation_key}_with_channel_name"
class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity):

View File

@@ -63,8 +63,6 @@ from .utils import (
get_blu_trv_device_info,
get_device_entry_gen,
get_device_uptime,
get_entity_translation_attributes,
get_rpc_channel_name,
get_shelly_air_lamp_life,
get_virtual_component_unit,
is_rpc_wifi_stations_disabled,
@@ -106,27 +104,15 @@ class RpcSensor(ShellyRpcAttributeEntity, SensorEntity):
"""Initialize select."""
super().__init__(coordinator, key, attribute, description)
if not description.role:
translation_placeholders, translation_key = (
get_entity_translation_attributes(
get_rpc_channel_name(coordinator.device, key),
description.translation_key,
description.device_class,
self._default_to_device_class_name(),
)
)
if translation_placeholders:
self._attr_translation_placeholders = translation_placeholders
if translation_key:
self._attr_translation_key = translation_key
if self.option_map:
if description.role == ROLE_GENERIC:
self._attr_options = list(self.option_map.values())
else:
self._attr_options = list(self.option_map)
if not description.role:
self.configure_translation_attributes()
@property
def native_value(self) -> StateType:
"""Return value of sensor."""
@@ -1898,19 +1884,7 @@ class RpcSleepingSensor(ShellySleepingRpcAttributeEntity, RestoreSensor):
self.restored_data: SensorExtraStoredData | None = None
if coordinator.device.initialized:
translation_placeholders, translation_key = (
get_entity_translation_attributes(
get_rpc_channel_name(coordinator.device, key),
description.translation_key,
description.device_class,
self._default_to_device_class_name(),
)
)
if translation_placeholders:
self._attr_translation_placeholders = translation_placeholders
if translation_key:
self._attr_translation_key = translation_key
self.configure_translation_attributes()
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""

View File

@@ -459,23 +459,6 @@ def get_rpc_sub_device_name(
return f"{device.name} {component.title()} {component_id}"
def get_entity_translation_attributes(
channel_name: str | None,
translation_key: str | None,
device_class: str | None,
default_to_device_class_name: bool,
) -> tuple[dict[str, str] | None, str | None]:
"""Translation attributes for entity with channel name."""
if channel_name is None:
return None, None
key = translation_key
if key is None and default_to_device_class_name:
key = device_class
return {"channel_name": channel_name}, f"{key}_with_channel_name" if key else None
def get_device_entry_gen(entry: ConfigEntry) -> int:
"""Return the device generation from config entry."""
return entry.data.get(CONF_GEN, 1) # type: ignore[no-any-return]