Update pyunifi component to use APIError passed from pyunifi 2.13. Better accommodate login failures with wrapper in pyunifi 2.13. (#7899)

* Pyunifi update

* Update pyunifi_test

* Import API Error

* Adjust test_unifi.py to import APIError

* Remove urllib import

* Remove urllib import from test

* Try fix mock

* Remove automations.yaml

* Lint
pull/8055/head
Caleb 2017-06-17 13:09:27 -05:00 committed by Paulus Schoutsen
parent 363a429c41
commit a2fbc0d2ef
5 changed files with 15 additions and 14 deletions

View File

@ -5,7 +5,6 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.unifi/ https://home-assistant.io/components/device_tracker.unifi/
""" """
import logging import logging
import urllib
import voluptuous as vol import voluptuous as vol
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -15,7 +14,7 @@ from homeassistant.components.device_tracker import (
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD
from homeassistant.const import CONF_VERIFY_SSL from homeassistant.const import CONF_VERIFY_SSL
REQUIREMENTS = ['pyunifi==2.12'] REQUIREMENTS = ['pyunifi==2.13']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_PORT = 'port' CONF_PORT = 'port'
@ -40,7 +39,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def get_scanner(hass, config): def get_scanner(hass, config):
"""Set up the Unifi device_tracker.""" """Set up the Unifi device_tracker."""
from pyunifi.controller import Controller from pyunifi.controller import Controller, APIError
host = config[DOMAIN].get(CONF_HOST) host = config[DOMAIN].get(CONF_HOST)
username = config[DOMAIN].get(CONF_USERNAME) username = config[DOMAIN].get(CONF_USERNAME)
@ -53,7 +52,7 @@ def get_scanner(hass, config):
try: try:
ctrl = Controller(host, username, password, port, version='v4', ctrl = Controller(host, username, password, port, version='v4',
site_id=site_id, ssl_verify=verify_ssl) site_id=site_id, ssl_verify=verify_ssl)
except urllib.error.HTTPError as ex: except APIError as ex:
_LOGGER.error("Failed to connect to Unifi: %s", ex) _LOGGER.error("Failed to connect to Unifi: %s", ex)
persistent_notification.create( persistent_notification.create(
hass, 'Failed to connect to Unifi. ' hass, 'Failed to connect to Unifi. '
@ -77,9 +76,10 @@ class UnifiScanner(DeviceScanner):
def _update(self): def _update(self):
"""Get the clients from the device.""" """Get the clients from the device."""
from pyunifi.controller import APIError
try: try:
clients = self._controller.get_clients() clients = self._controller.get_clients()
except urllib.error.HTTPError as ex: except APIError as ex:
_LOGGER.error("Failed to scan clients: %s", ex) _LOGGER.error("Failed to scan clients: %s", ex)
clients = [] clients = []

View File

@ -742,7 +742,7 @@ pytrackr==0.0.5
pytradfri==1.1 pytradfri==1.1
# homeassistant.components.device_tracker.unifi # homeassistant.components.device_tracker.unifi
pyunifi==2.12 pyunifi==2.13
# homeassistant.components.keyboard # homeassistant.components.keyboard
# pyuserinput==0.1.11 # pyuserinput==0.1.11

View File

@ -100,6 +100,9 @@ pynx584==0.4
# homeassistant.components.sensor.darksky # homeassistant.components.sensor.darksky
python-forecastio==1.3.5 python-forecastio==1.3.5
# homeassistant.components.device_tracker.unifi
pyunifi==2.13
# homeassistant.components.notify.html5 # homeassistant.components.notify.html5
pywebpush==1.0.4 pywebpush==1.0.4

View File

@ -67,6 +67,7 @@ TEST_REQUIREMENTS = (
'pywebpush', 'pywebpush',
'PyJWT', 'PyJWT',
'restrictedpython', 'restrictedpython',
'pyunifi',
) )
IGNORE_PACKAGES = ( IGNORE_PACKAGES = (

View File

@ -1,6 +1,6 @@
"""The tests for the Unifi WAP device tracker platform.""" """The tests for the Unifi WAP device tracker platform."""
from unittest import mock from unittest import mock
import urllib from pyunifi.controller import APIError
import pytest import pytest
import voluptuous as vol import voluptuous as vol
@ -13,11 +13,8 @@ from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD,
@pytest.fixture @pytest.fixture
def mock_ctrl(): def mock_ctrl():
"""Mock pyunifi.""" """Mock pyunifi."""
module = mock.MagicMock() with mock.patch('pyunifi.controller.Controller') as mock_control:
with mock.patch.dict('sys.modules', { yield mock_control
'pyunifi.controller': module.controller,
}):
yield module.controller.Controller
@pytest.fixture @pytest.fixture
@ -100,7 +97,7 @@ def test_config_controller_failed(hass, mock_ctrl, mock_scanner):
CONF_PASSWORD: 'password', CONF_PASSWORD: 'password',
} }
} }
mock_ctrl.side_effect = urllib.error.HTTPError( mock_ctrl.side_effect = APIError(
'/', 500, 'foo', {}, None) '/', 500, 'foo', {}, None)
result = unifi.get_scanner(hass, config) result = unifi.get_scanner(hass, config)
assert result is False assert result is False
@ -122,7 +119,7 @@ def test_scanner_update():
def test_scanner_update_error(): def test_scanner_update_error():
"""Test the scanner update for error.""" """Test the scanner update for error."""
ctrl = mock.MagicMock() ctrl = mock.MagicMock()
ctrl.get_clients.side_effect = urllib.error.HTTPError( ctrl.get_clients.side_effect = APIError(
'/', 500, 'foo', {}, None) '/', 500, 'foo', {}, None)
unifi.UnifiScanner(ctrl) unifi.UnifiScanner(ctrl)