Skip to content

Commit

Permalink
Fix for camera image captures (#61)
Browse files Browse the repository at this point in the history
* Fix for camera image captures

* Update tests and remove unused constant

* Update capture method

* Update to tests and removed unused constant

* Update capture method

* Update comment

* Updates to camera.py and tests

* Refactoring

* Updated timeline_event for IP cam
  • Loading branch information
shred86 authored and MisterWil committed Jan 28, 2020
1 parent 81d8238 commit bc33934
Show file tree
Hide file tree
Showing 5 changed files with 473 additions and 218 deletions.
12 changes: 10 additions & 2 deletions abodepy/devices/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ def __init__(self, json_obj, abode):

def capture(self):
"""Request a new camera image."""
url = str.replace(CONST.CAMS_ID_CAPTURE_URL,
'$DEVID$', self.device_id)
# Abode IP cameras use a different URL for image captures.
if 'control_url_snapshot' in self._json_state:
url = CONST.BASE_URL + self._json_state['control_url_snapshot']

elif 'control_url' in self._json_state:
url = CONST.BASE_URL + self._json_state['control_url']

else:
raise AbodeException((ERROR.MISSING_CONTROL_URL))

try:
response = self._abode.send_request("put", url)

_LOGGER.debug("Capture image response: %s", response.text)

return True

except AbodeException as exc:
_LOGGER.warning("Failed to capture image: %s", exc)

Expand Down
3 changes: 0 additions & 3 deletions abodepy/helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ def get_panel_mode_url(area, mode):
AUTOMATION_EDIT_URL = AUTOMATION_ID_URL + 'edit'
AUTOMATION_APPLY_URL = AUTOMATION_ID_URL + 'apply'

CAMS_ID_CAPTURE_URL = BASE_URL + 'api/v1/cams/$DEVID$/capture'


TIMELINE_IMAGES_ID_URL = BASE_URL + \
'api/v1/timeline?device_id=$DEVID$&dir=next' + \
'&event_label=Image+Capture&size=1'
Expand Down
3 changes: 3 additions & 0 deletions abodepy/helpers/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@

SOCKETIO_ERROR = (
29, "SocketIO Error Packet Received")

MISSING_CONTROL_URL = (
30, "Control URL does not exist in device JSON.")
206 changes: 206 additions & 0 deletions tests/mock/devices/ipcam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
"""Mock Abode IP Camera Device."""
import abodepy.helpers.constants as CONST

DEVICE_ID = 'ZB:00000305'
CONTROL_URL = 'api/v1/cams/' + DEVICE_ID + '/record'
CONTROL_URL_SNAPSHOT = 'api/v1/cams/' + DEVICE_ID + '/capture'


def device(devid=DEVICE_ID, status=CONST.STATUS_ONLINE,
low_battery=False, no_response=False):
"""IP camera mock device."""
return '''
{
"id":"''' + devid + '''",
"type_tag": "device_type.ipcam",
"type": "IP Cam",
"name": "Living Room Camera",
"area": "1",
"zone": "1",
"sort_order": "",
"is_window": "",
"bypass": "0",
"schar_24hr": "1",
"sresp_24hr": "5",
"sresp_mode_0": "0",
"sresp_entry_0": "0",
"sresp_exit_0": "0",
"group_name": "Streaming Camera",
"group_id": "397974",
"default_group_id": "1",
"sort_id": "10000",
"sresp_mode_1": "0",
"sresp_entry_1": "0",
"sresp_exit_1": "0",
"sresp_mode_2": "0",
"sresp_entry_2": "0",
"sresp_exit_2": "0",
"sresp_mode_3": "0",
"uuid": "123456789",
"sresp_entry_3": "0",
"sresp_exit_3": "0",
"sresp_mode_4": "0",
"sresp_entry_4": "0",
"sresp_exit_4": "0",
"version": "1.0.2.22G_6.8E_homekit_2.0.9_s2 ABODE oz",
"origin": "abode",
"has_subscription": null,
"onboard": "1",
"s2_grnt_keys": "",
"s2_dsk": "",
"s2_propty": "",
"s2_keys_valid": "",
"zwave_secure_protocol": "",
"control_url":"''' + CONTROL_URL + '''",
"deep_link": null,
"status_color": "#5cb85c",
"faults": {
"low_battery":''' + str(int(low_battery)) + ''',
"tempered": 0,
"supervision": 0,
"out_of_order": 0,
"no_response":''' + str(int(no_response)) + ''',
"jammed": 0,
"zwave_fault": 0
},
"status":"''' + status + '''",
"status_display": "Online",
"statuses": [],
"status_ex": "",
"actions": [
{
"label": "Capture Video",
"value": "a=1&z=1&req=vid;"
},
{
"label": "Turn off Live Video",
"value": "a=1&z=1&privacy=on;"
},
{
"label": "Turn on Live Video",
"value": "a=1&z=1&privacy=off;"
}
],
"status_icons": [],
"icon": "assets\/icons\/streaming-camaera-new.svg",
"control_url_snapshot":"''' + CONTROL_URL_SNAPSHOT + '''",
"ptt_supported": true,
"is_new_camera": 1,
"stream_quality": 3,
"camera_mac": "AB:CD:EF:GF:HI",
"privacy": "1",
"enable_audio": "1",
"alarm_video": "25",
"pre_alarm_video": "5",
"mic_volume": "75",
"speaker_volume": "75",
"mic_default_volume": 40,
"speaker_default_volume": 46,
"bandwidth": {
"slider_labels": [
{
"name": "High",
"value": 3
},
{
"name": "Medium",
"value": 2
},
{
"name": "Low",
"value": 1
}
],
"min": 1,
"max": 3,
"step": 1
},
"volume": {
"min": 0,
"max": 100,
"step": 1
},
"video_flip": "0",
"hframe": "1080P"
}'''


def get_capture_timeout():
"""Mock timeout response."""
return '''
{
"code":600,
"message":"Image Capture request has timed out.",
"title":"",
"detail":null
}'''

FILE_PATH_ID = 'ZB00000305'
FILE_PATH = 'api/storage/' + FILE_PATH_ID + '/2020-01-26/173238/0.jpg'

LOCATION_HEADER = 'https://www.google.com/images/branding/googlelogo/' + \
'1x/googlelogo_color_272x92dp.png'


def timeline_event(devid=DEVICE_ID, event_code='5001', file_path=FILE_PATH):
"""Camera Timeline Event Mockup."""
return '''
{
"mac": "B0:C5:CZ:54:12:9A",
"id": "1171272698",
"xml": null,
"date": "01/26/2020",
"time": "05:32 PM",
"event_utc": "1580088758",
"event_cid": "",
"event_code": "''' + event_code + '''",
"device_id": "''' + devid + '''",
"device_type_id": "69",
"device_type": "IP Cam",
"timeline_ha_device": null,
"d_name": "Living Room Camera",
"delete_by_user": null,
"pin_code_user": " ",
"file_del_at": "",
"nest_has_motion": null,
"nest_has_sound": null,
"nest_has_person": null,
"neaz": null,
"hasFaults": "0",
"file_path":"''' + file_path + '''",
"deep_link": null,
"file_name": "48755_b0c5ca37894b_2020-01-26_173238_0-M2+56431.jpg",
"file_size": "197207",
"file_count": "1",
"file_is_del": "0",
"event_type": "Image Capture",
"severity": "6",
"pos": "l",
"color": "#40bbea",
"is_alarm": "0",
"triggered_by_str": null,
"ha_type": null,
"ha_device_name": null,
"ha_location": null,
"ha_mobile": null,
"ha_cond": null,
"h_location": null,
"ha_trigger": null,
"icon": "assets/email/motion-camera.png",
"user_id": "95244",
"user_name": "Shred",
"mobile_name": "",
"parent_tid": "",
"app_type": "WebApp",
"viewed_by_uid": null,
"verified_by_tid": null,
"la_applied_by": null,
"la_event_type": null,
"la_culprit_mobiles": null,
"la_executed": null,
"la_applied_at": null,
"device_name": "Living Room Camera",
"event_name": "Living Room Camera Image Capture",
"event_by": "by Shred using WebApp",
"file_delete_by": ""
}'''
Loading

0 comments on commit bc33934

Please sign in to comment.