Skip to content

Commit f907920

Browse files
authored
Merge pull request fronzbot#1136 from mback2k/reduce-redundancy
Reduce code duplication while staying API backwards compatible
2 parents 1e868e2 + 2d55d3d commit f907920

File tree

2 files changed

+133
-172
lines changed

2 files changed

+133
-172
lines changed

blinkpy/api.py

Lines changed: 108 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -262,33 +262,33 @@ async def request_sync_events(blink, network, **kwargs):
262262

263263

264264
@Throttle(seconds=MIN_THROTTLE_TIME)
265-
async def request_new_image(blink, network, camera_id, **kwargs):
265+
async def request_new_image(blink, network, camera_id, camera_type="", **kwargs):
266266
"""
267267
Request to capture new thumbnail for camera.
268268
269269
:param blink: Blink instance.
270270
:param network: Sync module network id.
271271
:param camera_id: Camera ID of camera to request new image from.
272+
:param camera_type: Camera type ("default", "mini", "doorbell").
272273
"""
273-
url = f"{blink.urls.base_url}/network/{network}/camera/{camera_id}/thumbnail"
274-
response = await http_post(blink, url)
275-
await wait_for_command(blink, response)
276-
return response
274+
return await request_camera_action(
275+
blink, network, camera_id, action="snap", camera_type=camera_type
276+
)
277277

278278

279279
@Throttle(seconds=MIN_THROTTLE_TIME)
280-
async def request_new_video(blink, network, camera_id, **kwargs):
280+
async def request_new_video(blink, network, camera_id, camera_type="", **kwargs):
281281
"""
282282
Request to capture new video clip.
283283
284284
:param blink: Blink instance.
285285
:param network: Sync module network id.
286286
:param camera_id: Camera ID of camera to request new video from.
287+
:param camera_type: Camera type ("default", "mini", "doorbell").
287288
"""
288-
url = f"{blink.urls.base_url}/network/{network}/camera/{camera_id}/clip"
289-
response = await http_post(blink, url)
290-
await wait_for_command(blink, response)
291-
return response
289+
return await request_camera_action(
290+
blink, network, camera_id, action="record", camera_type=camera_type
291+
)
292292

293293

294294
@Throttle(seconds=MIN_THROTTLE_TIME)
@@ -347,22 +347,18 @@ async def request_camera_usage(blink):
347347
return await http_get(blink, url)
348348

349349

