Skip to content

Commit ba3c2e3

Browse files
authored
Merge pull request #401 from fronzbot/fix-exception-on-refresh
Added test to catch NoneType error on refresh, fixed offending code
2 parents 0842739 + 92a2cc2 commit ba3c2e3

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

blinkpy/blinkpy.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,22 @@ def __init__(
7676
self.no_owls = no_owls
7777

7878
@util.Throttle(seconds=MIN_THROTTLE_TIME)
79-
def refresh(self, force=False):
79+
def refresh(self, force=False, force_cache=False):
8080
"""
8181
Perform a system refresh.
8282
83-
:param force: Force an update of the camera data
83+
:param force: Used to override throttle, resets refresh
84+
:param force_cache: Used to force update without overriding throttle
8485
"""
85-
if self.check_if_ok_to_update() or force:
86+
if self.check_if_ok_to_update() or force or force_cache:
8687
if not self.available:
8788
self.setup_post_verify()
8889

8990
self.get_homescreen()
9091
for sync_name, sync_module in self.sync.items():
9192
_LOGGER.debug("Attempting refresh of sync %s", sync_name)
92-
sync_module.refresh(force_cache=force)
93-
if not force:
93+
sync_module.refresh(force_cache=(force or force_cache))
94+
if not force_cache:
9495
# Prevents rapid clearing of motion detect property
9596
self.last_refresh = int(time.time())
9697
return True

blinkpy/sync_module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def get_owl_info(self, name):
145145
for owl in self.blink.homescreen["owls"]:
146146
if owl["name"] == name:
147147
return owl
148-
except KeyError:
148+
except (TypeError, KeyError):
149149
pass
150150
return None
151151

@@ -270,7 +270,7 @@ def get_camera_info(self, camera_id, **kwargs):
270270
if owl["name"] == self.name:
271271
self.status = owl["enabled"]
272272
return owl
273-
except KeyError:
273+
except (TypeError, KeyError):
274274
pass
275275
return None
276276

tests/test_blink_functions.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,23 @@
55

66
from blinkpy import blinkpy
77
from blinkpy.sync_module import BlinkSyncModule
8+
from blinkpy.camera import BlinkCamera
89
from blinkpy.helpers.util import get_time, BlinkURLHandler
910

1011

1112
class MockSyncModule(BlinkSyncModule):
12-
"""Mock http requests from sync module."""
13+
"""Mock blink sync module object."""
1314

14-
def __init__(self, blink, header):
15-
"""Create mock sync module instance."""
16-
super().__init__(blink, header, network_id=None, camera_list=None)
17-
self.blink = blink
18-
self.header = header
19-
self.return_value = None
20-
self.return_value2 = None
15+
def get_network_info(self):
16+
"""Mock network info method."""
17+
return True
2118

22-
def http_get(self, url, stream=False, json=True):
23-
"""Mock get request."""
24-
if stream and self.return_value2 is not None:
25-
return self.return_value2
26-
return self.return_value
2719

28-
def http_post(self, url):
29-
"""Mock post request."""
30-
return self.return_value
20+
class MockCamera(BlinkCamera):
21+
"""Mock blink camera object."""
22+
23+
def update(self, config, force_cache=False, **kwargs):
24+
"""Mock camera update method."""
3125

3226

3327
class TestBlinkFunctions(unittest.TestCase):
@@ -121,3 +115,16 @@ def test_parse_camera_not_in_list(self, mock_req):
121115
with self.assertLogs() as dl_log:
122116
blink.download_videos("/tmp", camera="bar", stop=2)
123117
self.assertEqual(dl_log.output, expected_log)
118+
119+
@mock.patch("blinkpy.blinkpy.api.request_network_update")
120+
@mock.patch("blinkpy.auth.Auth.query")
121+
def test_refresh(self, mock_req, mock_update):
122+
"""Test ability to refresh system."""
123+
mock_update.return_value = {"network": {"sync_module_error": False}}
124+
mock_req.return_value = None
125+
self.blink.last_refresh = 0
126+
self.blink.available = True
127+
self.blink.sync["foo"] = MockSyncModule(self.blink, "foo", 1, [])
128+
self.blink.cameras = {"bar": MockCamera(self.blink.sync)}
129+
self.blink.sync["foo"].cameras = self.blink.cameras
130+
self.assertTrue(self.blink.refresh())

tests/test_blinkpy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_throttle(self, mock_time):
6969
with mock.patch(
7070
"blinkpy.sync_module.BlinkSyncModule.refresh", return_value=True
7171
), mock.patch("blinkpy.blinkpy.Blink.get_homescreen", return_value=True):
72-
self.blink.refresh()
72+
self.blink.refresh(force=True)
7373

7474
self.assertEqual(self.blink.last_refresh, now)
7575
self.assertEqual(self.blink.check_if_ok_to_update(), False)

0 commit comments

Comments
 (0)