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 * Lintpull/8055/head
parent
363a429c41
commit
a2fbc0d2ef
|
@ -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 = []
|
||||
|
||||
|
|
|
@ -742,7 +742,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
|
||||
|
|
|
@ -100,6 +100,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
|
||||
|
||||
|
|
|
@ -67,6 +67,7 @@ TEST_REQUIREMENTS = (
|
|||
'pywebpush',
|
||||
'PyJWT',
|
||||
'restrictedpython',
|
||||
'pyunifi',
|
||||
)
|
||||
|
||||
IGNORE_PACKAGES = (
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue