Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit 8eab60d

Browse files
fix: Add update noise threshold method (#87)
* Add update noise threshold method * Update seamapi/noise_thresholds.py * Update seamapi/noise_thresholds.py * Update noise_thresholds.update method * Update methods' docs * Test update noise thresholds
1 parent 2b89985 commit 8eab60d

File tree

4 files changed

+124
-13
lines changed

4 files changed

+124
-13
lines changed

seamapi/noise_thresholds.py

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ class NoiseThresholds(AbstractNoiseThresholds):
2828
-------
2929
list(device_id)
3030
Gets a list of noise thresholds of a noise-monitoring device
31-
create(device_id, starts_daily_at, ends_daily_at, sync=None, noise_threshold_decibels=None, noise_threshold_nrs=None)
31+
32+
create(device_id, starts_daily_at, ends_daily_at, name=None noise_threshold_decibels=None, noise_threshold_nrs=None, wait_for_action_attempt=True)
3233
Creates a noise threshold on a noise-monitoring device
33-
delete(noise_threshold_id, device_id, sync=None)
34+
35+
update(device_id, noise_threshold_id, name=None, starts_daily_at=None, ends_daily_at=None, noise_threshold_decibels=None, noise_threshold_nrs=None, wait_for_action_attempt=True)
36+
Updates a noise threshold on a noise-monitoring device
37+
38+
delete(noise_threshold_id, device_id, wait_for_action_attempt=True)
3439
Deletes a noise threshold on a noise-monitoring device
3540
"""
3641

@@ -105,6 +110,7 @@ def create(
105110
The noise level in decibels
106111
noise_threshold_nrs: Optional[float],
107112
Noise Level in Noiseaware Noise Risk Score (NRS)
113+
108114
Raises
109115
------
110116
Exception
@@ -168,8 +174,99 @@ def create(
168174
return NoiseThreshold.from_dict(noise_threshold)
169175

170176
@report_error
171-
def update(self, noise_threshold_id):
172-
raise NotImplementedError()
177+
def update(
178+
self,
179+
device_id: str,
180+
noise_threshold_id: str,
181+
name: Optional[str] = None,
182+
starts_daily_at: Optional[str] = None,
183+
ends_daily_at: Optional[str] = None,
184+
noise_threshold_decibels: Optional[float] = None,
185+
noise_threshold_nrs: Optional[float] = None,
186+
wait_for_action_attempt: Optional[bool] = True,
187+
) -> Union[ActionAttempt, NoiseThreshold]:
188+
"""Updates a noise threshold.
189+
Parameters
190+
----------
191+
device_id : str
192+
Device ID of a device to update noise threshold of
193+
noise_threshold_id : str
194+
Id of a noise threshold to update
195+
name: Optional[str]
196+
Noise threshold name
197+
starts_daily_at: Optional[str],
198+
Time when noise threshold becomes active
199+
ends_daily_at: Optional[str],
200+
Time when noise threshold becomes inactive
201+
noise_threshold_decibels: Optional[float],
202+
Noise level in decibels
203+
noise_threshold_nrs: Optional[float],
204+
Noise Level in Noiseaware Noise Risk Score (NRS)
205+
wait_for_action_attempt: Optional[bool]
206+
Should wait for action attempt to resolve
207+
208+
Raises
209+
------
210+
Exception
211+
If the API request wasn't successful.
212+
213+
Returns
214+
------
215+
ActionAttempt or NoiseThreshold
216+
"""
217+
params = {
218+
"device_id": device_id,
219+
"noise_threshold_id": noise_threshold_id,
220+
}
221+
222+
arguments = {
223+
"name": name,
224+
"starts_daily_at": starts_daily_at,
225+
"ends_daily_at": ends_daily_at,
226+
"noise_threshold_decibels": noise_threshold_decibels,
227+
"noise_threshold_nrs": noise_threshold_nrs,
228+
}
229+
230+
for name in arguments:
231+
if arguments[name]:
232+
params.update({name: arguments[name]})
233+
234+
res = self.seam.make_request(
235+
"PUT",
236+
"/noise_sensors/noise_thresholds/update",
237+
json=params,
238+
)
239+
240+
json_aa = res["action_attempt"]
241+
aa_error = None
242+
if "error" in json_aa and json_aa["error"] is not None:
243+
aa_error = ActionAttemptError(
244+
type=json_aa["error"]["type"],
245+
message=json_aa["error"]["message"],
246+
)
247+
248+
if not wait_for_action_attempt or aa_error:
249+
return ActionAttempt(
250+
action_attempt_id=json_aa["action_attempt_id"],
251+
status=json_aa["status"],
252+
action_type=json_aa["action_type"],
253+
result=json_aa["result"],
254+
error=aa_error,
255+
)
256+
257+
updated_action_attempt = self.seam.action_attempts.poll_until_ready(
258+
json_aa["action_attempt_id"]
259+
)
260+
261+
action_attempt_result = getattr(updated_action_attempt, "result", None)
262+
noise_threshold = action_attempt_result.get("noise_threshold", None)
263+
if not action_attempt_result or not noise_threshold:
264+
raise Exception(
265+
"Failed to create noise_threshold: no noise_threshold returned: "
266+
+ json.dumps(asdict(updated_action_attempt))
267+
)
268+
269+
return NoiseThreshold.from_dict(noise_threshold)
173270

174271
@report_error
175272
def delete(

seamapi/types.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,22 @@ def create(
294294
name: Optional[str],
295295
noise_threshold_decibels: Optional[float] = None,
296296
noise_threshold_nrs: Optional[float] = None,
297-
sync: Optional[bool] = None,
298-
) -> NoiseThreshold:
297+
wait_for_action_attempt: Optional[bool] = True,
298+
) -> Union[ActionAttempt, NoiseThreshold]:
299299
raise NotImplementedError
300300

301301
@abc.abstractmethod
302302
def update(
303303
self,
304+
device_id: str,
304305
noise_threshold_id: str,
305306
name: Optional[str] = None,
307+
starts_daily_at: Optional[str] = None,
308+
ends_daily_at: Optional[str] = None,
306309
noise_threshold_decibels: Optional[str] = None,
307310
noise_threshold_nrs: Optional[str] = None,
308-
) -> None:
311+
wait_for_action_attempt: Optional[bool] = True,
312+
) -> Union[ActionAttempt, NoiseThreshold]:
309313
raise NotImplementedError
310314

311315
@abc.abstractmethod
@@ -320,8 +324,8 @@ def delete(
320324
self,
321325
noise_threshold_id: str,
322326
device_id: str,
323-
sync: Optional[bool] = None,
324-
) -> None:
327+
wait_for_action_attempt: Optional[bool] = True,
328+
) -> ActionAttempt:
325329
raise NotImplementedError
326330

327331

tests/connected_accounts/test_connected_accounts.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from seamapi import Seam
22
from tests.fixtures.run_august_factory import run_august_factory
33

4-
4+
5+
56

67
def test_connected_accounts(seam: Seam):
78
run_august_factory(seam)
@@ -24,5 +25,7 @@ def test_connected_accounts(seam: Seam):
2425
try:
2526
seam.connected_accounts.get()
2627
except Exception as e:
27-
assert str(e) == "Must provide either ConnectedAccount (ConnectedAccount or ConnectedAccountId) or Email"
28-
28+
assert (
29+
str(e)
30+
== "Must provide either ConnectedAccount (ConnectedAccount or ConnectedAccountId) or Email"
31+
)

tests/noise_sensors/noise_thresholds/test_noise_thresholds.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ def get_minut_device_noise_thresholds():
3030
noise_thresholds = get_minut_device_noise_thresholds()
3131
assert len(noise_thresholds) == 1
3232

33-
seam.noise_sensors.noise_thresholds.create(
33+
noise_threshold = seam.noise_sensors.noise_thresholds.create(
3434
device_id=device.device_id,
3535
starts_daily_at="20:00:00[America/Los_Angeles]",
3636
ends_daily_at="08:00:00[America/Los_Angeles]",
3737
noise_threshold_decibels=75,
3838
)
3939
noise_thresholds = get_minut_device_noise_thresholds()
4040
assert len(noise_thresholds) == 2
41+
42+
updated_noise_threshold = seam.noise_sensors.noise_thresholds.update(
43+
device_id=device.device_id,
44+
noise_threshold_id=noise_threshold.noise_threshold_id,
45+
noise_threshold_decibels=80,
46+
)
47+
assert updated_noise_threshold.noise_threshold_decibels == 80

0 commit comments

Comments
 (0)