mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Add coverage to Shelly utils (#157455)
This commit is contained in:
@@ -31,7 +31,6 @@ from .utils import (
|
||||
async_remove_orphaned_entities,
|
||||
async_remove_shelly_entity,
|
||||
get_block_channel,
|
||||
get_block_channel_name,
|
||||
get_block_custom_name,
|
||||
get_block_number_of_channels,
|
||||
get_device_entry_gen,
|
||||
@@ -211,7 +210,7 @@ class ShellyBlockEvent(ShellyBlockEntity, EventEntity):
|
||||
else ""
|
||||
}
|
||||
else:
|
||||
self._attr_name = get_block_channel_name(coordinator.device, block)
|
||||
self._attr_name = get_block_custom_name(coordinator.device, block)
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""When entity is added to hass."""
|
||||
|
||||
@@ -110,8 +110,6 @@ def get_block_number_of_channels(device: BlockDevice, block: Block) -> int:
|
||||
channels = device.shelly.get("num_emeters")
|
||||
elif block.type in ["relay", "light"]:
|
||||
channels = device.shelly.get("num_outputs")
|
||||
elif block.type in ["roller", "device"]:
|
||||
channels = 1
|
||||
|
||||
return channels or 1
|
||||
|
||||
@@ -134,21 +132,6 @@ def get_block_channel(block: Block | None, base: str = "1") -> str:
|
||||
return chr(int(block.channel) + ord(base))
|
||||
|
||||
|
||||
def get_block_channel_name(device: BlockDevice, block: Block | None) -> str | None:
|
||||
"""Get name based on device and channel name."""
|
||||
if (
|
||||
not block
|
||||
or block.type in ("device", "light", "relay", "emeter")
|
||||
or get_block_number_of_channels(device, block) == 1
|
||||
):
|
||||
return None
|
||||
|
||||
if custom_name := get_block_custom_name(device, block):
|
||||
return custom_name
|
||||
|
||||
return f"Channel {get_block_channel(block)}"
|
||||
|
||||
|
||||
def get_block_sub_device_name(device: BlockDevice, block: Block) -> str:
|
||||
"""Get name of block sub-device."""
|
||||
if TYPE_CHECKING:
|
||||
@@ -664,10 +647,7 @@ def async_remove_shelly_rpc_entities(
|
||||
|
||||
def get_virtual_component_ids(config: dict[str, Any], platform: str) -> list[str]:
|
||||
"""Return a list of virtual component IDs for a platform."""
|
||||
component = VIRTUAL_COMPONENTS_MAP.get(platform)
|
||||
|
||||
if not component:
|
||||
return []
|
||||
component = VIRTUAL_COMPONENTS_MAP[platform]
|
||||
|
||||
ids: list[str] = []
|
||||
|
||||
@@ -975,10 +955,10 @@ def async_migrate_rpc_virtual_components_unique_ids(
|
||||
The new unique_id format is: {mac}-{key}-{component}_{role}
|
||||
"""
|
||||
for component in VIRTUAL_COMPONENTS:
|
||||
if entity_entry.unique_id.endswith(f"-{component!s}"):
|
||||
key = entity_entry.unique_id.split("-")[-2]
|
||||
if key not in config:
|
||||
continue
|
||||
if (
|
||||
entity_entry.unique_id.endswith(f"-{component!s}")
|
||||
and (key := entity_entry.unique_id.split("-")[-2]) in config
|
||||
):
|
||||
role = get_rpc_role_by_key(config, key)
|
||||
new_unique_id = f"{entity_entry.unique_id}_{role}"
|
||||
LOGGER.debug(
|
||||
|
||||
@@ -9,7 +9,6 @@ from aioshelly.const import (
|
||||
MODEL_BUTTON1,
|
||||
MODEL_BUTTON1_V2,
|
||||
MODEL_DIMMER_2,
|
||||
MODEL_EM3,
|
||||
MODEL_I3,
|
||||
MODEL_MOTION,
|
||||
MODEL_PLUS_2PM_V2,
|
||||
@@ -24,7 +23,6 @@ from homeassistant.components.shelly.const import (
|
||||
UPTIME_DEVIATION,
|
||||
)
|
||||
from homeassistant.components.shelly.utils import (
|
||||
get_block_channel_name,
|
||||
get_block_device_sleep_period,
|
||||
get_block_input_triggers,
|
||||
get_block_number_of_channels,
|
||||
@@ -76,44 +74,6 @@ async def test_block_get_block_number_of_channels(
|
||||
)
|
||||
|
||||
|
||||
async def test_block_get_block_channel_name(
|
||||
mock_block_device: Mock, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""Test block get block channel name."""
|
||||
result = get_block_channel_name(
|
||||
mock_block_device,
|
||||
mock_block_device.blocks[DEVICE_BLOCK_ID],
|
||||
)
|
||||
# when has_entity_name is True the result should be None
|
||||
assert result is None
|
||||
|
||||
monkeypatch.setattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "type", "relay")
|
||||
result = get_block_channel_name(
|
||||
mock_block_device,
|
||||
mock_block_device.blocks[DEVICE_BLOCK_ID],
|
||||
)
|
||||
# when has_entity_name is True the result should be None
|
||||
assert result is None
|
||||
|
||||
monkeypatch.setitem(mock_block_device.settings["device"], "type", MODEL_EM3)
|
||||
result = get_block_channel_name(
|
||||
mock_block_device,
|
||||
mock_block_device.blocks[DEVICE_BLOCK_ID],
|
||||
)
|
||||
# when has_entity_name is True the result should be None
|
||||
assert result is None
|
||||
|
||||
monkeypatch.setitem(
|
||||
mock_block_device.settings, "relays", [{"name": "test-channel"}]
|
||||
)
|
||||
result = get_block_channel_name(
|
||||
mock_block_device,
|
||||
mock_block_device.blocks[DEVICE_BLOCK_ID],
|
||||
)
|
||||
# when has_entity_name is True the result should be None
|
||||
assert result is None
|
||||
|
||||
|
||||
async def test_is_block_momentary_input(
|
||||
mock_block_device: Mock, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user