Skip to content

Commit 315d9fa

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 25b6815 commit 315d9fa

File tree

4 files changed

+65
-22
lines changed

4 files changed

+65
-22
lines changed

pyoverkiz/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
UnknownUserException,
7272
)
7373
from pyoverkiz.models import (
74+
ActionGroup,
7475
Command,
7576
Device,
7677
Event,
@@ -82,7 +83,6 @@
8283
OptionParameter,
8384
OverkizServer,
8485
Place,
85-
Scenario,
8686
Setup,
8787
State,
8888
)
@@ -665,10 +665,12 @@ async def execute_commands(
665665
return cast(str, response["execId"])
666666

667667
@retry_on_auth_error
668-
async def get_scenarios(self) -> list[Scenario]:
669-
"""List the scenarios."""
668+
async def get_action_groups(self) -> list[ActionGroup]:
669+
"""List the action groups (scenarios)."""
670670
response = await self.__get("actionGroups")
671-
return [Scenario(**scenario) for scenario in humps.decamelize(response)]
671+
return [
672+
ActionGroup(**action_group) for action_group in humps.decamelize(response)
673+
]
672674

673675
@retry_on_auth_error
674676
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": "[email protected]",
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
@@ -423,7 +423,7 @@ async def test_get_setup_option(
423423
],
424424
)
425425
@pytest.mark.asyncio
426-
async def test_get_scenarios(
426+
async def test_get_action_groups(
427427
self,
428428
client: OverkizClient,
429429
fixture_name: str,
@@ -437,16 +437,16 @@ async def test_get_scenarios(
437437
resp = MockResponse(action_group_mock.read())
438438

439439
with patch.object(aiohttp.ClientSession, "get", return_value=resp):
440-
scenarios = await client.get_scenarios()
440+
action_groups = await client.get_action_groups()
441441

442-
assert len(scenarios) == scenario_count
442+
assert len(action_groups) == scenario_count
443443

444-
for scenario in scenarios:
445-
assert scenario.oid
446-
assert scenario.label is not None
447-
assert scenario.actions
444+
for action_group in action_groups:
445+
assert action_group.oid
446+
assert action_group.label is not None
447+
assert action_group.actions
448448

449-
for action in scenario.actions:
449+
for action in action_group.actions:
450450
assert action.device_url
451451
assert action.commands
452452

0 commit comments

Comments
 (0)