Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support creating scenes with controller trigger #74

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,22 @@ class Trigger(BaseIkeaModel):
type: str
triggered_at: Optional[datetime.datetime] = None
disabled: bool
trigger: Optional[ControllerTrigger] = None
```

Example how to create scene with trigger:
```python
from dirigera.devices.scene import Info, Icon, Trigger, SceneType, ControllerTrigger, ControllerType, ClickPattern

scene = dirigera_hub.create_scene(
info=Info(name="Scene with trigger", icon=Icon.SCENES_HEART),
scene_type=SceneType.USER_SCENE,
triggers=[
Trigger(type="app", disabled=False),
Trigger(type="controller", disabled=False,
trigger=ControllerTrigger(clickPattern=ClickPattern.SINGLE_PRESS, buttonIndex=0,
deviceId="0000aaaa-0000-0000-aa00-0a0aa0a000a0_1",
controllerType=ControllerType.SHORTCUT_CONTROLLER))])
```

All available icons can be found here: [Icons](./src/dirigera/devices/scene.py)
Expand Down
19 changes: 19 additions & 0 deletions src/dirigera/devices/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ class Trigger(BaseIkeaModel):
type: str
triggered_at: Optional[datetime.datetime] = None
disabled: bool
trigger: Optional[ControllerTrigger] = None


class ControllerTrigger(BaseIkeaModel):
days: Optional[List[str]] = None
controllerType: ControllerType
buttonIndex: int
clickPattern: ClickPattern
deviceId: str


class ControllerType(Enum):
SHORTCUT_CONTROLLER = "shortcutController"


class ClickPattern(Enum):
LONG_PRESS = "longPress"
DOUBLE_PRESS = "doublePress"
SINGLE_PRESS = "singlePress"


class ActionAttributes(BaseIkeaModel, extra="allow"):
Expand Down
62 changes: 55 additions & 7 deletions tests/test_scenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Any, Dict
import pytest
from src.dirigera.hub.abstract_smart_home_hub import FakeDirigeraHub
from src.dirigera.devices.scene import Scene, dict_to_scene
from src.dirigera.devices.scene import Scene, dict_to_scene, ControllerType

TEST_ID = "c9bbf831-6dcd-4442-8195-53eedb66a598"
TEST_NAME = "Tesscene"
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_trigger(fake_scene: Scene, fake_client: FakeDirigeraHub) -> None:


def test_dict_to_scene(fake_client: FakeDirigeraHub) -> None:
data: Dict[str, Any] = {
data1: Dict[str, Any] = {
"id": "c9bbf831-6dcd-4442-8195-53eedb66a598",
"info": {"name": "Tesscene", "icon": "scenes_clean_sparkles"},
"type": "userScene",
Expand All @@ -84,10 +84,58 @@ def test_dict_to_scene(fake_client: FakeDirigeraHub) -> None:
"lastUndo": "2023-08-27T10:29:33.049Z",
}

scene = dict_to_scene(data, fake_client)
assert scene.id == TEST_ID
assert scene.info.name == TEST_NAME
assert scene.info.icon.value == TEST_ICON
assert scene.last_completed == datetime.datetime.strptime(
scene1 = dict_to_scene(data1, fake_client)
assert scene1.id == TEST_ID
assert scene1.info.name == TEST_NAME
assert scene1.info.icon.value == TEST_ICON
assert scene1.last_completed == datetime.datetime.strptime(
TEST_LAST_COMPLETED, "%Y-%m-%dT%H:%M:%S.%f%z"
)

data2: Dict[str, Any] = {
"id": "00a0a00a-0aa0-0000-a000-00a0000a0a00",
"info": {
"name": "Night",
"icon": "scenes_clean_sparkles"
},
"type": "userScene",
"triggers": [
{
"id": "0000a0a0-0a00-000a-0a00-aaaaaa000000",
"type": "app",
"triggeredAt": "2024-04-23T21:34:51.619Z",
"disabled": False
},
{
"id": "a0000000-a000-0000-aaaa-0aaa000aa000",
"type": "controller",
"triggeredAt": "2024-04-20T05:49:20.178Z",
"disabled": False,
"trigger": {
"days": [
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun"
],
"controllerType": "shortcutController",
"clickPattern": "doublePress",
"buttonIndex": 0,
"deviceId": "0000aaaa-0000-0000-aa00-0a0aa0a000a0_2"
}
}
],
"actions": [],
"commands": [],
"createdAt": "2023-11-06T22:10:14.806Z",
"lastCompleted": "2024-04-24T05:13:24.352Z",
"lastTriggered": "2024-04-24T05:13:24.352Z",
"undoAllowedDuration": 30
}

scene2 = dict_to_scene(data2, fake_client)
assert scene2.triggers[1].trigger is not None
assert scene2.triggers[1].trigger.controllerType == ControllerType.SHORTCUT_CONTROLLER
Loading