Skip to content

Commit

Permalink
Merge pull request #191 from fronzbot/0.14.1
Browse files Browse the repository at this point in the history
0.14.1
  • Loading branch information
fronzbot authored Jun 20, 2019
2 parents f4480da + 6c8fc0a commit 92dda40
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 73 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Changelog

A list of changes between each release

0.14.1 (2019-06-20)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Fix timeout problems blocking blinkpy startup
- Updated login urls using ``rest-region`` subdomain
- Removed deprecated thumbanil recovery from homescreen

0.14.0 (2019-05-23)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
**Breaking Changes:**
Expand Down
14 changes: 7 additions & 7 deletions blinkpy/blinkpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(self, username=None, password=None,
self.cameras = CaseInsensitiveDict({})
self.video_list = CaseInsensitiveDict({})
self._login_url = LOGIN_URL
self.login_urls = []
self.motion_interval = motion_interval
self.version = __version__
self.legacy = legacy_subdomain
Expand Down Expand Up @@ -131,9 +132,9 @@ def get_auth_token(self, is_retry=False):
if not isinstance(self._password, str):
raise BlinkAuthenticationException(ERROR.PASSWORD)

login_urls = [LOGIN_URL, OLD_LOGIN_URL, LOGIN_BACKUP_URL]
self.login_urls = [LOGIN_URL, OLD_LOGIN_URL, LOGIN_BACKUP_URL]

response = self.login_request(login_urls, is_retry=is_retry)
response = self.login_request(is_retry=is_retry)

if not response:
return False
Expand All @@ -148,10 +149,10 @@ def get_auth_token(self, is_retry=False):

return self._auth_header

def login_request(self, login_urls, is_retry=False):
def login_request(self, is_retry=False):
"""Make a login request."""
try:
login_url = login_urls.pop(0)
login_url = self.login_urls.pop(0)
except IndexError:
_LOGGER.error("Could not login to blink servers.")
return False
Expand All @@ -165,14 +166,13 @@ def login_request(self, login_urls, is_retry=False):
is_retry=is_retry)
try:
if response.status_code != 200:
response = self.login_request(login_urls)
response = self.login_request(is_retry=True)
response = response.json()
(self.region_id, self.region), = response['region'].items()

except AttributeError:
_LOGGER.error("Login API endpoint failed with response %s",
response,
exc_info=True)
response)
return False

except KeyError:
Expand Down
23 changes: 5 additions & 18 deletions blinkpy/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@ def update(self, config, force_cache=False, **kwargs):
_LOGGER.warning("Could not retrieve calibrated temperature.")

# Check if thumbnail exists in config, if not try to
# get it from the homescreen info in teh sync module
# get it from the homescreen info in the sync module
# otherwise set it to None and log an error
new_thumbnail = None
thumb_addr = None
if config['thumbnail']:
thumb_addr = config['thumbnail']
else:
thumb_addr = self.get_thumb_from_homescreen()
_LOGGER.warning("Could not find thumbnail for camera %s",
self.name,
exc_info=True)

if thumb_addr is not None:
new_thumbnail = "{}{}.jpg".format(self.sync.urls.base_url,
Expand Down Expand Up @@ -192,19 +195,3 @@ def video_to_file(self, path):
return
with open(path, 'wb') as vidfile:
copyfileobj(response.raw, vidfile)

def get_thumb_from_homescreen(self):
"""Retrieve thumbnail from homescreen."""
for device in self.sync.homescreen['devices']:
try:
device_type = device['device_type']
device_name = device['name']
device_thumb = device['thumbnail']
if device_type == 'camera' and device_name == self.name:
return device_thumb
except KeyError:
pass
_LOGGER.error("Could not find thumbnail for camera %s",
self.name,
exc_info=True)
return None
7 changes: 4 additions & 3 deletions blinkpy/helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

MAJOR_VERSION = 0
MINOR_VERSION = 14
PATCH_VERSION = 0
PATCH_VERSION = 1

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

Expand Down Expand Up @@ -33,6 +33,7 @@
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Topic :: Home Automation'
]

