Ejemplo n.º 1
0
async def test_controller_setup():
    """Successful setup."""
    hass = Mock()
    hass.data = {UNIFI_CONFIG: {}}
    entry = Mock()
    entry.data = ENTRY_CONFIG
    api = Mock()
    api.initialize.return_value = mock_coro(True)
    api.sites.return_value = mock_coro(CONTROLLER_SITES)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller,
                      "get_controller",
                      return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert unifi_controller.api is api
    assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 2
    assert hass.config_entries.async_forward_entry_setup.mock_calls[0][1] == (
        entry,
        "device_tracker",
    )
    assert hass.config_entries.async_forward_entry_setup.mock_calls[1][1] == (
        entry,
        "switch",
    )
Ejemplo n.º 2
0
async def test_reset_unloads_entry_if_setup():
    """Calling reset when the entry has been setup."""
    hass = Mock()
    hass.data = {UNIFI_CONFIG: {}}
    entry = Mock()
    entry.data = ENTRY_CONFIG
    entry.options = {}
    api = Mock()
    api.initialize.return_value = mock_coro(True)
    api.sites.return_value = mock_coro(CONTROLLER_SITES)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller,
                      "get_controller",
                      return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 2

    hass.config_entries.async_forward_entry_unload.return_value = mock_coro(
        True)
    assert await unifi_controller.async_reset()

    assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 2
Ejemplo n.º 3
0
async def test_controller_host():
    """Config entry host and controller host are the same."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG

    unifi_controller = controller.UniFiController(hass, entry)

    assert unifi_controller.host == CONTROLLER_DATA[CONF_HOST]
Ejemplo n.º 4
0
async def test_controller_site():
    """Config entry site and controller site are the same."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG

    unifi_controller = controller.UniFiController(hass, entry)

    assert unifi_controller.site == CONTROLLER_DATA[CONF_SITE_ID]
Ejemplo n.º 5
0
async def test_controller_not_accessible():
    """Retry to login gets scheduled when connection fails."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG
    api = Mock()
    api.initialize.return_value = mock_coro(True)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(
        controller, "get_controller", side_effect=errors.CannotConnect
    ), pytest.raises(ConfigEntryNotReady):
        await unifi_controller.async_setup()
Ejemplo n.º 6
0
async def test_controller_unknown_error():
    """Unknown errors are handled."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG
    api = Mock()
    api.initialize.return_value = mock_coro(True)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, "get_controller", side_effect=Exception):
        assert await unifi_controller.async_setup() is False

    assert not hass.helpers.event.async_call_later.mock_calls
Ejemplo n.º 7
0
async def test_reset_if_entry_had_wrong_auth():
    """Calling reset when the entry contains wrong auth."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, 'get_controller',
                      side_effect=errors.AuthenticationRequired):
        assert await unifi_controller.async_setup() is False

    assert not hass.async_add_job.mock_calls

    assert await unifi_controller.async_reset()
Ejemplo n.º 8
0
async def test_controller_setup():
    """Successful setup."""
    hass = Mock()
    hass.data = {
        UNIFI_CONFIG: [{
            CONF_HOST: CONTROLLER_DATA[CONF_HOST],
            CONF_SITE_ID: "nice name",
            controller.CONF_BLOCK_CLIENT: ["mac"],
            controller.CONF_DONT_TRACK_CLIENTS: True,
            controller.CONF_DONT_TRACK_DEVICES: True,
            controller.CONF_DONT_TRACK_WIRED_CLIENTS: True,
            controller.CONF_DETECTION_TIME: 30,
            controller.CONF_SSID_FILTER: ["ssid"],
        }],
        UNIFI_WIRELESS_CLIENTS:
        Mock(),
    }
    entry = Mock()
    entry.data = ENTRY_CONFIG
    entry.options = {}
    api = Mock()
    api.initialize.return_value = mock_coro(True)
    api.sites.return_value = mock_coro(CONTROLLER_SITES)
    api.clients = []

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller,
                      "get_controller",
                      return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert unifi_controller.api is api
    assert len(
        hass.config_entries.async_forward_entry_setup.mock_calls) == len(
            controller.SUPPORTED_PLATFORMS)
    assert hass.config_entries.async_forward_entry_setup.mock_calls[0][1] == (
        entry,
        "device_tracker",
    )
    assert hass.config_entries.async_forward_entry_setup.mock_calls[1][1] == (
        entry,
        "sensor",
    )
    assert hass.config_entries.async_forward_entry_setup.mock_calls[2][1] == (
        entry,
        "switch",
    )
Ejemplo n.º 9
0
async def test_controller_not_accessible():
    """Retry to login gets scheduled when connection fails."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG
    api = Mock()
    api.initialize.return_value = mock_coro(True)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, 'get_controller',
                      side_effect=errors.CannotConnect):
        assert await unifi_controller.async_setup() is False

    assert len(hass.helpers.event.async_call_later.mock_calls) == 1
    # Assert we are going to wait 2 seconds
    assert hass.helpers.event.async_call_later.mock_calls[0][1][0] == 2
