Repository: home-assistant/core
File: tests/components/tuya/test_event.py (Python)

Code before:

    mock_device.status[dpcode] = value

    await mock_listener.async_send_device_update(hass, mock_device, mock_device.status)

    # Verify event was triggered with correct type and decoded URL
    state = hass.states.get(entity_id)
    assert state.attributes == snapshot
    assert state.attributes["message"]


@pytest.mark.parametrize(
    "mock_device_code",
    ["wxkg_l8yaz4um5b3pwyvf"],
)
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.EVENT])
@pytest.mark.freeze_time("2024-01-01")
async def test_selective_state_update(
    hass: HomeAssistant,
    mock_manager: Manager,
    mock_config_entry: MockConfigEntry,
    mock_device: CustomerDevice,
    mock_listener: MockDeviceListener,
    freezer: FrozenDateTimeFactory,
) -> None:
    """Test skip_update/last_reported."""
    entity_id = "event.bathroom_smart_switch_button_1"
    await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)

    # Initial state is unknown
    assert hass.states.get(entity_id).state == STATE_UNKNOWN

    # Device receives a data update - event gets triggered and state gets updated
    freezer.tick(10)
    await mock_listener.async_send_device_update(
        hass, mock_device, {"switch_mode1": "click"}
    )
    assert hass.states.get(entity_id).state == "2024-01-01T00:00:10.000+00:00"

    # Device goes offline
    freezer.tick(10)
    mock_device.online = False
    await mock_listener.async_send_device_update(hass, mock_device, None)
    assert hass.states.get(entity_id).state == STATE_UNAVAILABLE

    # Device comes back online - state should go back to last known value,
    # not new datetime since no new data update has come in
    freezer.tick(10)
    mock_device.online = True
    await mock_listener.async_send_device_update(hass, mock_device, None)
    assert hass.states.get(entity_id).state == "2024-01-01T00:00:10.000+00:00"

Review comment (suggestion):
```suggestion
    """Ensure event is only triggered when device reports actual data."""
```

Code after:

    mock_device.status[dpcode] = value

    await mock_listener.async_send_device_update(hass, mock_device, mock_device.status)

    # Verify event was triggered with correct type and decoded URL
    state = hass.states.get(entity_id)
    assert state.attributes == snapshot
    assert state.attributes["message"]


@pytest.mark.parametrize(
    "mock_device_code",
    ["wxkg_l8yaz4um5b3pwyvf"],
)
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.EVENT])
@pytest.mark.freeze_time("2024-01-01")
async def test_selective_state_update(
    hass: HomeAssistant,
    mock_manager: Manager,
    mock_config_entry: MockConfigEntry,
    mock_device: CustomerDevice,
    mock_listener: MockDeviceListener,
    freezer: FrozenDateTimeFactory,
) -> None:
    """Ensure event is only triggered when device reports actual data."""
    entity_id = "event.bathroom_smart_switch_button_1"
    await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)

    # Initial state is unknown
    assert hass.states.get(entity_id).state == STATE_UNKNOWN

    # Device receives a data update - event gets triggered and state gets updated
    freezer.tick(10)
    await mock_listener.async_send_device_update(
        hass, mock_device, {"switch_mode1": "click"}
    )
    assert hass.states.get(entity_id).state == "2024-01-01T00:00:10.000+00:00"

    # Device goes offline
    freezer.tick(10)
    mock_device.online = False
    await mock_listener.async_send_device_update(hass, mock_device, None)
    assert hass.states.get(entity_id).state == STATE_UNAVAILABLE

    # Device comes back online - state should go back to last known value,
    # not new datetime since no new data update has come in
    freezer.tick(10)
    mock_device.online = True
    await mock_listener.async_send_device_update(hass, mock_device, None)
    assert hass.states.get(entity_id).state == "2024-01-01T00:00:10.000+00:00"