Skip to content

Commit

Permalink
Merge pull request #177 from fronzbot/fix-motion-detect
Browse files Browse the repository at this point in the history
Fix motion detect
  • Loading branch information
fronzbot authored May 22, 2019
2 parents d46e7ed + 50b1a35 commit 683650e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
16 changes: 9 additions & 7 deletions blinkpy/blinkpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@
create_session, merge_dicts, get_time, BlinkURLHandler,
BlinkAuthenticationException, Throttle)
from blinkpy.helpers.constants import (
BLINK_URL, LOGIN_URL, OLD_LOGIN_URL, LOGIN_BACKUP_URL)
BLINK_URL, LOGIN_URL, OLD_LOGIN_URL, LOGIN_BACKUP_URL,
DEFAULT_MOTION_INTERVAL, DEFAULT_REFRESH, MIN_THROTTLE_TIME)
from blinkpy.helpers.constants import __version__

REFRESH_RATE = 30

# Prevents rapid calls to blink.refresh()
# with the force_cache flag set to True
MIN_THROTTLE_TIME = 2

_LOGGER = logging.getLogger(__name__)

Expand All @@ -46,14 +42,19 @@ class Blink():
"""Class to initialize communication."""

def __init__(self, username=None, password=None,
refresh_rate=REFRESH_RATE):
refresh_rate=DEFAULT_REFRESH,
motion_interval=DEFAULT_MOTION_INTERVAL):
"""
Initialize Blink system.
:param username: Blink username (usually email address)
:param password: Blink password
:param refresh_rate: Refresh rate of blink information.
Defaults to 15 (seconds)
:param motion_interval: How far back to register motion in minutes.
Defaults to last refresh time.
Useful for preventing motion_detected property
from de-asserting too quickly.
"""
self._username = username
self._password = password
Expand All @@ -73,6 +74,7 @@ def __init__(self, username=None, password=None,
self.cameras = CaseInsensitiveDict({})
self.video_list = CaseInsensitiveDict({})
self._login_url = LOGIN_URL
self.motion_interval = DEFAULT_MOTION_INTERVAL
self.version = __version__

@property
Expand Down
4 changes: 4 additions & 0 deletions blinkpy/helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@
OTHER
'''
TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S%Z'

DEFAULT_MOTION_INTERVAL = 1
DEFAULT_REFRESH = 30
MIN_THROTTLE_TIME = 2
2 changes: 1 addition & 1 deletion blinkpy/helpers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_time(time_to_convert=None):
"""Create blink-compatible timestamp."""
if time_to_convert is None:
time_to_convert = time.time()
return time.strftime(TIMESTAMP_FORMAT, time.localtime(time_to_convert))
return time.strftime(TIMESTAMP_FORMAT, time.gmtime(time_to_convert))


def merge_dicts(dict_a, dict_b):
Expand Down
10 changes: 9 additions & 1 deletion blinkpy/sync_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, blink, network_name, network_id, camera_list):
self.network_info = None
self.events = []
self.cameras = CaseInsensitiveDict({})
self.motion_interval = blink.motion_interval
self.motion = {}
self.last_record = {}
self.camera_list = camera_list
Expand Down Expand Up @@ -161,8 +162,15 @@ def refresh(self, force_cache=False):

def check_new_videos(self):
"""Check if new videos since last refresh."""
try:
interval = self.blink.last_refresh - self.motion_interval*60
except TypeError:
# This is the first start, so refresh hasn't happened yet.
# No need to check for motion.
return False

resp = api.request_videos(self.blink,
time=self.blink.last_refresh,
time=interval,
page=1)

for camera in self.cameras.keys():
Expand Down
10 changes: 9 additions & 1 deletion tests/test_sync_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class TestBlinkSyncModule(unittest.TestCase):
def setUp(self):
"""Set up Blink module."""
self.blink = blinkpy.Blink(username=USERNAME,
password=PASSWORD)
password=PASSWORD,
motion_interval=0)
# pylint: disable=protected-access
self.blink._auth_header = {
'Host': 'test.url.tld',
'TOKEN_AUTH': 'foobar123'
}
self.blink.last_refresh = 0
self.blink.urls = blinkpy.BlinkURLHandler('test')
self.blink.sync['test'] = BlinkSyncModule(self.blink,
'test',
Expand Down Expand Up @@ -59,6 +61,12 @@ def test_get_camera_info(self, mock_resp):
self.assertEqual(self.blink.sync['test'].get_camera_info('1234'),
'foobar')

def test_check_new_videos_startup(self, mock_resp):
"""Test that check_new_videos does not block startup."""
sync_module = self.blink.sync['test']
self.blink.last_refresh = None
self.assertFalse(sync_module.check_new_videos())

def test_check_new_videos(self, mock_resp):
"""Test recent video response."""
mock_resp.return_value = {
Expand Down

0 comments on commit 683650e

Please sign in to comment.