Expand All @@ -45,11 +46,11 @@
URLS
'''
BLINK_URL = 'immedia-semi.com'
DEFAULT_URL = "{}.{}".format('prod', BLINK_URL)
DEFAULT_URL = "{}.{}".format('rest-prod', BLINK_URL)
BASE_URL = "https://{}".format(DEFAULT_URL)
LOGIN_URL = "{}/api/v2/login".format(BASE_URL)
OLD_LOGIN_URL = "{}/login".format(BASE_URL)
LOGIN_BACKUP_URL = "https://{}.{}/login".format('rest.piri', BLINK_URL)
LOGIN_BACKUP_URL = "https://{}.{}/login".format('rest-piri', BLINK_URL)

'''
Dictionaries
Expand Down
12 changes: 9 additions & 3 deletions blinkpy/helpers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
import time
from functools import wraps
from functools import partial, wraps
from requests import Request, Session, exceptions
from blinkpy.helpers.constants import BLINK_URL, TIMESTAMP_FORMAT
import blinkpy.helpers.errors as ERROR
Expand All @@ -28,8 +28,14 @@ def merge_dicts(dict_a, dict_b):


def create_session():
"""Create a session for blink communication."""
"""
Create a session for blink communication.
From @ericfrederich via
https://github.com/kennethreitz/requests/issues/2011
"""
sess = Session()
sess.get = partial(sess.get, timeout=5)
return sess


Expand Down Expand Up @@ -65,7 +71,7 @@ def http_req(blink, url='http://example.com', data=None, headers=None,
prepped = req.prepare()

try:
response = blink.session.send(prepped, stream=stream, timeout=10)
response = blink.session.send(prepped, stream=stream)
if json_resp and 'code' in response.json():
if is_retry:
_LOGGER.error("Cannot obtain new token for server auth.")
Expand Down
6 changes: 4 additions & 2 deletions tests/test_blink_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def test_backup_url(self, req, mock_sess):
bad_req,
new_req
]
self.blink.login_request(['test1', 'test2', 'test3'])
self.blink.login_urls = ['test1', 'test2', 'test3']
self.blink.login_request()
# pylint: disable=protected-access
self.assertEqual(self.blink._login_url, 'test3')

Expand All @@ -78,7 +79,8 @@ def test_backup_url(self, req, mock_sess):
new_req,
bad_req
]
self.blink.login_request(['test1', 'test2', 'test3'])
self.blink.login_urls = ['test1', 'test2', 'test3']
self.blink.login_request()
# pylint: disable=protected-access
self.assertEqual(self.blink._login_url, 'test2')

Expand Down
41 changes: 1 addition & 40 deletions tests/test_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,45 +99,6 @@ def test_camera_update(self, mock_sess):
self.assertEqual(self.camera.image_from_cache, 'test')
self.assertEqual(self.camera.video_from_cache, 'foobar')

def test_thumbnail_not_in_info(self, mock_sess):
"""Test that we grab thumbanil if not in camera_info."""
mock_sess.side_effect = [
mresp.MockResponse({'temp': 71}, 200),
'foobar',
'barfoo'
]
self.camera.last_record = ['1']
self.camera.sync.last_record = {
'new': {
'clip': '/test.mp4',
'time': '1970-01-01T00:00:00'
}
}
config = {
'name': 'new',
'id': 1234,
'network_id': 5678,
'serial': '12345678',
'enabled': False,
'battery_voltage': 90,
'battery_state': 'ok',
'temperature': 68,
'wifi_strength': 4,
'thumbnail': '',
}
self.camera.sync.homescreen = {
'devices': [
{'foo': 'bar'},
{'device_type': 'foobar'},
{'device_type': 'camera',
'name': 'new',
'thumbnail': '/new/thumb'}
]
}
self.camera.update(config)
self.assertEqual(self.camera.thumbnail,
'https://rest-test.immedia-semi.com/new/thumb.jpg')

def test_no_thumbnails(self, mock_sess):
"""Tests that thumbnail is 'None' if none found."""
mock_sess.return_value = 'foobar'
Expand Down Expand Up @@ -167,7 +128,7 @@ def test_no_thumbnails(self, mock_sess):
logrecord.output,
[("WARNING:blinkpy.camera:Could not retrieve calibrated "
"temperature."),
("ERROR:blinkpy.camera:Could not find thumbnail for camera new"
("WARNING:blinkpy.camera:Could not find thumbnail for camera new"
"\nNoneType: None")]
)

Expand Down

0 comments on commit 92dda40

Please sign in to comment.