Skip to content

Commit 7e041d1

Browse files
committed
Rename Scenario to ActionGroup (and relevant methods), reuse ActionGroup for Execution typing (#1864)
* Rename scenario to action group * Rename test * Fix docstring formatting in ActionGroup class * Remove unnecessary assertions in ActionGroup initialization
1 parent ef41dc7 commit 7e041d1

4 files changed

Lines changed: 65 additions & 22 deletions

File tree

pyoverkiz/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
UnknownUserException,
7676
)
7777
from pyoverkiz.models import (
78+
ActionGroup,
7879
Command,
7980
Device,
8081
Event,
@@ -86,7 +87,6 @@
8687
OptionParameter,
8788
OverkizServer,
8889
Place,
89-
Scenario,
9090
Setup,
9191
State,
9292
)
@@ -694,10 +694,12 @@ async def execute_commands(
694694
return cast(str, response["execId"])
695695

696696
@retry_on_auth_error
697-
async def get_scenarios(self) -> list[Scenario]:
698-
"""List the scenarios."""
697+
async def get_action_groups(self) -> list[ActionGroup]:
698+
"""List the action groups (scenarios)."""
699699
response = await self.__get("actionGroups")
700-
return [Scenario(**scenario) for scenario in humps.decamelize(response)]
700+
return [
701+
ActionGroup(**action_group) for action_group in humps.decamelize(response)
702+
]
701703

702704
@retry_on_auth_error
703705
async def get_places(self) -> Place:

pyoverkiz/models.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -574,23 +574,23 @@ class Execution:
574574
description: str
575575
owner: str = field(repr=obfuscate_email)
576576
state: str
577-
action_group: list[dict[str, Any]]
577+
action_group: ActionGroup
578578

579579
def __init__(
580580
self,
581581
id: str,
582582
description: str,
583583
owner: str,
584584
state: str,
585-
action_group: list[dict[str, Any]],
585+
action_group: dict[str, Any],
586586
**_: Any,
587587
):
588588
"""Initialize Execution object from API fields."""
589589
self.id = id
590590
self.description = description
591591
self.owner = owner
592592
self.state = state
593-
self.action_group = action_group
593+
self.action_group = ActionGroup(**action_group)
594594

595595

596596
@define(init=False, kw_only=True)
@@ -607,15 +607,15 @@ def __init__(self, device_url: str, commands: list[dict[str, Any]]):
607607

608608

609609
@define(init=False, kw_only=True)
610-
class Scenario:
610+
class ActionGroup:
611611
"""An action group is composed of one or more actions.
612612
613613
Each action is related to a single setup device (designated by its device URL) and
614614
is composed of one or more commands to be executed on that device.
615615
"""
616616

617617
id: str = field(repr=obfuscate_id)
618-
creation_time: int
618+
creation_time: int | None = None
619619
last_update_time: int | None = None
620620
label: str = field(repr=obfuscate_string)
621621
metadata: str | None = None
@@ -629,10 +629,11 @@ class Scenario:
629629

630630
def __init__(
631631
self,
632-
creation_time: int,
633632
actions: list[dict[str, Any]],
634-
oid: str,
633+
creation_time: int | None = None,
635634
metadata: str | None = None,
635+
oid: str | None = None,
636+
id: str | None = None,
636637
last_update_time: int | None = None,
637638
label: str | None = None,
638639
shortcut: bool | None = None,
@@ -642,8 +643,11 @@ def __init__(
642643
notification_title: str | None = None,
643644
**_: Any,
644645
) -> None:
645-
"""Initialize Scenario (action group) from API data."""
646-
self.id = oid
646+
"""Initialize ActionGroup from API data and convert nested actions."""
647+
if oid is None and id is None:
648+
raise ValueError("Either 'oid' or 'id' must be provided")
649+
650+
self.id = cast(str, oid or id)
647651
self.creation_time = creation_time
648652
self.last_update_time = last_update_time
649653
self.label = (
@@ -656,7 +660,7 @@ def __init__(
656660
self.notification_text = notification_text
657661
self.notification_title = notification_title
658662
self.actions = [Action(**action) for action in actions]
659-
self.oid = oid
663+
self.oid = cast(str, oid or id)
660664

661665

662666
@define(init=False, kw_only=True)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{
3+
"startTime": 1767003511145,
4+
"owner": "somfy@imick.nl",
5+
"actionGroup": {
6+
"label": "Execution via Home Assistant",
7+
"shortcut": false,
8+
"notificationTypeMask": 0,
9+
"notificationCondition": "NEVER",
10+
"actions": [
11+
{
12+
"deviceURL": "rts://2025-8464-6867/16756006",
13+
"commands": [
14+
{
15+
"type": 1,
16+
"name": "close"
17+
}
18+
]
19+
},
20+
{
21+
"deviceURL": "rts://2025-8464-6867/16719623",
22+
"commands": [
23+
{
24+
"type": 1,
25+
"name": "identify"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"description": "Execution : Execution via Home Assistant",
32+
"id": "699dd967-0a19-0481-7a62-99b990a2feb8",
33+
"state": "TRANSMITTED",
34+
"executionType": "Immediate execution",
35+
"executionSubType": "MANUAL_CONTROL"
36+
}
37+
]

tests/test_client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ async def test_get_setup_option(
519519
],
520520
)
521521
@pytest.mark.asyncio
522-
async def test_get_scenarios(
522+
async def test_get_action_groups(
523523
self,
524524
client: OverkizClient,
525525
fixture_name: str,
@@ -533,16 +533,16 @@ async def test_get_scenarios(
533533
resp = MockResponse(action_group_mock.read())
534534

535535
with patch.object(aiohttp.ClientSession, "get", return_value=resp):
536-
scenarios = await client.get_scenarios()
536+
action_groups = await client.get_action_groups()
537537

538-
assert len(scenarios) == scenario_count
538+
assert len(action_groups) == scenario_count
539539

540-
for scenario in scenarios:
541-
assert scenario.oid
542-
assert scenario.label is not None
543-
assert scenario.actions
540+
for action_group in action_groups:
541+
assert action_group.oid
542+
assert action_group.label is not None
543+
assert action_group.actions
544544

545-
for action in scenario.actions:
545+
for action in action_group.actions:
546546
assert action.device_url
547547
assert action.commands
548548

0 commit comments

Comments
 (0)