Skip to content

Commit

Permalink
Merge pull request #388 from fronzbot/fix-mini-thumb-refresh
Browse files Browse the repository at this point in the history
Generalized image/video download, fixed non-updating image for minis
  • Loading branch information
fronzbot authored Oct 6, 2020
2 parents 828b411 + b5b7498 commit a94f59e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions blinkpy/blinkpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def refresh(self, force=False):
if not self.available:
self.setup_post_verify()

self.get_homescreen()
for sync_name, sync_module in self.sync.items():
_LOGGER.debug("Attempting refresh of sync %s", sync_name)
sync_module.refresh(force_cache=force)
Expand Down
30 changes: 14 additions & 16 deletions blinkpy/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def arm(self, value):
self.sync.blink, self.network_id, self.camera_id
)

def get_media(self, media_type="image"):
"""Download media (image or video)."""
url = self.thumbnail
if media_type.lower() == "video":
url = self.clip
return api.http_get(
self.sync.blink, url=url, stream=True, json=False, timeout=TIMEOUT_MEDIA,
)

def snap_picture(self):
"""Take a picture with camera to create a new thumbnail."""
return api.request_new_image(self.sync.blink, self.network_id, self.camera_id)
Expand Down Expand Up @@ -180,21 +189,10 @@ def update_images(self, config, force_cache=False):
update_cached_video = True

if new_thumbnail is not None and (update_cached_image or force_cache):
self._cached_image = api.http_get(
self.sync.blink,
url=self.thumbnail,
stream=True,
json=False,
timeout=TIMEOUT_MEDIA,
)
self._cached_image = self.get_media()

if clip_addr is not None and (update_cached_video or force_cache):
self._cached_video = api.http_get(
self.sync.blink,
url=self.clip,
stream=True,
json=False,
timeout=TIMEOUT_MEDIA,
)
self._cached_video = self.get_media(media_type="video")

def get_liveview(self):
"""Get livewview rtsps link."""
Expand All @@ -210,7 +208,7 @@ def image_to_file(self, path):
:param path: Path to write file
"""
_LOGGER.debug("Writing image from %s to %s", self.name, path)
response = self._cached_image
response = self.get_media()
if response.status_code == 200:
with open(path, "wb") as imgfile:
copyfileobj(response.raw, imgfile)
Expand All @@ -226,7 +224,7 @@ def video_to_file(self, path):
:param path: Path to write file
"""
_LOGGER.debug("Writing video from %s to %s", self.name, path)
response = self._cached_video
response = self.get_media(media_type="video")
if response is None:
_LOGGER.error("No saved video exist for %s.", self.name)
return
Expand Down
2 changes: 1 addition & 1 deletion tests/test_blinkpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_throttle(self, mock_time):
self.assertEqual(self.blink.last_refresh, None)
with mock.patch(
"blinkpy.sync_module.BlinkSyncModule.refresh", return_value=True
):
), mock.patch("blinkpy.blinkpy.Blink.get_homescreen", return_value=True):
self.blink.refresh()

self.assertEqual(self.blink.last_refresh, now)
Expand Down

0 comments on commit a94f59e

Please sign in to comment.