Ejemplo n.º 1
0
async def test_install_addon_failure(opp, supervisor, addon_installed,
                                     install_addon):
    """Test add-on install failure."""
    addon_installed.return_value["version"] = None
    install_addon.side_effect = OppioAPIError()
    await setup.async_setup_component(opp, "persistent_notification", {})

    result = await opp.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER})

    assert result["type"] == "form"
    assert result["step_id"] == "on_supervisor"

    result = await opp.config_entries.flow.async_configure(
        result["flow_id"], {"use_addon": True})

    assert result["type"] == "progress"

    # Make sure the flow continues when the progress task is done.
    await opp.async_block_till_done()

    result = await opp.config_entries.flow.async_configure(result["flow_id"])

    assert install_addon.call_args == call(opp, "core_zwave_js")

    assert result["type"] == "abort"
    assert result["reason"] == "addon_install_failed"
Ejemplo n.º 2
0
async def test_oppio_discovery_startup_done(opp, aioclient_mock, oppio_client):
    """Test startup and discovery with opp.discovery."""
    aioclient_mock.post(
        "http://127.0.0.1/supervisor/options",
        json={"result": "ok", "data": {}},
    )
    aioclient_mock.get(
        "http://127.0.0.1/discovery",
        json={
            "result": "ok",
            "data": {
                "discovery": [
                    {
                        "service": "mqtt",
                        "uuid": "test",
                        "addon": "mosquitto",
                        "config": {
                            "broker": "mock-broker",
                            "port": 1883,
                            "username": "******",
                            "password": "******",
                            "protocol": "3.1.1",
                        },
                    }
                ]
            },
        },
    )
    aioclient_mock.get(
        "http://127.0.0.1/addons/mosquitto/info",
        json={"result": "ok", "data": {"name": "Mosquitto Test"}},
    )

    with patch(
        "openpeerpower.components.oppio.OppIO.update_opp_api",
        return_value={"result": "ok"},
    ), patch(
        "openpeerpower.components.oppio.OppIO.get_info",
        Mock(side_effect=OppioAPIError()),
    ), patch(
        "openpeerpower.components.mqtt.config_flow.FlowHandler.async_step_oppio",
        return_value={"type": "abort"},
    ) as mock_mqtt:
        await opp.async_start()
        await async_setup_component(opp, "oppio", {})
        await opp.async_block_till_done()

        assert aioclient_mock.call_count == 2
        assert mock_mqtt.called
        mock_mqtt.assert_called_with(
            {
                "broker": "mock-broker",
                "port": 1883,
                "username": "******",
                "password": "******",
                "protocol": "3.1.1",
                "addon": "Mosquitto Test",
            }
        )
Ejemplo n.º 3
0
def oppio_env():
    """Fixture to inject oppio env."""
    with patch.dict(os.environ, {"OPPIO": "127.0.0.1"}), patch(
            "openpeerpower.components.oppio.OppIO.is_connected",
            return_value={
                "result": "ok",
                "data": {}
            },
    ), patch.dict(os.environ, {"OPPIO_TOKEN": OPPIO_TOKEN}), patch(
            "openpeerpower.components.oppio.OppIO.get_info",
            Mock(side_effect=OppioAPIError()),
    ):
        yield
Ejemplo n.º 4
0
async def test_addon_info_failure(opp, supervisor, addon_info):
    """Test add-on info failure."""
    addon_info.side_effect = OppioAPIError()
    await setup.async_setup_component(opp, "persistent_notification", {})

    result = await opp.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER})

    result = await opp.config_entries.flow.async_configure(
        result["flow_id"], {"use_addon": True})

    assert result["type"] == "abort"
    assert result["reason"] == "addon_info_failed"
Ejemplo n.º 5
0
def oppio_stubs(oppio_env, opp, opp_client, aioclient_mock):
    """Create mock oppio http client."""
    with patch(
            "openpeerpower.components.oppio.OppIO.update_opp_api",
            return_value={"result": "ok"},
    ) as opp_api, patch(
            "openpeerpower.components.oppio.OppIO.update_opp_timezone",
            return_value={"result": "ok"},
    ), patch(
            "openpeerpower.components.oppio.OppIO.get_info",
            side_effect=OppioAPIError(),
    ):
        opp.state = CoreState.starting
        opp.loop.run_until_complete(async_setup_component(opp, "oppio", {}))

    return opp_api.call_args[0][1]
Ejemplo n.º 6
0
async def test_set_addon_config_failure(opp, supervisor, addon_installed,
                                        addon_options, set_addon_options):
    """Test add-on set config failure."""
    set_addon_options.side_effect = OppioAPIError()
    await setup.async_setup_component(opp, "persistent_notification", {})

    result = await opp.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER})
    result = await opp.config_entries.flow.async_configure(
        result["flow_id"], {"use_addon": True})

    result = await opp.config_entries.flow.async_configure(
        result["flow_id"], {
            "usb_path": "/test",
            "network_key": "abc123"
        })

    assert result["type"] == "abort"
    assert result["reason"] == "addon_set_config_failed"
Ejemplo n.º 7
0
        "use_addon": True,
        "integration_created_addon": False,
    }
    assert len(mock_setup.mock_calls) == 1
    assert len(mock_setup_entry.mock_calls) == 1