Ejemplo n.º 10
0
async def test_controller_no_mac():
    """Test that it works to not find the controllers mac."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG
    client = Mock()
    client.ip = '5.6.7.8'
    api = Mock()
    api.initialize.return_value = mock_coro(True)
    api.clients = {'client1': client}

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, 'get_controller',
                      return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert unifi_controller.mac is None
Ejemplo n.º 11
0
async def test_controller_setup():
    """Successful setup."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG
    api = Mock()
    api.initialize.return_value = mock_coro(True)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, 'get_controller',
                      return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert unifi_controller.api is api
    assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 1
    assert hass.config_entries.async_forward_entry_setup.mock_calls[0][1] == \
        (entry, 'switch')
Ejemplo n.º 12
0
async def test_controller_mac():
    """Test that it is possible to identify controller mac."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG
    client = Mock()
    client.ip = '1.2.3.4'
    client.mac = '00:11:22:33:44:55'
    api = Mock()
    api.initialize.return_value = mock_coro(True)
    api.clients = {'client1': client}

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, 'get_controller',
                      return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert unifi_controller.mac == '00:11:22:33:44:55'
Ejemplo n.º 13
0
async def test_reset_cancels_retry_setup():
    """Resetting a controller while we're waiting to retry setup."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, 'get_controller',
                      side_effect=errors.CannotConnect):
        assert await unifi_controller.async_setup() is False

    mock_call_later = hass.helpers.event.async_call_later

    assert len(mock_call_later.mock_calls) == 1

    assert await unifi_controller.async_reset()

    assert len(mock_call_later.mock_calls) == 2
    assert len(mock_call_later.return_value.mock_calls) == 1
Ejemplo n.º 14
0
async def test_controller_no_mac():
    """Test that it works to not find the controllers mac."""
    hass = Mock()
    hass.data = {UNIFI_CONFIG: {}}
    entry = Mock()
    entry.data = ENTRY_CONFIG
    entry.options = {}
    client = Mock()
    client.ip = "5.6.7.8"
    api = Mock()
    api.initialize.return_value = mock_coro(True)
    api.clients = {"client1": client}
    api.sites.return_value = mock_coro(CONTROLLER_SITES)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, "get_controller", return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert unifi_controller.mac is None
Ejemplo n.º 15
0
async def test_reset_unloads_entry_if_setup():
    """Calling reset when the entry has been setup."""
    hass = Mock()
    entry = Mock()
    entry.data = ENTRY_CONFIG
    api = Mock()
    api.initialize.return_value = mock_coro(True)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, 'get_controller',
                      return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 1

    hass.config_entries.async_forward_entry_unload.return_value = \
        mock_coro(True)
    assert await unifi_controller.async_reset()

    assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 1
Ejemplo n.º 16
0
async def test_controller_mac():
    """Test that it is possible to identify controller mac."""
    hass = Mock()
    hass.data = {UNIFI_CONFIG: {}}
    entry = Mock()
    entry.data = ENTRY_CONFIG
    entry.options = {}
    client = Mock()
    client.ip = "1.2.3.4"
    client.mac = "00:11:22:33:44:55"
    api = Mock()
    api.initialize.return_value = mock_coro(True)
    api.clients = {"client1": client}
    api.sites.return_value = mock_coro(CONTROLLER_SITES)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, "get_controller", return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert unifi_controller.mac == "00:11:22:33:44:55"
Ejemplo n.º 17
0
async def test_reset_unloads_entry_without_poe_control():
    """Calling reset while the entry has been setup."""
    hass = Mock()
    entry = Mock()
    entry.data = dict(ENTRY_CONFIG)
    entry.data[unifi.CONF_POE_CONTROL] = False
    api = Mock()
    api.initialize.return_value = mock_coro(True)

    unifi_controller = controller.UniFiController(hass, entry)

    with patch.object(controller, 'get_controller',
                      return_value=mock_coro(api)):
        assert await unifi_controller.async_setup() is True

    assert not hass.config_entries.async_forward_entry_setup.mock_calls

    hass.config_entries.async_forward_entry_unload.return_value = \
        mock_coro(True)
    assert await unifi_controller.async_reset()

    assert not hass.config_entries.async_forward_entry_unload.mock_calls