Skip to content

Commit 3574f64

Browse files
authored
Move Shelly binary sensor translation logic to base class (home-assistant#157127)
Signed-off-by: David Rapan <[email protected]>
1 parent 6c4296a commit 3574f64

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

homeassistant/components/shelly/binary_sensor.py

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
async_remove_orphaned_entities,
3939
get_blu_trv_device_info,
4040
get_device_entry_gen,
41-
get_entity_translation_attributes,
42-
get_rpc_channel_name,
4341
get_rpc_custom_name,
4442
get_rpc_key,
4543
is_block_momentary_input,
@@ -83,27 +81,14 @@ def __init__(
8381
super().__init__(coordinator, key, attribute, description)
8482

8583
if not description.role:
86-
if description.key == "input":
87-
if custom_name := get_rpc_custom_name(coordinator.device, key):
88-
self._attr_name = custom_name
89-
else:
90-
_, _, component_id = get_rpc_key(key)
91-
self._attr_translation_placeholders = {"input_number": component_id}
92-
self._attr_translation_key = "input_with_number"
84+
if description.key != "input":
85+
self.configure_translation_attributes()
86+
elif custom_name := get_rpc_custom_name(coordinator.device, key):
87+
self._attr_name = custom_name
9388
else:
94-
translation_placeholders, translation_key = (
95-
get_entity_translation_attributes(
96-
get_rpc_channel_name(coordinator.device, key),
97-
description.translation_key,
98-
description.device_class,
99-
self._default_to_device_class_name(),
100-
)
101-
)
102-
103-
if translation_placeholders:
104-
self._attr_translation_placeholders = translation_placeholders
105-
if translation_key:
106-
self._attr_translation_key = translation_key
89+
_, _, component_id = get_rpc_key(key)
90+
self._attr_translation_placeholders = {"input_number": component_id}
91+
self._attr_translation_key = "input_with_number"
10792

10893
@property
10994
def is_on(self) -> bool:
@@ -508,19 +493,7 @@ def __init__(
508493
super().__init__(coordinator, key, attribute, description, entry)
509494

510495
if coordinator.device.initialized:
511-
translation_placeholders, translation_key = (
512-
get_entity_translation_attributes(
513-
get_rpc_channel_name(coordinator.device, key),
514-
description.translation_key,
515-
description.device_class,
516-
self._default_to_device_class_name(),
517-
)
518-
)
519-
520-
if translation_placeholders:
521-
self._attr_translation_placeholders = translation_placeholders
522-
if translation_key:
523-
self._attr_translation_key = translation_key
496+
self.configure_translation_attributes()
524497

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

homeassistant/components/shelly/entity.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from .utils import (
2626
async_remove_shelly_entity,
2727
get_block_device_info,
28+
get_entity_translation_attributes,
2829
get_rpc_channel_name,
2930
get_rpc_device_info,
3031
get_rpc_key,
@@ -595,6 +596,20 @@ def available(self) -> bool:
595596

596597
return self.entity_description.available(self.sub_status)
597598

599+
def configure_translation_attributes(self) -> None:
600+
"""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
612+
598613

599614
class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity):
600615
"""Represent a shelly sleeping block attribute entity."""

0 commit comments

Comments
 (0)