Skip to content

Commit 01babca

Browse files
committed
Added battery_string property to camera
Ability to detect if battery is "OK" or "Low"
1 parent aeeb894 commit 01babca

File tree

4 files changed

+121
-107
lines changed

4 files changed

+121
-107
lines changed

CHANGES.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ Changelog
33

44
A list of changes between each release
55

6-
0.6.0.dev1 (unreleased)
7-
^^^^^^^^^^^^^^^^^^
8-
- Added auto-reauthorization (token refresh) when a request fails due to an expired token
9-
10-
0.6.0.dev0 (unreleased)
6+
0.6.0 (unreleased)
117
^^^^^^^^^^^^^^^^^^
128
- Removed redundent properties that only called hidden variables
139
- Revised request wrapper function to be more intelligent
1410
- Added tests to ensure exceptions are caught and handled (100% coverage!)
11+
- Added auto-reauthorization (token refresh) when a request fails due to an expired token (@TySwift93)
12+
- Added battery level string to reduce confusion with the way Blink reports battery level as integer from 0 to 3
1513

1614
0.5.2 (2017-03-12)
1715
^^^^^^^^^^^^^^^^^^

blinkpy.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ def armed(self):
111111
"""Return camera arm status."""
112112
return self._status
113113

114+
@property
115+
def battery_string(self):
116+
"""Return string indicating battery status."""
117+
if self.battery > 1 and self.battery <= 3:
118+
return "OK"
119+
elif self.battery >= 0:
120+
return "Low"
121+
else:
122+
return "Unknown"
123+
114124
def snap_picture(self):
115125
"""Take a picture with camera to create a new thumbnail."""
116126
_request(self.blink, url=self.image_link,

helpers/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
MAJOR_VERSION = 0
88
MINOR_VERSION = 6
9-
PATCH_VERSION = '0.dev1'
9+
PATCH_VERSION = '0.dev2'
1010

1111
__version__ = '{}.{}.{}'.format(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
1212

tests/test_blink_cameras.py

Lines changed: 107 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,107 @@
1-
"""
2-
Tests the camera initialization and attributes of
3-
individual BlinkCamera instantiations.
4-
"""
5-
6-
import unittest
7-
from unittest import mock
8-
import blinkpy
9-
import tests.mock_responses as mresp
10-
11-
USERNAME = 'foobar'
12-
PASSWORD = 'deadbeef'
13-
14-
15-
class TestBlinkCameraSetup(unittest.TestCase):
16-
"""Test the Blink class in blinkpy."""
17-
18-
def setUp(self):
19-
"""Set up Blink module."""
20-
self.blink = blinkpy.Blink(username=USERNAME,
21-
password=PASSWORD)
22-
23-
def tearDown(self):
24-
"""Clean up after test."""
25-
self.blink = None
26-
27-
@mock.patch('blinkpy.blinkpy.requests.post',
28-
side_effect=mresp.mocked_requests_post)
29-
@mock.patch('blinkpy.blinkpy.requests.get',
30-
side_effect=mresp.mocked_requests_get)
31-
def test_camera_properties(self, mock_get, mock_post):
32-
"""Tests all property set/recall."""
33-
test_value = 'foobar'
34-
test_region_id = list(mresp.LOGIN_RESPONSE['region'].keys())[0]
35-
self.blink.setup_system()
36-
for name in self.blink.cameras:
37-
camera = self.blink.cameras[name]
38-
camera.name = test_value
39-
camera.clip = test_value + '.mp4'
40-
camera.thumbnail = test_value + '.jpg'
41-
camera.temperature = 10
42-
camera.battery = 0
43-
camera.notifications = 100
44-
camera.image_link = test_value + '/image.jpg'
45-
camera.arm_link = test_value + '/arm'
46-
camera.header = {'foo': 'bar'}
47-
camera.motion = {'bar': 'foo'}
48-
self.assertEqual(camera.clip, test_value + '.mp4')
49-
self.assertEqual(camera.name, test_value)
50-
self.assertEqual(camera.thumbnail, test_value + '.jpg')
51-
self.assertEqual(camera.temperature, 10)
52-
self.assertEqual(camera.battery, 0)
53-
self.assertEqual(camera.notifications, 100)
54-
self.assertEqual(camera.image_link, test_value + '/image.jpg')
55-
self.assertEqual(camera.arm_link, test_value + '/arm')
56-
self.assertEqual(camera.header, {'foo': 'bar'})
57-
self.assertEqual(camera.motion, {'bar': 'foo'})
58-
self.assertEqual(camera.region_id, test_region_id)
59-
60-
@mock.patch('blinkpy.blinkpy.requests.post',
61-
side_effect=mresp.mocked_requests_post)
62-
@mock.patch('blinkpy.blinkpy.requests.get',
63-
side_effect=mresp.mocked_requests_get)
64-
def test_camera_values_from_setup(self, mock_get, mock_post):
65-
"""Tests all property values after camera setup."""
66-
self.blink.setup_system()
67-
68-
# Get expected test values
69-
test_network_id = str(mresp.NETWORKS_RESPONSE['networks'][0]['id'])
70-
# pylint: disable=unused-variable
71-
(region_id, region), = mresp.LOGIN_RESPONSE['region'].items()
72-
# pylint: disable=protected-access
73-
expected_header = self.blink._auth_header
74-
test_urls = blinkpy.BlinkURLHandler(region_id)
75-
76-
test_cameras = mresp.get_test_cameras(test_urls.base_url)
77-
test_net_id_url = test_urls.network_url + test_network_id
78-
for name in self.blink.cameras:
79-
camera = self.blink.cameras[name]
80-
self.assertEqual(name, camera.name)
81-
if name in test_cameras:
82-
self.assertEqual(camera.id,
83-
test_cameras[name]['device_id'])
84-
self.assertEqual(camera.armed,
85-
test_cameras[name]['armed'])
86-
self.assertEqual(camera.thumbnail,
87-
test_cameras[name]['thumbnail'])
88-
self.assertEqual(camera.temperature,
89-
test_cameras[name]['temperature'])
90-
self.assertEqual(camera.battery,
91-
test_cameras[name]['battery'])
92-
self.assertEqual(camera.notifications,
93-
test_cameras[name]['notifications'])
94-
else:
95-
self.fail("Camera wasn't initialized: " + name)
96-
97-
expected_arm_link = test_net_id_url + '/camera/' + camera.id + '/'
98-
expected_image_link = expected_arm_link + 'thumbnail'
99-
self.assertEqual(camera.image_link, expected_image_link)
100-
self.assertEqual(camera.arm_link, expected_arm_link)
101-
self.assertEqual(camera.header, expected_header)
1+
"""
2+
Tests the camera initialization and attributes of
3+
individual BlinkCamera instantiations.
4+
"""
5+
6+
import unittest
7+
from unittest import mock
8+
import blinkpy
9+
import tests.mock_responses as mresp
10+
11+
USERNAME = 'foobar'
12+
PASSWORD = 'deadbeef'
13+
14+
15+
class TestBlinkCameraSetup(unittest.TestCase):
16+
"""Test the Blink class in blinkpy."""
17+
18+
def setUp(self):
19+
"""Set up Blink module."""
20+
self.blink = blinkpy.Blink(username=USERNAME,
21+
password=PASSWORD)
22+
23+
def tearDown(self):
24+
"""Clean up after test."""
25+
self.blink = None
26+
27+
@mock.patch('blinkpy.blinkpy.requests.post',
28+
side_effect=mresp.mocked_requests_post)
29+
@mock.patch('blinkpy.blinkpy.requests.get',
30+
side_effect=mresp.mocked_requests_get)
31+
def test_camera_properties(self, mock_get, mock_post):
32+
"""Tests all property set/recall."""
33+
test_value = 'foobar'
34+
test_region_id = list(mresp.LOGIN_RESPONSE['region'].keys())[0]
35+
self.blink.setup_system()
36+
for name in self.blink.cameras:
37+
camera = self.blink.cameras[name]
38+
camera.name = test_value
39+
camera.clip = test_value + '.mp4'
40+
camera.thumbnail = test_value + '.jpg'
41+
camera.temperature = 10
42+
camera.battery = 0
43+
camera.notifications = 100
44+
camera.image_link = test_value + '/image.jpg'
45+
camera.arm_link = test_value + '/arm'
46+
camera.header = {'foo': 'bar'}
47+
camera.motion = {'bar': 'foo'}
48+
self.assertEqual(camera.clip, test_value + '.mp4')
49+
self.assertEqual(camera.name, test_value)
50+
self.assertEqual(camera.thumbnail, test_value + '.jpg')
51+
self.assertEqual(camera.temperature, 10)
52+
self.assertEqual(camera.battery, 0)
53+
self.assertEqual(camera.notifications, 100)
54+
self.assertEqual(camera.image_link, test_value + '/image.jpg')
55+
self.assertEqual(camera.arm_link, test_value + '/arm')
56+
self.assertEqual(camera.header, {'foo': 'bar'})
57+
self.assertEqual(camera.motion, {'bar': 'foo'})
58+
self.assertEqual(camera.region_id, test_region_id)
59+
60+
self.assertEqual(camera.battery_string, "Low")
61+
camera.battery = 3
62+
self.assertEqual(camera.battery_string, "OK")
63+
camera.battery = -10
64+
self.assertEqual(camera.battery_string, "Unknown")
65+
66+
@mock.patch('blinkpy.blinkpy.requests.post',
67+
side_effect=mresp.mocked_requests_post)
68+
@mock.patch('blinkpy.blinkpy.requests.get',
69+
side_effect=mresp.mocked_requests_get)
70+
def test_camera_values_from_setup(self, mock_get, mock_post):
71+
"""Tests all property values after camera setup."""
72+
self.blink.setup_system()
73+
74+
# Get expected test values
75+
test_network_id = str(mresp.NETWORKS_RESPONSE['networks'][0]['id'])
76+
# pylint: disable=unused-variable
77+
(region_id, region), = mresp.LOGIN_RESPONSE['region'].items()
78+
# pylint: disable=protected-access
79+
expected_header = self.blink._auth_header
80+
test_urls = blinkpy.BlinkURLHandler(region_id)
81+
82+
test_cameras = mresp.get_test_cameras(test_urls.base_url)
83+
test_net_id_url = test_urls.network_url + test_network_id
84+
for name in self.blink.cameras:
85+
camera = self.blink.cameras[name]
86+
self.assertEqual(name, camera.name)
87+
if name in test_cameras:
88+
self.assertEqual(camera.id,
89+
test_cameras[name]['device_id'])
90+
self.assertEqual(camera.armed,
91+
test_cameras[name]['armed'])
92+
self.assertEqual(camera.thumbnail,
93+
test_cameras[name]['thumbnail'])
94+
self.assertEqual(camera.temperature,
95+
test_cameras[name]['temperature'])
96+
self.assertEqual(camera.battery,
97+
test_cameras[name]['battery'])
98+
self.assertEqual(camera.notifications,
99+
test_cameras[name]['notifications'])
100+
else:
101+
self.fail("Camera wasn't initialized: " + name)
102+
103+
expected_arm_link = test_net_id_url + '/camera/' + camera.id + '/'
104+
expected_image_link = expected_arm_link + 'thumbnail'
105+
self.assertEqual(camera.image_link, expected_image_link)
106+
self.assertEqual(camera.arm_link, expected_arm_link)
107+
self.assertEqual(camera.header, expected_header)

0 commit comments

Comments
 (0)