350-
async def request_camera_liveview(blink, network, camera_id):
350+
async def request_camera_liveview(blink, network, camera_id, camera_type="", **kwargs):
351351
"""
352352
Request camera liveview.
353353
354354
:param blink: Blink instance.
355355
:param network: Sync module network id.
356356
:param camera_id: Camera ID of camera to request liveview from.
357+
:param camera_type: Camera type ("default", "mini", "doorbell").
357358
"""
358-
url = (
359-
f"{blink.urls.base_url}/api/v5/accounts/{blink.account_id}"
360-
f"/networks/{network}/cameras/{camera_id}/liveview"
359+
return await request_camera_action(
360+
blink, network, camera_id, action="liveview", camera_type=camera_type
361361
)
362-
data = dumps({"intent": "liveview"})
363-
response = await http_post(blink, url, data=data)
364-
await wait_for_command(blink, response)
365-
return response
366362

367363

368364
async def request_camera_sensors(blink, network, camera_id):
@@ -378,33 +374,37 @@ async def request_camera_sensors(blink, network, camera_id):
378374

379375

380376
@Throttle(seconds=MIN_THROTTLE_TIME)
381-
async def request_motion_detection_enable(blink, network, camera_id, **kwargs):
377+
async def request_motion_detection_enable(
378+
blink, network, camera_id, camera_type="", **kwargs
379+
):
382380
"""
383381
Enable motion detection for a camera.
384382
385383
:param blink: Blink instance.
386384
:param network: Sync module network id.
387385
:param camera_id: Camera ID of camera to enable.
386+
:param camera_type: Camera type ("default", "mini", "doorbell").
388387
"""
389-
url = f"{blink.urls.base_url}/network/{network}/camera/{camera_id}/enable"
390-
response = await http_post(blink, url)
391-
await wait_for_command(blink, response)
392-
return response
388+
return await request_camera_action(
389+
blink, network, camera_id, action="arm", camera_type=camera_type, arm=True
390+
)
393391

394392

395393
@Throttle(seconds=MIN_THROTTLE_TIME)
396-
async def request_motion_detection_disable(blink, network, camera_id, **kwargs):
394+
async def request_motion_detection_disable(
395+
blink, network, camera_id, camera_type="", **kwargs
396+
):
397397
"""
398398
Disable motion detection for a camera.
399399
400400
:param blink: Blink instance.
401401
:param network: Sync module network id.
402402
:param camera_id: Camera ID of camera to disable.
403+
:param camera_type: Camera type ("default", "mini", "doorbell").
403404
"""
404-
url = f"{blink.urls.base_url}/network/{network}/camera/{camera_id}/disable"
405-
response = await http_post(blink, url)
406-
await wait_for_command(blink, response)
407-
return response
405+
return await request_camera_action(
406+
blink, network, camera_id, action="arm", camera_type=camera_type, arm=False
407+
)
408408

409409

410410
async def request_local_storage_manifest(blink, network, sync_id):
@@ -567,6 +567,86 @@ async def http_post(blink, url, is_retry=False, data=None, json=True, timeout=TI
567567
)
568568

569569

570+
async def request_camera_action(
571+
blink, network, camera_id, action, camera_type="", arm=None, **kwargs
572+
):
573+
"""
574+
Perform camera actions for different camera types.
575+
576+
:param blink: Blink instance.
577+
:param network: Sync module network id.
578+
:param camera_id: Camera ID.
579+
:param action: Action type ("arm", "record", "snap", "liveview").
580+
:param camera_type: Camera type ("default", "mini", "doorbell").
581+
:param arm: Value for arm action (True/False), ignored for other actions.
582+
"""
583+
# Define URL patterns for different camera types
584+
# owl = mini cameras, catalina = default cameras, lotus = doorbell cameras
585+
camera_type = camera_type or "default"
586+
patterns = {
587+
"mini": {
588+
"base": (
589+
f"{blink.urls.base_url}/api/v1/accounts/{blink.account_id}"
590+
f"/networks/{network}/owls/{camera_id}"
591+
),
592+
"arm": {"path": "config", "data": {"enabled": arm}},
593+
"record": {"path": "clip", "data": None},
594+
"snap": {"path": "thumbnail", "data": None},
595+
"liveview": {"path": "liveview", "data": {"intent": "liveview"}},
596+
},
597+
"doorbell": {
598+
"base": (
599+
f"{blink.urls.base_url}/api/v1/accounts/{blink.account_id}"
600+
f"/networks/{network}/doorbells/{camera_id}"
601+
),
602+
"arm": {"path": "enable" if arm else "disable", "data": None},
603+
"record": {"path": "clip", "data": None},
604+
"snap": {"path": "thumbnail", "data": None},
605+
"liveview": {"path": "liveview", "data": {"intent": "liveview"}},
606+
},
607+
"default": {
608+
"base": f"{blink.urls.base_url}/network/{network}/camera/{camera_id}",
609+
"arm": {"path": "enable" if arm else "disable", "data": None},
610+
"record": {"path": "clip", "data": None},
611+
"snap": {"path": "thumbnail", "data": None},
612+
"liveview": {
613+
"path": (
614+
f"api/v5/accounts/{blink.account_id}"
615+
f"/networks/{network}/cameras/{camera_id}/liveview"
616+
),
617+
"data": {"intent": "liveview"},
618+
"full_path": True,
619+
},
620+
},
621+
}
622+
623+
if camera_type not in patterns:
624+
raise ValueError(f"Unsupported camera type: {camera_type}")
625+
626+
if action not in patterns[camera_type]:
627+
raise ValueError(
628+
f"Unsupported action '{action}' for camera type '{camera_type}'"
629+
)
630+
631+
config = patterns[camera_type][action]
632+
633+
# Build URL
634+
if config.get("full_path"):
635+
url = f"{blink.urls.base_url}/{config['path']}"
636+
else:
637+
url = f"{patterns[camera_type]['base']}/{config['path']}"
638+
639+
# Prepare data
640+
data = None
641+
if config["data"]:
642+
data = dumps(config["data"])
643+
644+
# Execute request
645+
response = await http_post(blink, url, data=data)
646+
await wait_for_command(blink, response)
647+
return response
648+
649+
570650
async def wait_for_command(blink, json_data: dict) -> bool:
571651
"""Wait for command to complete."""
572652
_LOGGER.debug("Command Wait %s", json_data)

0 commit comments

Comments
 (0)