diff --git a/homeassistant/components/device_tracker/unifi.py b/homeassistant/components/device_tracker/unifi.py index b0409e99883..29c997b4dac 100644 --- a/homeassistant/components/device_tracker/unifi.py +++ b/homeassistant/components/device_tracker/unifi.py @@ -5,7 +5,6 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/device_tracker.unifi/ """ import logging -import urllib import voluptuous as vol 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_VERIFY_SSL -REQUIREMENTS = ['pyunifi==2.12'] +REQUIREMENTS = ['pyunifi==2.13'] _LOGGER = logging.getLogger(__name__) CONF_PORT = 'port' @@ -40,7 +39,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ def get_scanner(hass, config): """Set up the Unifi device_tracker.""" - from pyunifi.controller import Controller + from pyunifi.controller import Controller, APIError host = config[DOMAIN].get(CONF_HOST) username = config[DOMAIN].get(CONF_USERNAME) @@ -53,7 +52,7 @@ def get_scanner(hass, config): try: ctrl = Controller(host, username, password, port, version='v4', 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) persistent_notification.create( hass, 'Failed to connect to Unifi. ' @@ -77,9 +76,10 @@ class UnifiScanner(DeviceScanner): def _update(self): """Get the clients from the device.""" + from pyunifi.controller import APIError try: clients = self._controller.get_clients() - except urllib.error.HTTPError as ex: + except APIError as ex: _LOGGER.error("Failed to scan clients: %s", ex) clients = [] diff --git a/requirements_all.txt b/requirements_all.txt index 9319b231b06..28ac02167ab 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -740,7 +740,7 @@ pytrackr==0.0.5 pytradfri==1.1 # homeassistant.components.device_tracker.unifi -pyunifi==2.12 +pyunifi==2.13 # homeassistant.components.keyboard # pyuserinput==0.1.11 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index d1b226eda3e..ca78aa345ff 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -101,6 +101,9 @@ pynx584==0.4 # homeassistant.components.sensor.darksky python-forecastio==1.3.5 +# homeassistant.components.device_tracker.unifi +pyunifi==2.13 + # homeassistant.components.notify.html5 pywebpush==1.0.4 diff --git a/script/gen_requirements_all.py b/script/gen_requirements_all.py index 833c351b750..92617a4ad60 100755 --- a/script/gen_requirements_all.py +++ b/script/gen_requirements_all.py @@ -67,6 +67,7 @@ TEST_REQUIREMENTS = ( 'pywebpush', 'PyJWT', 'restrictedpython', + 'pyunifi', ) IGNORE_PACKAGES = ( diff --git a/tests/components/device_tracker/test_unifi.py b/tests/components/device_tracker/test_unifi.py index eea52637241..d62897a86c4 100644 --- a/tests/components/device_tracker/test_unifi.py +++ b/tests/components/device_tracker/test_unifi.py @@ -1,6 +1,6 @@ """The tests for the Unifi WAP device tracker platform.""" from unittest import mock -import urllib +from pyunifi.controller import APIError import pytest import voluptuous as vol @@ -13,11 +13,8 @@ from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD, @pytest.fixture def mock_ctrl(): """Mock pyunifi.""" - module = mock.MagicMock() - with mock.patch.dict('sys.modules', { - 'pyunifi.controller': module.controller, - }): - yield module.controller.Controller + with mock.patch('pyunifi.controller.Controller') as mock_control: + yield mock_control @pytest.fixture @@ -100,7 +97,7 @@ def test_config_controller_failed(hass, mock_ctrl, mock_scanner): CONF_PASSWORD: 'password', } } - mock_ctrl.side_effect = urllib.error.HTTPError( + mock_ctrl.side_effect = APIError( '/', 500, 'foo', {}, None) result = unifi.get_scanner(hass, config) assert result is False @@ -122,7 +119,7 @@ def test_scanner_update(): def test_scanner_update_error(): """Test the scanner update for error.""" ctrl = mock.MagicMock() - ctrl.get_clients.side_effect = urllib.error.HTTPError( + ctrl.get_clients.side_effect = APIError( '/', 500, 'foo', {}, None) unifi.UnifiScanner(ctrl)