diff --git a/ring_doorbell/__init__.py b/ring_doorbell/__init__.py index a05d3d58..3e391364 100644 --- a/ring_doorbell/__init__.py +++ b/ring_doorbell/__init__.py @@ -159,7 +159,8 @@ def query(self, attempts=RETRY_TOKEN, method='GET', raw=False, - extra_params=None): + extra_params=None, + json=None): """Query data from Ring API.""" if self.debug: _LOGGER.debug("Querying %s", url) @@ -189,7 +190,7 @@ def query(self, elif method == 'PUT': req = self.session.put((url), params=urlencode(params)) elif method == 'POST': - req = self.session.post((url), params=urlencode(params)) + req = self.session.post((url), params=urlencode(params), json=json) if self.debug: _LOGGER.debug("_query %s ret %s", loop, req.status_code) diff --git a/ring_doorbell/const.py b/ring_doorbell/const.py index e3e1d2da..ccbba380 100644 --- a/ring_doorbell/const.py +++ b/ring_doorbell/const.py @@ -43,6 +43,8 @@ NEW_SESSION_ENDPOINT = '/clients_api/session' RINGTONES_ENDPOINT = '/ringtones' SIREN_ENDPOINT = DOORBELLS_ENDPOINT + '/siren_{1}' +SNAPSHOT_ENDPOINT = "/clients_api/snapshots/image/{0}" +SNAPSHOT_TIMESTAMP_ENDPOINT = "/clients_api/snapshots/timestamps" TESTSOUND_CHIME_ENDPOINT = CHIMES_ENDPOINT + '/play_sound' URL_DOORBELL_HISTORY = DOORBELLS_ENDPOINT + '/history' URL_RECORDING = '/clients_api/dings/{0}/recording' diff --git a/ring_doorbell/doorbot.py b/ring_doorbell/doorbot.py index 699f8007..81695793 100644 --- a/ring_doorbell/doorbot.py +++ b/ring_doorbell/doorbot.py @@ -5,6 +5,7 @@ from datetime import datetime import os import pytz +import time from ring_doorbell.generic import RingGeneric @@ -14,7 +15,8 @@ API_URI, DOORBELLS_ENDPOINT, DOORBELL_VOL_MIN, DOORBELL_VOL_MAX, DOORBELL_EXISTING_TYPE, DINGS_ENDPOINT, FILE_EXISTS, LIVE_STREAMING_ENDPOINT, MSG_BOOLEAN_REQUIRED, MSG_EXISTING_TYPE, - MSG_VOL_OUTBOUND, URL_DOORBELL_HISTORY, URL_RECORDING) + MSG_VOL_OUTBOUND, SNAPSHOT_ENDPOINT, SNAPSHOT_TIMESTAMP_ENDPOINT, + URL_DOORBELL_HISTORY, URL_RECORDING) _LOGGER = logging.getLogger(__name__) @@ -342,3 +344,18 @@ def volume(self, value): def connection_status(self): """Return connection status.""" return self._attrs.get('alerts').get('connection') + + def get_snapshot(self, retries=3, delay=1): + """Take a snapshot and download it""" + url = API_URI + SNAPSHOT_TIMESTAMP_ENDPOINT + payload = {"doorbot_ids": [self._attrs.get('id')]} + self._ring.query(url, json=payload) + request_time = time.time() + for _ in range(retries): + time.sleep(delay) + response = self._ring.query(url, method="POST", json=payload, + raw=1).json() + if response["timestamps"][0]["timestamp"] / 1000 > request_time: + return self._ring.query(API_URI + SNAPSHOT_ENDPOINT.format( + self._attrs.get('id')), raw=True).content + return False