Skip to content

Commit d1fef02

Browse files
Merge pull request #18 from ysohma/add-scene-support
Add scene support
2 parents ab4ca45 + 18d70bc commit d1fef02

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,17 @@ remote.command('low_speed')
7979
# To send custom commands,
8080
remote.command('MyCustomCommand', customize=True)
8181
```
82+
83+
### Scenes
84+
```python
85+
# To list all infra red remotes
86+
scenes = switchbot.scenes()
87+
for scene in scenes:
88+
print(scene)
89+
90+
# If you already know a remote id:
91+
scene = switchbot.scene(id='')
92+
93+
# To execute scene
94+
scene.execute()
95+
```

switchbot/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from switchbot.client import SwitchBotClient
55
from switchbot.devices import Device
66
from switchbot.remotes import Remote
7+
from switchbot.scene import Scene
78

89
__version__ = "2.3.0"
910

@@ -41,3 +42,13 @@ def remote(self, id: str) -> Remote:
4142
if remote.id == id:
4243
return remote
4344
raise ValueError(f"Unknown remote {id}")
45+
46+
def scenes(self) -> List[Scene]:
47+
response = self.client.get("scenes")
48+
return [Scene(client=self.client, id=scene["scene_id"], **scene) for scene in response["body"]]
49+
50+
def scene(self, id: str) -> Scene:
51+
for scene in self.scenes():
52+
if scene.id == id:
53+
return scene
54+
raise ValueError(f"Unknown scene {id}")

switchbot/scene.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from __future__ import annotations
2+
3+
from switchbot.client import SwitchBotClient
4+
5+
6+
class Scene:
7+
def __init__(self, client: SwitchBotClient, id: str, **extra):
8+
self.client = client
9+
self.id: str = id
10+
self.name: str = extra.get("scene_name")
11+
12+
def execute(self):
13+
self.client.post(f"scenes/{self.id}/execute")
14+
15+
def __repr__(self):
16+
name = "Scene" if self.name is None else self.name
17+
name = name.replace(" ", "")
18+
return f"{name}(id={self.id})"

0 commit comments

Comments
 (0)