Skip to content

Commit

Permalink
Fixed update() method and removed ding feature from stickupcams/flo…
Browse files Browse the repository at this point in the history
…odlight (#10428)

* Simplified URL expiration calculation and fixed refresh method

* Remove support from Ring from StickupCams or floodlight cameras

* Makes lint happy

* Removed unecessary attributes
  • Loading branch information
tchellomello authored and balloob committed Nov 11, 2017
1 parent 30bd92c commit fe2e0c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/binary_sensor/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

# Sensor types: Name, category, device_class
SENSOR_TYPES = {
'ding': ['Ding', ['doorbell', 'stickup_cams'], 'occupancy'],
'ding': ['Ding', ['doorbell'], 'occupancy'],
'motion': ['Motion', ['doorbell', 'stickup_cams'], 'motion'],
}

Expand Down
30 changes: 16 additions & 14 deletions homeassistant/components/camera/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import asyncio
import logging

from datetime import datetime, timedelta
from datetime import timedelta

import voluptuous as vol

Expand All @@ -23,6 +23,8 @@

DEPENDENCIES = ['ring', 'ffmpeg']

FORCE_REFRESH_INTERVAL = timedelta(minutes=45)

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(seconds=90)
Expand Down Expand Up @@ -63,8 +65,8 @@ def __init__(self, hass, camera, device_info):
self._ffmpeg_arguments = device_info.get(CONF_FFMPEG_ARGUMENTS)
self._last_video_id = self._camera.last_recording_id
self._video_url = self._camera.recording_url(self._last_video_id)
self._expires_at = None
self._utcnow = None
self._utcnow = dt_util.utcnow()
self._expires_at = FORCE_REFRESH_INTERVAL + self._utcnow

@property
def name(self):
Expand Down Expand Up @@ -123,19 +125,19 @@ def should_poll(self):

def update(self):
"""Update camera entity and refresh attributes."""
# extract the video expiration from URL
x_amz_expires = int(self._video_url.split('&')[0].split('=')[-1])
x_amz_date = self._video_url.split('&')[1].split('=')[-1]
_LOGGER.debug("Checking if Ring DoorBell needs to refresh video_url")

self._camera.update()
self._utcnow = dt_util.utcnow()
self._expires_at = \
timedelta(seconds=x_amz_expires) + \
dt_util.as_utc(datetime.strptime(x_amz_date, "%Y%m%dT%H%M%SZ"))

if self._last_video_id != self._camera.last_recording_id:
_LOGGER.debug("Updated Ring DoorBell last_video_id")
self._last_video_id = self._camera.last_recording_id
last_recording_id = self._camera.last_recording_id

if self._utcnow >= self._expires_at:
_LOGGER.debug("Updated Ring DoorBell video_url")
if self._last_video_id != last_recording_id or \
self._utcnow >= self._expires_at:

_LOGGER.info("Ring DoorBell properties refreshed")

# update attributes if new video or if URL has expired
self._last_video_id = self._camera.last_recording_id
self._video_url = self._camera.recording_url(self._last_video_id)
self._expires_at = FORCE_REFRESH_INTERVAL + self._utcnow
2 changes: 1 addition & 1 deletion homeassistant/components/sensor/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'Last Activity', ['doorbell', 'stickup_cams'], None, 'history', None],

'last_ding': [
'Last Ding', ['doorbell', 'stickup_cams'], None, 'history', 'ding'],
'Last Ding', ['doorbell'], None, 'history', 'ding'],

'last_motion': [
'Last Motion', ['doorbell', 'stickup_cams'], None,
Expand Down

0 comments on commit fe2e0c4

Please sign in to comment.