Repository: dapr/python-sdk

Issue:
[BUG] Actor state store error on `try_add_state` 
## Expected Behavior

The actor `try_add_state` should save a newly added key to the store.


## Actual Behavior

The actor `try_add_state` method returns the error `The actor state name {state_name} already exist.` for keys that don't exist.


## Steps to Reproduce the Problem

Try to call the `try_add_state` method on an actor with a key that doesn't exist.

## Release Note
RELEASE NOTE: **FIX** Fixed not being able to save new keys with `try_add_state`

Discussion:

Solution patch:
diff --git a/dapr/actor/runtime/state_manager.py b/dapr/actor/runtime/state_manager.py
index 35cc33fb8..dc7ec033f 100644
--- a/dapr/actor/runtime/state_manager.py
+++ b/dapr/actor/runtime/state_manager.py
@@ -85,7 +85,7 @@ async def try_add_state(self, state_name: str, value: T) -> bool:
         existed = await self._actor.runtime_ctx.state_provider.contains_state(
             self._type_name, self._actor.id.id, state_name
         )
-        if not existed:
+        if existed:
             return False
 
         state_change_tracker[state_name] = StateMetadata(value, StateChangeKind.add)


Test patch:
diff --git a/tests/actor/test_state_manager.py b/tests/actor/test_state_manager.py
index 98db0228e..c9406dbd2 100644
--- a/tests/actor/test_state_manager.py
+++ b/tests/actor/test_state_manager.py
@@ -46,7 +46,7 @@ def setUp(self):
 
     @mock.patch(
         'tests.actor.fake_client.FakeDaprActorClient.get_state',
-        new=_async_mock(return_value=base64.b64encode(b'"value1"')),
+        new=_async_mock(),
     )
     @mock.patch(
         'tests.actor.fake_client.FakeDaprActorClient.save_state_transactionally', new=_async_mock()
@@ -67,6 +67,20 @@ def test_add_state(self):
         added = _run(state_manager.try_add_state('state1', 'value1'))
         self.assertFalse(added)
 
+    @mock.patch(
+        'tests.actor.fake_client.FakeDaprActorClient.get_state',
+        new=_async_mock(return_value=base64.b64encode(b'"value1"')),
+    )
+    @mock.patch(
+        'tests.actor.fake_client.FakeDaprActorClient.save_state_transactionally', new=_async_mock()
+    )
+    def test_add_state_with_existing_state(self):
+        state_manager = ActorStateManager(self._fake_actor)
+
+        # Add first 'state1'
+        added = _run(state_manager.try_add_state('state1', 'value1'))
+        self.assertFalse(added)
+
     @mock.patch('tests.actor.fake_client.FakeDaprActorClient.get_state', new=_async_mock())
     def test_get_state_for_no_state(self):
         state_manager = ActorStateManager(self._fake_actor)