mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Added persistent_notification in case of error during Unifi device_tracker setup (#4682)
This commit is contained in:
parent
c25aa56751
commit
53c1b93b61
@ -9,6 +9,7 @@ import urllib
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant.components.device_tracker import DOMAIN, PLATFORM_SCHEMA
|
||||
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD
|
||||
|
||||
@ -19,6 +20,9 @@ _LOGGER = logging.getLogger(__name__)
|
||||
CONF_PORT = 'port'
|
||||
CONF_SITE_ID = 'site_id'
|
||||
|
||||
NOTIFICATION_ID = 'unifi_notification'
|
||||
NOTIFICATION_TITLE = 'Unifi Device Tracker Setup'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_HOST, default='localhost'): cv.string,
|
||||
vol.Optional(CONF_SITE_ID, default='default'): cv.string,
|
||||
@ -38,10 +42,18 @@ def get_scanner(hass, config):
|
||||
site_id = config[DOMAIN].get(CONF_SITE_ID)
|
||||
port = config[DOMAIN].get(CONF_PORT)
|
||||
|
||||
persistent_notification = loader.get_component('persistent_notification')
|
||||
try:
|
||||
ctrl = Controller(host, username, password, port, 'v4', site_id)
|
||||
except urllib.error.HTTPError as ex:
|
||||
_LOGGER.error('Failed to connect to unifi: %s', ex)
|
||||
_LOGGER.error('Failed to connect to Unifi: %s', ex)
|
||||
persistent_notification.create(
|
||||
hass, 'Failed to connect to Unifi. '
|
||||
'Error: {}<br />'
|
||||
'You will need to restart hass after fixing.'
|
||||
''.format(ex),
|
||||
title=NOTIFICATION_TITLE,
|
||||
notification_id=NOTIFICATION_ID)
|
||||
return False
|
||||
|
||||
return UnifiScanner(ctrl)
|
||||
|
@ -6,6 +6,7 @@ import urllib
|
||||
from unifi import controller
|
||||
import voluptuous as vol
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from homeassistant.components.device_tracker import DOMAIN, unifi as unifi
|
||||
from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD,
|
||||
CONF_PLATFORM)
|
||||
@ -14,6 +15,14 @@ from homeassistant.const import (CONF_HOST, CONF_USERNAME, CONF_PASSWORD,
|
||||
class TestUnifiScanner(unittest.TestCase):
|
||||
"""Test the Unifiy platform."""
|
||||
|
||||
def setUp(self):
|
||||
"""Initialize values for this testcase class."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
|
||||
@mock.patch.object(controller, 'Controller')
|
||||
def test_config_minimal(self, mock_ctrl, mock_scanner):
|
||||
@ -25,7 +34,7 @@ class TestUnifiScanner(unittest.TestCase):
|
||||
CONF_PASSWORD: 'password',
|
||||
})
|
||||
}
|
||||
result = unifi.get_scanner(None, config)
|
||||
result = unifi.get_scanner(self.hass, config)
|
||||
self.assertEqual(mock_scanner.return_value, result)
|
||||
self.assertEqual(mock_ctrl.call_count, 1)
|
||||
self.assertEqual(
|
||||
@ -52,7 +61,7 @@ class TestUnifiScanner(unittest.TestCase):
|
||||
'site_id': 'abcdef01',
|
||||
})
|
||||
}
|
||||
result = unifi.get_scanner(None, config)
|
||||
result = unifi.get_scanner(self.hass, config)
|
||||
self.assertEqual(mock_scanner.return_value, result)
|
||||
self.assertEqual(mock_ctrl.call_count, 1)
|
||||
self.assertEqual(
|
||||
@ -96,7 +105,7 @@ class TestUnifiScanner(unittest.TestCase):
|
||||
}
|
||||
mock_ctrl.side_effect = urllib.error.HTTPError(
|
||||
'/', 500, 'foo', {}, None)
|
||||
result = unifi.get_scanner(None, config)
|
||||
result = unifi.get_scanner(self.hass, config)
|
||||
self.assertFalse(result)
|
||||
|
||||
def test_scanner_update(self): # pylint: disable=no-self-use
|
||||
|
Loading…
x
Reference in New Issue
Block a user