Add password parameter to uvc component (#7499)

This commit is contained in:
Nuno Sousa 2017-05-10 05:54:38 +01:00 committed by Paulus Schoutsen
parent b30c352e37
commit 89d950c73a
2 changed files with 20 additions and 34 deletions

View File

@ -20,12 +20,15 @@ _LOGGER = logging.getLogger(__name__)
CONF_NVR = 'nvr'
CONF_KEY = 'key'
CONF_PASSWORD = 'password'
DEFAULT_PASSWORD = 'ubnt'
DEFAULT_PORT = 7080
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_NVR): cv.string,
vol.Required(CONF_KEY): cv.string,
vol.Optional(CONF_PASSWORD, default=DEFAULT_PASSWORD): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
})
@ -34,6 +37,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
"""Discover cameras on a Unifi NVR."""
addr = config[CONF_NVR]
key = config[CONF_KEY]
password = config[CONF_PASSWORD]
port = config[CONF_PORT]
from uvcclient import nvr
@ -59,7 +63,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
add_devices([UnifiVideoCamera(nvrconn,
camera[identifier],
camera['name'])
camera['name'],
password)
for camera in cameras])
return True
@ -67,12 +72,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class UnifiVideoCamera(Camera):
"""A Ubiquiti Unifi Video Camera."""
def __init__(self, nvr, uuid, name):
def __init__(self, nvr, uuid, name, password):
"""Initialize an Unifi camera."""
super(UnifiVideoCamera, self).__init__()
self._nvr = nvr
self._uuid = uuid
self._name = name
self._password = password
self.is_streaming = False
self._connect_addr = None
self._camera = None
@ -102,7 +108,6 @@ class UnifiVideoCamera(Camera):
def _login(self):
"""Login to the camera."""
from uvcclient import camera as uvc_camera
from uvcclient import store as uvc_store
caminfo = self._nvr.get_camera(self._uuid)
if self._connect_addr:
@ -110,13 +115,6 @@ class UnifiVideoCamera(Camera):
else:
addrs = [caminfo['host'], caminfo['internalHost']]
store = uvc_store.get_info_store()
password = store.get_camera_password(self._uuid)
if password is None:
_LOGGER.debug("Logging into camera %(name)s with default password",
dict(name=self._name))
password = 'ubnt'
if self._nvr.server_version >= (3, 2, 0):
client_cls = uvc_camera.UVCCameraClientV320
else:
@ -126,7 +124,7 @@ class UnifiVideoCamera(Camera):
for addr in addrs:
try:
camera = client_cls(
addr, caminfo['username'], password)
addr, caminfo['username'], self._password)
camera.login()
_LOGGER.debug("Logged into UVC camera %(name)s via %(addr)s",
dict(name=self._name, addr=addr))

View File

@ -31,6 +31,7 @@ class TestUVCSetup(unittest.TestCase):
config = {
'platform': 'uvc',
'nvr': 'foo',
'password': 'bar',
'port': 123,
'key': 'secret',
}
@ -58,8 +59,8 @@ class TestUVCSetup(unittest.TestCase):
mock_remote.call_args, mock.call('foo', 123, 'secret')
)
mock_uvc.assert_has_calls([
mock.call(mock_remote.return_value, 'id1', 'Front'),
mock.call(mock_remote.return_value, 'id2', 'Back'),
mock.call(mock_remote.return_value, 'id1', 'Front', 'bar'),
mock.call(mock_remote.return_value, 'id2', 'Back', 'bar'),
])
@mock.patch('uvcclient.nvr.UVCRemote')
@ -86,8 +87,8 @@ class TestUVCSetup(unittest.TestCase):
mock_remote.call_args, mock.call('foo', 7080, 'secret')
)
mock_uvc.assert_has_calls([
mock.call(mock_remote.return_value, 'id1', 'Front'),
mock.call(mock_remote.return_value, 'id2', 'Back'),
mock.call(mock_remote.return_value, 'id1', 'Front', 'ubnt'),
mock.call(mock_remote.return_value, 'id2', 'Back', 'ubnt'),
])
@mock.patch('uvcclient.nvr.UVCRemote')
@ -114,8 +115,8 @@ class TestUVCSetup(unittest.TestCase):
mock_remote.call_args, mock.call('foo', 7080, 'secret')
)
mock_uvc.assert_has_calls([
mock.call(mock_remote.return_value, 'one', 'Front'),
mock.call(mock_remote.return_value, 'two', 'Back'),
mock.call(mock_remote.return_value, 'one', 'Front', 'ubnt'),
mock.call(mock_remote.return_value, 'two', 'Back', 'ubnt'),
])
@mock.patch.object(uvc, 'UnifiVideoCamera')
@ -156,7 +157,9 @@ class TestUVC(unittest.TestCase):
self.nvr = mock.MagicMock()
self.uuid = 'uuid'
self.name = 'name'
self.uvc = uvc.UnifiVideoCamera(self.nvr, self.uuid, self.name)
self.password = 'seekret'
self.uvc = uvc.UnifiVideoCamera(self.nvr, self.uuid, self.name,
self.password)
self.nvr.get_camera.return_value = {
'model': 'UVC Fake',
'recordingSettings': {
@ -179,7 +182,6 @@ class TestUVC(unittest.TestCase):
@mock.patch('uvcclient.camera.UVCCameraClientV320')
def test_login(self, mock_camera, mock_store):
""""Test the login."""
mock_store.return_value.get_camera_password.return_value = 'seekret'
self.uvc._login()
self.assertEqual(mock_camera.call_count, 1)
self.assertEqual(
@ -192,7 +194,6 @@ class TestUVC(unittest.TestCase):
@mock.patch('uvcclient.camera.UVCCameraClient')
def test_login_v31x(self, mock_camera, mock_store):
"""Test login with v3.1.x server."""
mock_store.return_value.get_camera_password.return_value = 'seekret'
self.nvr.server_version = (3, 1, 3)
self.uvc._login()
self.assertEqual(mock_camera.call_count, 1)
@ -202,19 +203,6 @@ class TestUVC(unittest.TestCase):
self.assertEqual(mock_camera.return_value.login.call_count, 1)
self.assertEqual(mock_camera.return_value.login.call_args, mock.call())
@mock.patch('uvcclient.store.get_info_store')
@mock.patch('uvcclient.camera.UVCCameraClientV320')
def test_login_no_password(self, mock_camera, mock_store):
""""Test the login with no password."""
mock_store.return_value.get_camera_password.return_value = None
self.uvc._login()
self.assertEqual(mock_camera.call_count, 1)
self.assertEqual(
mock_camera.call_args, mock.call('host-a', 'admin', 'ubnt')
)
self.assertEqual(mock_camera.return_value.login.call_count, 1)
self.assertEqual(mock_camera.return_value.login.call_args, mock.call())
@mock.patch('uvcclient.store.get_info_store')
@mock.patch('uvcclient.camera.UVCCameraClientV320')
def test_login_tries_both_addrs_and_caches(self, mock_camera, mock_store):
@ -239,7 +227,7 @@ class TestUVC(unittest.TestCase):
self.uvc._login()
self.assertEqual(mock_camera.call_count, 1)
self.assertEqual(
mock_camera.call_args, mock.call('host-b', 'admin', 'ubnt')
mock_camera.call_args, mock.call('host-b', 'admin', 'seekret')
)
self.assertEqual(mock_camera.return_value.login.call_count, 1)
self.assertEqual(mock_camera.return_value.login.call_args, mock.call())