Repository: home-assistant/core
File: tests/components/matter/test_adapter.py (Python)

Code before:
    await hass.async_block_till_done()

    entity_state = hass.states.get("light.mock_onoff_light")
    assert entity_state


@pytest.mark.usefixtures("matter_node")
@pytest.mark.parametrize("node_fixture", ["mock_air_purifier"])
async def test_device_registry_single_node_composed_device(
    hass: HomeAssistant,
    device_registry: dr.DeviceRegistry,
) -> None:
    """Test that a composed device within a standalone node only creates one HA device entry."""
    assert len(device_registry.devices) == 1


@pytest.mark.usefixtures("matter_node")
@pytest.mark.parametrize("node_fixture", ["inovelli_vtm31"])
async def test_multi_endpoint_name(hass: HomeAssistant) -> None:
    """Test that the entity name gets postfixed if the device has multiple primary endpoints."""
    entity_state = hass.states.get("light.inovelli_light_1")
    assert entity_state
    assert entity_state.name == "Inovelli Light (1)"
    entity_state = hass.states.get("light.inovelli_light_6")
    assert entity_state
    assert entity_state.name == "Inovelli Light (6)"


async def test_get_clean_name() -> None:
    """Test get_clean_name helper.

    Test device names that are assigned to `null`
    or have a trailing null char with spaces.
    """
    assert get_clean_name(None) is None
    assert get_clean_name("\x00") is None
    assert get_clean_name("   \x00") is None
    assert get_clean_name("") is None
    assert get_clean_name("Mock device") == "Mock device"
    assert get_clean_name("Mock device                    \x00") == "Mock device"


async def test_bad_node_not_crash_integration(
    hass: HomeAssistant,
    matter_client: MagicMock,
    caplog: pytest.LogCaptureFixture,
) -> None:
    """Test that a bad node does not crash the integration."""
    good_node = create_node_from_fixture("mock_onoff_light")
    bad_node = create_node_from_fixture("mock_onoff_light")
    del bad_node.endpoints[0].node

Review comment (suggestion):
Note: This test was **moved** into `test_entity.py` as `test_multi_endpoint_entity_translation_key`.

I think it makes more sense to have all the Matter entity behavior in a dedicated `test_entity` test file, instead of having it in `test_adapter`.

Code after:
    await hass.async_block_till_done()

    entity_state = hass.states.get("light.mock_onoff_light")
    assert entity_state


@pytest.mark.usefixtures("matter_node")
@pytest.mark.parametrize("node_fixture", ["mock_air_purifier"])
async def test_device_registry_single_node_composed_device(
    hass: HomeAssistant,
    device_registry: dr.DeviceRegistry,
) -> None:
    """Test that a composed device within a standalone node only creates one HA device entry."""
    assert len(device_registry.devices) == 1


async def test_get_clean_name() -> None:
    """Test get_clean_name helper.

    Test device names that are assigned to `null`
    or have a trailing null char with spaces.
    """
    assert get_clean_name(None) is None
    assert get_clean_name("\x00") is None
    assert get_clean_name("   \x00") is None
    assert get_clean_name("") is None
    assert get_clean_name("Mock device") == "Mock device"
    assert get_clean_name("Mock device                    \x00") == "Mock device"


async def test_bad_node_not_crash_integration(
    hass: HomeAssistant,
    matter_client: MagicMock,
    caplog: pytest.LogCaptureFixture,
) -> None:
    """Test that a bad node does not crash the integration."""
    good_node = create_node_from_fixture("mock_onoff_light")
    bad_node = create_node_from_fixture("mock_onoff_light")
    del bad_node.endpoints[0].node
    matter_client.get_nodes.return_value = [good_node, bad_node]
    config_entry = MockConfigEntry(
        domain="matter", data={"url": "http://mock-matter-server-url"}
    )
    config_entry.add_to_hass(hass)

    assert await hass.config_entries.async_setup(config_entry.entry_id)
    await hass.async_block_till_done()

    assert matter_client.get_nodes.call_count == 1
    assert hass.states.get("light.mock_onoff_light") is not None
    assert len(hass.states.async_all("light")) == 1