This repository was archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlocks.py
More file actions
205 lines (171 loc) · 4.68 KB
/
locks.py
File metadata and controls
205 lines (171 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from seamapi.types import (
AbstractLocks,
ActionAttempt,
ConnectWebview,
ConnectWebviewId,
ConnectedAccount,
ConnectedAccountId,
Device,
DeviceId,
AbstractSeam as Seam,
)
import time
from typing import List, Union, Optional, cast
import requests
from seamapi.utils.convert_to_id import (
to_connect_webview_id,
to_connected_account_id,
to_device_id,
)
from seamapi.utils.report_error import report_error
class Locks(AbstractLocks):
"""
A class used to retreive lock data
through interaction with Seam API
...
Attributes
----------
seam : Seam
Initial seam class
Methods
-------
list(connected_account=None, connect_webview=None)
Gets a list of locks
get(device=None, name=None)
Gets a lock
lock_door(device)
Locks a lock
unlock_door(device)
Unlocks a lock
"""
seam: Seam
def __init__(self, seam: Seam):
"""
Parameters
----------
seam : Seam
Intial seam class
"""
self.seam = seam
@report_error
def list(
self,
connected_account: Optional[
Union[ConnectedAccountId, ConnectedAccount]
] = None,
connect_webview: Optional[
Union[ConnectWebviewId, ConnectWebview]
] = None,
) -> List[Device]:
"""Gets a list of locks.
Parameters
----------
connected_account : ConnectedAccountId or ConnectedAccount, optional
Connected account id or ConnectedAccount to get locks associated with
connect_webview : ConnectWebviewId or ConnectWebview, optional
Connect webview id or ConnectWebview to get locks associated with
Raises
------
Exception
If the API request wasn't successful.
Returns
------
A list of locks.
"""
params = {}
if connected_account:
params["connected_account_id"] = to_connected_account_id(
connected_account
)
if connect_webview:
params["connect_webview_id"] = to_connect_webview_id(
connect_webview
)
res = self.seam.make_request(
"GET",
"/locks/list",
params=params,
)
json_locks = res["devices"]
return [Device.from_dict(d) for d in json_locks]
@report_error
def get(
self,
device: Optional[Union[DeviceId, Device]] = None,
name: Optional[str] = None,
) -> Device:
"""Gets a lock.
Parameters
----------
device : DeviceId or Device, optional
Device id or Device to get the latest state of
name : str, optional
Device name
Raises
------
Exception
If the API request wasn't successful.
Returns
------
A lock dict.
"""
params = {}
if device:
params["device_id"] = to_device_id(device)
if name:
params["name"] = name
res = self.seam.make_request(
"GET",
"/locks/get",
params=params,
)
json_lock = res["device"]
return Device.from_dict(json_lock)
@report_error
def lock_door(self, device: Union[DeviceId, Device]) -> ActionAttempt:
"""Locks a lock.
Parameters
----------
device : DeviceId or Device
Device id or Device to be locked
Raises
------
Exception
If the API request wasn't successful.
Returns
------
ActionAttempt
"""
device_id = to_device_id(device)
res = self.seam.make_request(
"POST",
"/locks/lock_door",
json={"device_id": device_id},
)
return self.seam.action_attempts.poll_until_ready(
res["action_attempt"]["action_attempt_id"]
)
@report_error
def unlock_door(self, device: Union[DeviceId, Device]) -> ActionAttempt:
"""Unlocks a lock.
Parameters
----------
device : DeviceId or Device
Device id or Device to be locked
Raises
------
Exception
If the API request wasn't successful.
Returns
------
ActionAttempt
"""
device_id = to_device_id(device)
res = self.seam.make_request(
"POST",
"/locks/unlock_door",
json={"device_id": device_id},
)
return self.seam.action_attempts.poll_until_ready(
res["action_attempt"]["action_attempt_id"]
)