Repository: home-assistant/core
File: tests/components/pooldose/test_number.py (Python)

Code before:

    # Verify state updated immediately (optimistic update)
    ph_target_state = hass.states.get("number.pool_device_ph_target")
    assert ph_target_state.state == "7.0"


@pytest.mark.usefixtures("init_integration")
async def test_actions_cannot_connect_number(
    hass: HomeAssistant,
    mock_pooldose_client: AsyncMock,
    init_integration: MockConfigEntry,
    snapshot: SnapshotAssertion,
) -> None:
    """When the client write method raises, ServiceValidationError('cannot_connect') is raised."""
    client = mock_pooldose_client
    entity_id = "number.pool_device_ph_target"
    before = hass.states.get(entity_id)
    assert before is not None

    err = ServiceValidationError(
        translation_domain="pooldose",
        translation_key="cannot_connect",
        translation_placeholders={"error": "mocked error"},
    )

    client.set_number = AsyncMock(side_effect=err)

    with pytest.raises(ServiceValidationError) as excinfo:
        await hass.services.async_call(
            "number", "set_value", {"entity_id": entity_id, "value": 7.0}, blocking=True
        )

    assert excinfo.value.translation_key == "cannot_connect"

    after = hass.states.get(entity_id)
    assert after is not None
    assert before.state == after.state


@pytest.mark.usefixtures("init_integration")
async def test_actions_write_rejected_number(
    hass: HomeAssistant,
    mock_pooldose_client: AsyncMock,
    init_integration: MockConfigEntry,
    snapshot: SnapshotAssertion,
) -> None:
    """When the client write method returns False, ServiceValidationError('write_rejected') is raised."""
    client = mock_pooldose_client
    entity_id = "number.pool_device_ph_target"
    before = hass.states.get(entity_id)
    assert before is not None

Review comment (bug):
This is incorrect. the `client.set_number` doesn't raise `ServiceValidationError`, it just returns a falsey state and the new exception handler does the rest. So instead we should mimick what the library would've done

Code after:

    # Verify state updated immediately (optimistic update)
    ph_target_state = hass.states.get("number.pool_device_ph_target")
    assert ph_target_state.state == "7.0"


@pytest.mark.usefixtures("init_integration")
async def test_actions_cannot_connect_number(
    hass: HomeAssistant,
    mock_pooldose_client: AsyncMock,
    init_integration: MockConfigEntry,
    snapshot: SnapshotAssertion,
) -> None:
    """When the client write method raises, ServiceValidationError('cannot_connect') is raised."""
    client = mock_pooldose_client
    entity_id = "number.pool_device_ph_target"
    before = hass.states.get(entity_id)
    assert before is not None

    client.is_connected = False
    client.set_number = AsyncMock(return_value=False)

    with pytest.raises(ServiceValidationError) as excinfo:
        await hass.services.async_call(
            "number", "set_value", {"entity_id": entity_id, "value": 7.0}, blocking=True
        )

    assert excinfo.value.translation_key == "cannot_connect"

    after = hass.states.get(entity_id)
    assert after is not None
    assert before.state == after.state


@pytest.mark.usefixtures("init_integration")
async def test_actions_write_rejected_number(
    hass: HomeAssistant,
    mock_pooldose_client: AsyncMock,
    init_integration: MockConfigEntry,
    snapshot: SnapshotAssertion,
) -> None:
    """When the client write method returns False, ServiceValidationError('write_rejected') is raised."""
    client = mock_pooldose_client
    entity_id = "number.pool_device_ph_target"
    before = hass.states.get(entity_id)
    assert before is not None

    client.set_number = AsyncMock(return_value=False)

    with pytest.raises(ServiceValidationError) as excinfo:
        await hass.services.async_call(