Skip to content

Commit 9aec7b1

Browse files
davidrapanthecode
andauthored
Refactor Shelly entity to remove name assignments (#157018)
Co-authored-by: Shay Levy <[email protected]>
1 parent bf42e37 commit 9aec7b1

File tree

2 files changed

+14
-60
lines changed

2 files changed

+14
-60
lines changed

homeassistant/components/shelly/entity.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
from homeassistant.helpers.typing import StateType
2121
from homeassistant.helpers.update_coordinator import CoordinatorEntity
2222

23-
from .const import CONF_SLEEP_PERIOD, DOMAIN, LOGGER
23+
from .const import CONF_SLEEP_PERIOD, DOMAIN, LOGGER, ROLE_GENERIC
2424
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
2525
from .utils import (
2626
async_remove_shelly_entity,
2727
get_block_device_info,
28-
get_block_entity_name,
28+
get_rpc_channel_name,
2929
get_rpc_device_info,
30-
get_rpc_entity_name,
30+
get_rpc_key,
3131
get_rpc_key_instances,
3232
get_rpc_role_by_key,
3333
)
@@ -371,7 +371,7 @@ def __init__(self, coordinator: ShellyBlockCoordinator, block: Block) -> None:
371371
"""Initialize Shelly entity."""
372372
super().__init__(coordinator)
373373
self.block = block
374-
self._attr_name = get_block_entity_name(coordinator.device, block)
374+
375375
self._attr_device_info = get_entity_block_device_info(coordinator, block)
376376
self._attr_unique_id = f"{coordinator.mac}-{block.description}"
377377

@@ -413,9 +413,9 @@ def __init__(self, coordinator: ShellyRpcCoordinator, key: str) -> None:
413413
"""Initialize Shelly entity."""
414414
super().__init__(coordinator)
415415
self.key = key
416+
416417
self._attr_device_info = get_entity_rpc_device_info(coordinator, key)
417418
self._attr_unique_id = f"{coordinator.mac}-{key}"
418-
self._attr_name = get_rpc_entity_name(coordinator.device, key)
419419

420420
@property
421421
def available(self) -> bool:
@@ -467,9 +467,6 @@ def __init__(
467467
self.entity_description = description
468468

469469
self._attr_unique_id: str = f"{super().unique_id}-{self.attribute}"
470-
self._attr_name = get_block_entity_name(
471-
coordinator.device, block, description.name
472-
)
473470

474471
@property
475472
def attribute_value(self) -> StateType:
@@ -507,9 +504,7 @@ def __init__(
507504
self.block_coordinator = coordinator
508505
self.attribute = attribute
509506
self.entity_description = description
510-
self._attr_name = get_block_entity_name(
511-
coordinator.device, None, description.name
512-
)
507+
513508
self._attr_unique_id = f"{coordinator.mac}-{attribute}"
514509
self._attr_device_info = get_entity_block_device_info(coordinator)
515510
self._last_value = None
@@ -546,13 +541,13 @@ def __init__(
546541
self.attribute = attribute
547542
self.entity_description = description
548543

544+
if description.role == ROLE_GENERIC:
545+
self._attr_name = get_rpc_channel_name(coordinator.device, key)
546+
549547
self._attr_unique_id = f"{super().unique_id}-{attribute}"
550-
self._attr_name = get_rpc_entity_name(
551-
coordinator.device, key, description.name, description.role
552-
)
553548
self._last_value = None
554-
id_key = key.split(":")[-1]
555-
self._id = int(id_key) if id_key.isnumeric() else None
549+
has_id, _, component_id = get_rpc_key(key)
550+
self._id = int(component_id) if has_id and component_id.isnumeric() else None
556551

557552
if description.unit is not None:
558553
self._attr_native_unit_of_measurement = description.unit(
@@ -626,9 +621,6 @@ def __init__(
626621
self._attr_unique_id = (
627622
f"{self.coordinator.mac}-{block.description}-{attribute}"
628623
)
629-
self._attr_name = get_block_entity_name(
630-
coordinator.device, block, description.name
631-
)
632624
elif entry is not None:
633625
self._attr_unique_id = entry.unique_id
634626

@@ -689,11 +681,7 @@ def __init__(
689681
self._attr_unique_id = f"{coordinator.mac}-{key}-{attribute}"
690682
self._last_value = None
691683

692-
if coordinator.device.initialized:
693-
self._attr_name = get_rpc_entity_name(
694-
coordinator.device, key, description.name
695-
)
696-
elif entry is not None:
684+
if not coordinator.device.initialized and entry is not None:
697685
self._attr_name = cast(str, entry.original_name)
698686

699687
async def async_update(self) -> None:

homeassistant/components/shelly/utils.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
DeviceInfo,
5050
)
5151
from homeassistant.helpers.network import NoURLAvailableError, get_url
52-
from homeassistant.helpers.typing import UNDEFINED, UndefinedType
5352
from homeassistant.util.dt import utcnow
5453

5554
from .const import (
@@ -117,20 +116,6 @@ def get_block_number_of_channels(device: BlockDevice, block: Block) -> int:
117116
return channels or 1
118117

119118

120-
def get_block_entity_name(
121-
device: BlockDevice,
122-
block: Block | None,
123-
name: str | UndefinedType | None = None,
124-
) -> str | None:
125-
"""Naming for block based switch and sensors."""
126-
channel_name = get_block_channel_name(device, block)
127-
128-
if name is not UNDEFINED and name:
129-
return f"{channel_name} {name.lower()}" if channel_name else name
130-
131-
return channel_name
132-
133-
134119
def get_block_custom_name(device: BlockDevice, block: Block | None) -> str | None:
135120
"""Get custom name from device settings."""
136121
if block and (key := cast(str, block.type) + "s") and key in device.settings:
@@ -474,23 +459,6 @@ def get_rpc_sub_device_name(
474459
return f"{device.name} {component.title()} {component_id}"
475460

476461

477-
def get_rpc_entity_name(
478-
device: RpcDevice,
479-
key: str,
480-
name: str | UndefinedType | None = None,
481-
role: str | None = None,
482-
) -> str | None:
483-
"""Naming for RPC based switch and sensors."""
484-
channel_name = get_rpc_channel_name(device, key)
485-
486-
if name is not UNDEFINED and name:
487-
if role and role != ROLE_GENERIC:
488-
return name
489-
return f"{channel_name} {name.lower()}" if channel_name else name
490-
491-
return channel_name
492-
493-
494462
def get_entity_translation_attributes(
495463
channel_name: str | None,
496464
translation_key: str | None,
@@ -826,11 +794,9 @@ async def get_rpc_scripts_event_types(
826794
device: RpcDevice, ignore_scripts: list[str]
827795
) -> dict[int, list[str]]:
828796
"""Return a dict of all scripts and their event types."""
829-
script_instances = get_rpc_key_instances(device.status, "script")
830797
script_events = {}
831-
for script in script_instances:
832-
script_name = get_rpc_entity_name(device, script)
833-
if script_name in ignore_scripts:
798+
for script in get_rpc_key_instances(device.status, "script"):
799+
if get_rpc_channel_name(device, script) in ignore_scripts:
834800
continue
835801

836802
script_id = get_rpc_key_id(script)

0 commit comments

Comments
 (0)