Repository: Thomas55555/aioautomower (language: python)

Issue:
Add support for StartInWorkArea
I have Husqvarna 410XE NERA which has multiple work areas configured. From the Automower app I have to choose the work area it will operate in. In the API the action is StartInWorkArea


Discussion / hints:

Fix patch:
diff --git a/example.py b/example.py
index dd682ed6..9fe36544 100644
--- a/example.py
+++ b/example.py
@@ -93,6 +93,9 @@ async def main() -> None:
         await asyncio.sleep(5)
         # await automower_api.commands.pause_mowing(_mower_id)
         # Uncomment the line above to let all your mowers pause.
+        await asyncio.sleep(5)
+        # await automower_api.commands.start_in_workarea(_mower_id, 0, 30)
+        # Uncomment the line above to start your mower in workarea 0 for 30min.
     await asyncio.sleep(3000)
     # The close() will stop the websocket and the token refresh tasks
     await automower_api.close()
diff --git a/src/aioautomower/session.py b/src/aioautomower/session.py
index 3e3cdf5b..b66008d9 100644
--- a/src/aioautomower/session.py
+++ b/src/aioautomower/session.py
@@ -105,6 +105,25 @@ async def park_for(self, mower_id: str, duration_in_min: int):
         url = AutomowerEndpoint.actions.format(mower_id=mower_id)
         await self.auth.post_json(url, json=body)
 
+    async def start_in_workarea(
+        self,
+        mower_id: str,
+        work_area_id: int,
+        duration_in_min: int | None = None,
+    ):
+        """Start the mower in a work area for a period of minutes.
+
+        If duration is skipped the mower will continue forever.
+        """
+        body = {
+            "data": {
+                "type": "StartInWorkArea",
+                "attributes": {"duration": duration_in_min, "workAreaId": work_area_id},
+            }
+        }
+        url = AutomowerEndpoint.actions.format(mower_id=mower_id)
+        await self.auth.post_json(url, json=body)
+
     async def start_for(self, mower_id: str, duration_in_min: int):
         """Start the mower for a period of minutes."""
         body = {


Test patch:
diff --git a/tests/test_session.py b/tests/test_session.py
index ab0463b9..d18ee09e 100644
--- a/tests/test_session.py
+++ b/tests/test_session.py
@@ -67,8 +67,30 @@ async def test_post_commands(mock_automower_client: AbstractAuth):
             }
         },
     )
-    await automower_api.commands.start_for(MOWER_ID, 30)
+    await automower_api.commands.start_in_workarea(MOWER_ID, 30)
     assert mocked_method.call_count == 6
+    mocked_method.assert_called_with(
+        f"mowers/{MOWER_ID}/actions",
+        json={
+            "data": {
+                "type": "StartInWorkArea",
+                "attributes": {"duration": None, "workAreaId": 30},
+            }
+        },
+    )
+    await automower_api.commands.start_in_workarea(MOWER_ID, 30, 2)
+    assert mocked_method.call_count == 7
+    mocked_method.assert_called_with(
+        f"mowers/{MOWER_ID}/actions",
+        json={
+            "data": {
+                "type": "StartInWorkArea",
+                "attributes": {"duration": 2, "workAreaId": 30},
+            }
+        },
+    )
+    await automower_api.commands.start_for(MOWER_ID, 30)
+    assert mocked_method.call_count == 8
     mocked_method.assert_called_with(
         f"mowers/{MOWER_ID}/actions",
         json={
@@ -79,13 +101,13 @@ async def test_post_commands(mock_automower_client: AbstractAuth):
         },
     )
     await automower_api.commands.set_cutting_height(MOWER_ID, 9)
-    assert mocked_method.call_count == 7
+    assert mocked_method.call_count == 9
     mocked_method.assert_called_with(
         f"mowers/{MOWER_ID}/settings",
         json={"data": {"type": "settings", "attributes": {"cuttingHeight": 9}}},
     )
     await automower_api.commands.set_headlight_mode(MOWER_ID, HeadlightModes.ALWAYS_OFF)
-    assert mocked_method.call_count == 8
+    assert mocked_method.call_count == 10
     mocked_method.assert_called_with(
         f"mowers/{MOWER_ID}/settings",
         json={
@@ -97,13 +119,13 @@ async def test_post_commands(mock_automower_client: AbstractAuth):
     )
     task_list = json.loads(load_fixture("task_list.json"))
     await automower_api.commands.set_calendar(MOWER_ID, task_list)
-    assert mocked_method.call_count == 9
+    assert mocked_method.call_count == 11
     mocked_method.assert_called_with(
         f"mowers/{MOWER_ID}/calendar",
         json={"data": {"type": "calendar", "attributes": {"tasks": task_list}}},
     )
     await automower_api.commands.error_confirm(MOWER_ID)
-    assert mocked_method.call_count == 10
+    assert mocked_method.call_count == 12
     mocked_method.assert_called_with(f"mowers/{MOWER_ID}/errors/confirm", json={})
     mocked_method.reset_mock()
     await automower_api.close()