Compare commits

..
8 Commits
Author SHA1 Message Date
Kevin FronczakandGitHub 6cbfe1440a Merge pull request #7 from fronzbot/patch
Patch
2017-01-28 15:21:17 -05:00
Kevin FronczakandGitHub b68b639d57 Merge branch 'master' into patch 2017-01-28 15:21:00 -05:00
Kevin Fronczak 83dd8b0a24 Updated test_const with realistic response and fixed key error in blinkpy 2017-01-28 15:17:10 -05:00
Kevin FronczakandGitHub 29dabb46c8 v0.4.1 (#6)
* Fixed refresh function

* String cast for refresh

* Added image refresh function, increased test coverage

* Added a thumb dict function
2017-01-27 00:09:38 -05:00
Kevin Fronczak 96730f3879 Added a thumb dict function 2017-01-27 00:03:35 -05:00
Kevin Fronczak 50e5e8c82d Added image refresh function, increased test coverage 2017-01-26 23:43:32 -05:00
Kevin Fronczak 38df3a8f9d String cast for refresh 2017-01-26 23:28:11 -05:00
Kevin Fronczak b3f95b9231 Fixed refresh function 2017-01-26 22:19:58 -05:00
3 changed files with 225 additions and 31 deletions
+26 -4
View File
@@ -17,7 +17,7 @@ import requests
import getpass
import json
__version__ = '0.3.0'
__version__ = '0.4.2'
BLINK_URL = 'immedia-semi.com'
LOGIN_URL = 'https://prod.' + BLINK_URL + '/login'
@@ -186,8 +186,21 @@ class BlinkCamera(object):
self._BATTERY = values['battery']
self._NOTIFICATIONS = values['notifications']
def image_refresh(self):
url = BASE_URL + '/homescreen'
response = _request(url, headers=self._HEADER, type='get')['devices']
for element in response:
try:
if str(element['device_id']) == self._ID:
self._THUMB = BASE_URL + element['thumbnail'] + '.jpg'
return self._THUMB
except KeyError:
pass
return None
def image_to_file(self, path):
response = _request(self._THUMB, headers=self._HEADER, stream=True, json=False)
thumb = self.image_refresh()
response = _request(thumb, headers=self._HEADER, stream=True, json=False)
if response.status_code == 200:
with open(path, 'wb') as f:
for chunk in response:
@@ -215,6 +228,15 @@ class Blink(object):
def cameras(self):
return self._CAMERAS
@property
def camera_thumbs(self):
self.refresh()
data = {}
for name, camera in self._CAMERAS.items():
data[name] = camera.thumbnail
return data
@property
def id_table(self):
return self._IDLOOKUP
@@ -240,7 +262,7 @@ class Blink(object):
"""Gets all events on server"""
url = BASE_URL + '/events/network/' + self._NETWORKID
headers = self._AUTH_HEADER
self._EVENTS = _request(url, headers=headers, type='get')['events']
self._EVENTS = _request(url, headers=headers, type='get')['event']
return self._EVENTS
@property
@@ -287,7 +309,7 @@ class Blink(object):
for name, camera in self._CAMERAS.items():
for element in response:
try:
if element['id'] == camera.id:
if str(element['device_id']) == camera.id:
camera.update(element)
except KeyError:
pass
+42 -27
View File
@@ -4,6 +4,7 @@ import unittest
from unittest import mock
from blinkpy import LOGIN_URL
from blinkpy import BASE_URL
import test_const as const
def mocked_requests_post(*args, **kwargs):
class MockPostResponse:
@@ -14,7 +15,7 @@ def mocked_requests_post(*args, **kwargs):
return self.json_data
if args[0] == LOGIN_URL:
return MockPostResponse({"region":{"test": "Notacountry"}, "authtoken":{"authtoken":"abcd1234"}}, 200)
return MockPostResponse({"region":{const.REGION_ID: const.REGION}, "authtoken":{"authtoken":const.TOKEN}}, 200)
elif args[0].split("/")[-1] == 'arm':
return MockPostResponse({"armed":True}, 200)
elif args[0].split("/")[-1] == 'disarm':
@@ -33,20 +34,9 @@ def mocked_requests_get(*args, **kwargs):
return self.json_data
if args[0] == BASE_URL + '/networks':
return MockGetResponse({'networks':[{"id":7777,"account_id":3333},{"nothing":"nothing"}]}, 200)
return MockGetResponse({'networks':[{"id":const.NETWORK_ID,"account_id":const.ACCOUNT_ID},{"nothing":"nothing"}]}, 200)
else:
return MockGetResponse({'devices':[{'device_type':'camera','name':'test','device_id':123,'armed':False,'thumbnail':'/some/url','temp':65,'battery':3,'notifications':1},
{'device_type':'camera','name':'test2','device_id':321,'armed':True,'thumbnail':'/some/new/url','temp':56,'battery':5,'notifications':0},
{'device_type':'None'}
],
'events':[{'camera_id':123, 'type':'motion', 'video_url':'/some/dumb/location.mp4', 'created_at':'2017-01-01'},
{'camera_id':321, 'type':'None'}
],
'syncmodule':{'name':'SyncName', 'status':'online'},
'network':{'name':'Sync','armed':True, 'notifications':4}
},
200
)
return MockGetResponse(const.response, 200)
return MockGetResponse({'message':'ERROR','code':404}, 404)
@@ -58,12 +48,12 @@ class TestBlinkRequests(unittest.TestCase):
blink = blinkpy.Blink(username='user',password='password')
blink.setup_system()
self.assertEqual(blink.network_id, '7777')
self.assertEqual(blink.account_id, '3333')
self.assertEqual(blink.region, 'Notacountry')
self.assertEqual(blink.region_id, 'test')
self.assertEqual(blink.online, True)
self.assertEqual(blink.arm, True)
self.assertEqual(blink.network_id, str(const.NETWORK_ID))
self.assertEqual(blink.account_id, str(const.ACCOUNT_ID))
self.assertEqual(blink.region, const.REGION)
self.assertEqual(blink.region_id, const.REGION_ID)
self.assertEqual(blink.online, const.ISONLINE)
self.assertEqual(blink.arm, const.ARMED)
@mock.patch('blinkpy.requests.post', side_effect=mocked_requests_post)
@mock.patch('blinkpy.requests.get', side_effect=mocked_requests_get)
@@ -72,14 +62,39 @@ class TestBlinkRequests(unittest.TestCase):
blink.setup_system()
blink.last_motion()
for name, camera in blink.cameras.items():
if camera.id == '123':
self.assertEqual(name, 'test')
self.assertEqual(camera.armed, False)
self.assertEqual(camera.motion['video'], BASE_URL+'/some/dumb/location.mp4')
elif camera.id == '321':
self.assertEqual(name, 'test2')
self.assertEqual(camera.armed, True)
if camera.id == str(const.DEVICE_ID):
self.assertEqual(name, const.CAMERA_NAME)
self.assertEqual(camera.armed, const.ARMED)
self.assertEqual(camera.motion['video'], BASE_URL + const.THUMB + '.mp4')
elif camera.id == str(const.DEVICE_ID2):
self.assertEqual(name, const.CAMERA_NAME2)
self.assertEqual(camera.armed, const.ARMED2)
self.assertEqual(len(camera.motion.keys()), 0)
else:
assert False is True
@mock.patch('blinkpy.requests.post', side_effect=mocked_requests_post)
@mock.patch('blinkpy.requests.get', side_effect=mocked_requests_get)
def test_blink_refresh(self, mock_get, mock_post):
blink = blinkpy.Blink(username='user',password='password')
blink.setup_system()
const.response['devices'][0]['thumbnail'] = const.THUMB + const.THUMB2
blink.refresh()
for name, camera in blink.cameras.items():
if camera.id == str(const.DEVICE_ID):
self.assertEqual(camera.thumbnail, BASE_URL + const.THUMB + const.THUMB2 + '.jpg')
elif camera.id == str(const.DEVICE_ID2):
pass
else:
assert False is True
const.response['devices'][0]['thumbnail'] = 'new'
blink.cameras[const.CAMERA_NAME].image_refresh()
for name, camera in blink.cameras.items():
if camera.id == str(const.DEVICE_ID):
self.assertEqual(camera.thumbnail, BASE_URL + 'new' + '.jpg')
elif camera.id == str(const.DEVICE_ID2):
pass
else:
assert False is True
+157
View File
@@ -0,0 +1,157 @@
ISONLINE = True
ARMED = True
ARMED2 = False
REGION_ID = 'test'
REGION = 'Oceaniaeurasia'
TOKEN = 'abcd1234$$@@'
NETWORK_ID = 1337
DEVICE_ID = 9876
DEVICE_ID2 = 6789
ACCOUNT_ID = 1234
CAMERA_NAME = 'My Camera'
CAMERA_NAME2 = 'Number 2'
BATTERY = 3
BATTERY2 = 1
TEMP = 70
TEMP2 = 66
THUMB = '/url/camera/7777/clip'
THUMB2 = '/url/camera/8888/clip'
NOTIFS = 1
NOTIFS2 = 1
SYNC_ID = 1000
if ISONLINE:
ONLINE = 'online'
else:
ONLINE = 'offline'
response = {'account': {'notifications': 1},
'devices': [{'device_type': 'camera',
'notifications': NOTIFS,
'battery': BATTERY,
'active': 'disabled',
'errors': 0,
'error_msg': '',
'enabled': False,
'temp': TEMP,
'updated_at': '2017-01-27T03:14:24+00:00',
'lfr_strength': 3,
'armed': ARMED,
'device_id': DEVICE_ID,
'wifi_strength': 5,
'warning': 0,
'thumbnail': THUMB,
'name': CAMERA_NAME,
'status': 'done'},
{'device_type': 'camera',
'notifications': NOTIFS2,
'battery': BATTERY2,
'active': 'disabled',
'errors': 0,
'error_msg': '',
'enabled': False,
'temp': TEMP2,
'updated_at': '2017-01-27T03:14:24+00:00',
'lfr_strength': 3,
'armed': ARMED2,
'device_id': DEVICE_ID2,
'wifi_strength': 5,
'warning': 0,
'thumbnail': THUMB2,
'name': CAMERA_NAME2,
'status': 'done'},
{'updated_at': '2017-01-26T19:32:10+00:00',
'device_type': 'sync_module',
'notifications': 0,
'device_id': SYNC_ID,
'status':
'online',
'last_hb':
'2017-01-27T03:14:49+00:00',
'errors': 0,
'error_msg': '',
'warning': 0}],
'network': {'armed': ARMED,
'wifi_strength': 5,
'warning': 0,
'error_msg': '',
'name': 'Blink',
'notifications': NOTIFS,
'status': 'ok'},
'event': [{'camera_name': CAMERA_NAME,
'updated_at': '2017-01-28T19:51:52+00:00',
'sync_module_id': None,
'camera': DEVICE_ID,
'type': 'motion',
'duration': None,
'status': None,
'created_at': '2017-01-28T19:51:52+00:00',
'camera_id': DEVICE_ID,
'id': 123456789,
'siren_id': None,
'account_id': ACCOUNT_ID,
'notified': True,
'siren': None,
'syncmodule': None,
'video_url': THUMB+'.mp4',
'command_id': None,
'network_id': None,
'account': ACCOUNT_ID,
'video_id': 100000001},
{'updated_at': '2017-01-28T19:51:29+00:00',
'sync_module_id': SYNC_ID,
'camera': None,
'type': 'armed',
'duration': None,
'status': None,
'created_at':
'2017-01-28T19:51:29+00:00',
'camera_id': None,
'id': 123456789,
'siren_id': None,
'account_id': ACCOUNT_ID,
'notified': False,
'siren': None,
'syncmodule': SYNC_ID,
'command_id': None,
'network_id': None,
'account': ACCOUNT_ID},
{'updated_at': '2017-01-28T19:00:12+00:00',
'sync_module_id': SYNC_ID,
'camera': None,
'type': 'disarmed',
'duration': None,
'status': None,
'created_at': '2017-01-28T19:00:12+00:00',
'camera_id': None,
'id': 123456789,
'siren_id': None,
'account_id': 2463,
'notified': False,
'siren': None,
'syncmodule': SYNC_ID,
'command_id': None,
'network_id': None,
'account': ACCOUNT_ID},
{'camera_name': CAMERA_NAME,
'updated_at': '2017-01-28T18:59:55+00:00',
'sync_module_id': None,
'camera': DEVICE_ID,
'type': 'motion',
'duration': None,
'status': None,
'created_at': '2017-01-28T18:59:55+00:00',
'camera_id': DEVICE_ID,
'id': 123456789,
'siren_id': None,
'account_id': ACCOUNT_ID,
'notified': True,
'siren': None,
'syncmodule': None,
'command_id': None,
'network_id': None,
'account': ACCOUNT_ID}],
'syncmodule':{'name':'SyncName',
'status':ONLINE}}