Skip to content

Commit 326f7f0

Browse files
authored
Add coverage to Shelly utils (home-assistant#157455)
1 parent 11afda8 commit 326f7f0

File tree

3 files changed

+6
-67
lines changed

3 files changed

+6
-67
lines changed

homeassistant/components/shelly/event.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
async_remove_orphaned_entities,
3232
async_remove_shelly_entity,
3333
get_block_channel,
34-
get_block_channel_name,
3534
get_block_custom_name,
3635
get_block_number_of_channels,
3736
get_device_entry_gen,
@@ -211,7 +210,7 @@ def __init__(
211210
else ""
212211
}
213212
else:
214-
self._attr_name = get_block_channel_name(coordinator.device, block)
213+
self._attr_name = get_block_custom_name(coordinator.device, block)
215214

216215
async def async_added_to_hass(self) -> None:
217216
"""When entity is added to hass."""

homeassistant/components/shelly/utils.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ def get_block_number_of_channels(device: BlockDevice, block: Block) -> int:
110110
channels = device.shelly.get("num_emeters")
111111
elif block.type in ["relay", "light"]:
112112
channels = device.shelly.get("num_outputs")
113-
elif block.type in ["roller", "device"]:
114-
channels = 1
115113

116114
return channels or 1
117115

@@ -134,21 +132,6 @@ def get_block_channel(block: Block | None, base: str = "1") -> str:
134132
return chr(int(block.channel) + ord(base))
135133

136134

137-
def get_block_channel_name(device: BlockDevice, block: Block | None) -> str | None:
138-
"""Get name based on device and channel name."""
139-
if (
140-
not block
141-
or block.type in ("device", "light", "relay", "emeter")
142-
or get_block_number_of_channels(device, block) == 1
143-
):
144-
return None
145-
146-
if custom_name := get_block_custom_name(device, block):
147-
return custom_name
148-
149-
return f"Channel {get_block_channel(block)}"
150-
151-
152135
def get_block_sub_device_name(device: BlockDevice, block: Block) -> str:
153136
"""Get name of block sub-device."""
154137
if TYPE_CHECKING:
@@ -664,10 +647,7 @@ def async_remove_shelly_rpc_entities(
664647

665648
def get_virtual_component_ids(config: dict[str, Any], platform: str) -> list[str]:
666649
"""Return a list of virtual component IDs for a platform."""
667-
component = VIRTUAL_COMPONENTS_MAP.get(platform)
668-
669-
if not component:
670-
return []
650+
component = VIRTUAL_COMPONENTS_MAP[platform]
671651

672652
ids: list[str] = []
673653

@@ -975,10 +955,10 @@ def async_migrate_rpc_virtual_components_unique_ids(
975955
The new unique_id format is: {mac}-{key}-{component}_{role}
976956
"""
977957
for component in VIRTUAL_COMPONENTS:
978-
if entity_entry.unique_id.endswith(f"-{component!s}"):
979-
key = entity_entry.unique_id.split("-")[-2]
980-
if key not in config:
981-
continue
958+
if (
959+
entity_entry.unique_id.endswith(f"-{component!s}")
960+
and (key := entity_entry.unique_id.split("-")[-2]) in config
961+
):
982962
role = get_rpc_role_by_key(config, key)
983963
new_unique_id = f"{entity_entry.unique_id}_{role}"
984964
LOGGER.debug(

tests/components/shelly/test_utils.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
MODEL_BUTTON1,
1010
MODEL_BUTTON1_V2,
1111
MODEL_DIMMER_2,
12-
MODEL_EM3,
1312
MODEL_I3,
1413
MODEL_MOTION,
1514
MODEL_PLUS_2PM_V2,
@@ -24,7 +23,6 @@
2423
UPTIME_DEVIATION,
2524
)
2625
from homeassistant.components.shelly.utils import (
27-
get_block_channel_name,
2826
get_block_device_sleep_period,
2927
get_block_input_triggers,
3028
get_block_number_of_channels,
@@ -76,44 +74,6 @@ async def test_block_get_block_number_of_channels(
7674
)
7775

7876

79-
async def test_block_get_block_channel_name(
80-
mock_block_device: Mock, monkeypatch: pytest.MonkeyPatch
81-
) -> None:
82-
"""Test block get block channel name."""
83-
result = get_block_channel_name(
84-
mock_block_device,
85-
mock_block_device.blocks[DEVICE_BLOCK_ID],
86-
)
87-
# when has_entity_name is True the result should be None
88-
assert result is None
89-
90-
monkeypatch.setattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "type", "relay")
91-
result = get_block_channel_name(
92-
mock_block_device,
93-
mock_block_device.blocks[DEVICE_BLOCK_ID],
94-
)
95-
# when has_entity_name is True the result should be None
96-
assert result is None
97-
98-
monkeypatch.setitem(mock_block_device.settings["device"], "type", MODEL_EM3)
99-
result = get_block_channel_name(
100-
mock_block_device,
101-
mock_block_device.blocks[DEVICE_BLOCK_ID],
102-
)
103-
# when has_entity_name is True the result should be None
104-
assert result is None
105-
106-
monkeypatch.setitem(
107-
mock_block_device.settings, "relays", [{"name": "test-channel"}]
108-
)
109-
result = get_block_channel_name(
110-
mock_block_device,
111-
mock_block_device.blocks[DEVICE_BLOCK_ID],
112-
)
113-
# when has_entity_name is True the result should be None
114-
assert result is None
115-
116-
11777
async def test_is_block_momentary_input(
11878
mock_block_device: Mock, monkeypatch: pytest.MonkeyPatch
11979
) -> None:

0 commit comments

Comments
 (0)