@pytest.mark.parametrize(
    "discovery_info, discovery_info_side_effect, server_version_side_effect, "
    "addon_info_side_effect, abort_reason",
    [
        (
            {
                "config": ADDON_DISCOVERY_INFO
            },
            OppioAPIError(),
            None,
            None,
            "addon_get_discovery_info_failed",
        ),
        (
            {
                "config": ADDON_DISCOVERY_INFO
            },
            None,
            asyncio.TimeoutError,
            None,
            "cannot_connect",
        ),
        (
            None,
Ejemplo n.º 8
0
async def test_remove_entry(opp, addon_installed, stop_addon, create_shapshot,
                            uninstall_addon, caplog):
    """Test remove the config entry."""
    # test successful remove without created add-on
    entry = MockConfigEntry(
        domain=DOMAIN,
        title="Z-Wave JS",
        data={"integration_created_addon": False},
    )
    entry.add_to_opp(opp)
    assert entry.state is ConfigEntryState.NOT_LOADED
    assert len(opp.config_entries.async_entries(DOMAIN)) == 1

    await opp.config_entries.async_remove(entry.entry_id)

    assert entry.state is ConfigEntryState.NOT_LOADED
    assert len(opp.config_entries.async_entries(DOMAIN)) == 0

    # test successful remove with created add-on
    entry = MockConfigEntry(
        domain=DOMAIN,
        title="Z-Wave JS",
        data={"integration_created_addon": True},
    )
    entry.add_to_opp(opp)
    assert len(opp.config_entries.async_entries(DOMAIN)) == 1

    await opp.config_entries.async_remove(entry.entry_id)

    assert stop_addon.call_count == 1
    assert stop_addon.call_args == call(opp, "core_zwave_js")
    assert create_shapshot.call_count == 1
    assert create_shapshot.call_args == call(
        opp,
        {
            "name": "addon_core_zwave_js_1.0",
            "addons": ["core_zwave_js"]
        },
        partial=True,
    )
    assert uninstall_addon.call_count == 1
    assert uninstall_addon.call_args == call(opp, "core_zwave_js")
    assert entry.state is ConfigEntryState.NOT_LOADED
    assert len(opp.config_entries.async_entries(DOMAIN)) == 0
    stop_addon.reset_mock()
    create_shapshot.reset_mock()
    uninstall_addon.reset_mock()

    # test add-on stop failure
    entry.add_to_opp(opp)
    assert len(opp.config_entries.async_entries(DOMAIN)) == 1
    stop_addon.side_effect = OppioAPIError()

    await opp.config_entries.async_remove(entry.entry_id)

    assert stop_addon.call_count == 1
    assert stop_addon.call_args == call(opp, "core_zwave_js")
    assert create_shapshot.call_count == 0
    assert uninstall_addon.call_count == 0
    assert entry.state is ConfigEntryState.NOT_LOADED
    assert len(opp.config_entries.async_entries(DOMAIN)) == 0
    assert "Failed to stop the Z-Wave JS add-on" in caplog.text
    stop_addon.side_effect = None
    stop_addon.reset_mock()
    create_shapshot.reset_mock()
    uninstall_addon.reset_mock()

    # test create snapshot failure
    entry.add_to_opp(opp)
    assert len(opp.config_entries.async_entries(DOMAIN)) == 1
    create_shapshot.side_effect = OppioAPIError()

    await opp.config_entries.async_remove(entry.entry_id)

    assert stop_addon.call_count == 1
    assert stop_addon.call_args == call(opp, "core_zwave_js")
    assert create_shapshot.call_count == 1
    assert create_shapshot.call_args == call(
        opp,
        {
            "name": "addon_core_zwave_js_1.0",
            "addons": ["core_zwave_js"]
        },
        partial=True,
    )
    assert uninstall_addon.call_count == 0
    assert entry.state is ConfigEntryState.NOT_LOADED
    assert len(opp.config_entries.async_entries(DOMAIN)) == 0
    assert "Failed to create a snapshot of the Z-Wave JS add-on" in caplog.text
    create_shapshot.side_effect = None
    stop_addon.reset_mock()
    create_shapshot.reset_mock()
    uninstall_addon.reset_mock()

    # test add-on uninstall failure
    entry.add_to_opp(opp)
    assert len(opp.config_entries.async_entries(DOMAIN)) == 1
    uninstall_addon.side_effect = OppioAPIError()

    await opp.config_entries.async_remove(entry.entry_id)

    assert stop_addon.call_count == 1
    assert stop_addon.call_args == call(opp, "core_zwave_js")
    assert create_shapshot.call_count == 1
    assert create_shapshot.call_args == call(
        opp,
        {
            "name": "addon_core_zwave_js_1.0",
            "addons": ["core_zwave_js"]
        },
        partial=True,
    )
    assert uninstall_addon.call_count == 1
    assert uninstall_addon.call_args == call(opp, "core_zwave_js")
    assert entry.state is ConfigEntryState.NOT_LOADED
    assert len(opp.config_entries.async_entries(DOMAIN)) == 0
    assert "Failed to uninstall the Z-Wave JS add-on" in caplog.text
Ejemplo n.º 9
0
    entry.add_to_opp(opp)

    await opp.config_entries.async_setup(entry.entry_id)
    await opp.async_block_till_done()

    assert entry.state is ConfigEntryState.SETUP_RETRY
    assert install_addon.call_count == 1
    assert install_addon.call_args == call(opp, "core_zwave_js")
    assert set_addon_options.call_count == 1
    assert set_addon_options.call_args == call(opp, "core_zwave_js",
                                               {"options": addon_options})
    assert start_addon.call_count == 1
    assert start_addon.call_args == call(opp, "core_zwave_js")


@pytest.mark.parametrize("addon_info_side_effect", [OppioAPIError("Boom")])
async def test_addon_info_failure(
    opp,
    addon_installed,
    install_addon,
    addon_options,
    set_addon_options,
    start_addon,
):
    """Test failure to get add-on info for Z-Wave JS add-on during entry setup."""
    device = "/test"
    network_key = "abc123"
    entry = MockConfigEntry(
        domain=DOMAIN,
        title="Z-Wave JS",
        data={