Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/zha/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def name(self) -> str | UndefinedType | None:
original_name = super().name

if original_name not in (UNDEFINED, None) or meta.fallback_name is None:
if meta.postfix is not None:
return f"{original_name} ({meta.postfix})"
return original_name

# This is to allow local development and to register niche devices, since
Expand Down
2 changes: 2 additions & 0 deletions tests/components/zha/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
'migrate_unique_ids': list([
]),
'platform': 'alarm_control_panel',
'postfix': None,
'primary': False,
'state_class': None,
'supported_features': 15,
Expand Down Expand Up @@ -318,6 +319,7 @@
'migrate_unique_ids': list([
]),
'platform': 'binary_sensor',
'postfix': None,
'primary': True,
'state_class': None,
'translation_key': 'ias_zone',
Expand Down
89 changes: 88 additions & 1 deletion tests/components/zha/test_entity.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
"""Test ZHA entities."""

from collections.abc import Callable, Coroutine
from unittest.mock import patch

import pytest
from zigpy.device import Device
from zigpy.profiles import zha
from zigpy.zcl.clusters import general
import zigpy.types as t
from zigpy.zcl.clusters import general, measurement

from homeassistant.components.zha.helpers import get_zha_gateway
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr

from .conftest import SIG_EP_INPUT, SIG_EP_OUTPUT, SIG_EP_PROFILE, SIG_EP_TYPE

ENTITY_ID_NO_PREFIX = "{}.fakemanufacturer_fakemodel"
ENTITY_ID_PREFIX_NUM = "{}.fakemanufacturer_fakemodel_{}_{}"


@pytest.fixture(autouse=True)
def sensor_and_switch_platform_only():
"""Only set up the switch and sensor platforms to speed up tests."""
with patch(
"homeassistant.components.zha.PLATFORMS",
(
Platform.SENSOR,
Platform.SWITCH,
),
):
yield


async def test_device_registry_via_device(
hass: HomeAssistant,
Expand Down Expand Up @@ -48,3 +68,70 @@ async def test_device_registry_via_device(
)

assert reg_device.via_device_id == reg_coordinator_device.id


@pytest.mark.parametrize("n_endpoints", [1, 2])
@pytest.mark.parametrize(
(
"cluster_id",
"entity_prefix",
"entity_suffix",
),
[
(
measurement.TemperatureMeasurement.cluster_id,
"sensor",
"temperature",
),
(
measurement.PressureMeasurement.cluster_id,
"sensor",
"pressure",
),
(
general.OnOff.cluster_id,
"switch",
"switch",
),
],
)
async def test_entity_postfix(
hass: HomeAssistant,
setup_zha: Callable[..., Coroutine[None]],
zigpy_device_mock: Callable[..., Device],
cluster_id: t.uint16_t,
entity_prefix: str,
entity_suffix: str,
n_endpoints: int,
) -> None:
"""Test postfix being present in entity name."""

await setup_zha()
gateway = get_zha_gateway(hass)

endpoint_definition = {
SIG_EP_INPUT: [cluster_id, general.Basic.cluster_id],
SIG_EP_OUTPUT: [],
SIG_EP_TYPE: zha.DeviceType.ON_OFF_SWITCH,
}

# Create a device with n_endpoints identical endpoints
zigpy_device = zigpy_device_mock(
dict.fromkeys(range(1, n_endpoints + 1), endpoint_definition),
)

gateway.get_or_create_device(zigpy_device)
await gateway.async_device_initialized(zigpy_device)
await hass.async_block_till_done(wait_background_tasks=True)

if n_endpoints > 1:
for n in range(1, n_endpoints + 1):
state = hass.states.get(
ENTITY_ID_PREFIX_NUM.format(entity_prefix, entity_suffix, n)
)
assert state is not None
assert state.name.endswith(f" ({n})")
else:
state = hass.states.get(ENTITY_ID_NO_PREFIX.format(entity_prefix))
assert state is not None
assert not state.name.endswith(")")