예제 #1
0
async def test_reload_entry_in_setup_retry(hass, client, hass_admin_user):
    """Test reloading an entry via the API that is in setup retry."""
    mock_setup_entry = AsyncMock(return_value=True)
    mock_unload_entry = AsyncMock(return_value=True)
    mock_migrate_entry = AsyncMock(return_value=True)

    mock_integration(
        hass,
        MockModule(
            "comp",
            async_setup_entry=mock_setup_entry,
            async_unload_entry=mock_unload_entry,
            async_migrate_entry=mock_migrate_entry,
        ),
    )
    mock_entity_platform(hass, "config_flow.comp", None)
    entry = MockConfigEntry(domain="comp",
                            state=core_ce.ConfigEntryState.SETUP_RETRY)
    entry.supports_unload = True
    entry.add_to_hass(hass)

    with patch.dict(HANDLERS, {"comp": ConfigFlow, "test": ConfigFlow}):
        resp = await client.post(
            f"/api/config/config_entries/entry/{entry.entry_id}/reload")
        await hass.async_block_till_done()
    assert resp.status == HTTPStatus.OK
    data = await resp.json()
    assert data == {"require_restart": False}
    assert len(hass.config_entries.async_entries()) == 1
async def test_get_entries(hass, client):
    """Test get entries."""
    with patch.dict(HANDLERS, clear=True):

        @HANDLERS.register("comp1")
        class Comp1ConfigFlow:
            """Config flow with options flow."""
            @staticmethod
            @callback
            def async_get_options_flow(config, options):
                """Get options flow."""
                pass

        hass.helpers.config_entry_flow.register_discovery_flow(
            "comp2", "Comp 2", lambda: None, core_ce.CONN_CLASS_ASSUMED)

        entry = MockConfigEntry(
            domain="comp1",
            title="Test 1",
            source="bla",
            connection_class=core_ce.CONN_CLASS_LOCAL_POLL,
        )
        entry.supports_unload = True
        entry.add_to_hass(hass)
        MockConfigEntry(
            domain="comp2",
            title="Test 2",
            source="bla2",
            state=core_ce.ENTRY_STATE_LOADED,
            connection_class=core_ce.CONN_CLASS_ASSUMED,
        ).add_to_hass(hass)

        resp = await client.get("/api/config/config_entries/entry")
        assert resp.status == 200
        data = await resp.json()
        for entry in data:
            entry.pop("entry_id")
        assert data == [
            {
                "domain": "comp1",
                "title": "Test 1",
                "source": "bla",
                "state": "not_loaded",
                "connection_class": "local_poll",
                "supports_options": True,
                "supports_unload": True,
            },
            {
                "domain": "comp2",
                "title": "Test 2",
                "source": "bla2",
                "state": "loaded",
                "connection_class": "assumed",
                "supports_options": False,
                "supports_unload": False,
            },
        ]
예제 #3
0
async def test_get_entries(hass, client):
    """Test get entries."""
    with patch.dict(HANDLERS, clear=True):

        @HANDLERS.register("comp1")
        class Comp1ConfigFlow:
            """Config flow with options flow."""

            @staticmethod
            @callback
            def async_get_options_flow(config, options):
                """Get options flow."""
                pass

        hass.helpers.config_entry_flow.register_discovery_flow(
            "comp2", "Comp 2", lambda: None
        )

        entry = MockConfigEntry(
            domain="comp1",
            title="Test 1",
            source="bla",
        )
        entry.supports_unload = True
        entry.add_to_hass(hass)
        MockConfigEntry(
            domain="comp2",
            title="Test 2",
            source="bla2",
            state=core_ce.ConfigEntryState.SETUP_ERROR,
            reason="Unsupported API",
        ).add_to_hass(hass)
        MockConfigEntry(
            domain="comp3",
            title="Test 3",
            source="bla3",
            disabled_by=core_ce.DISABLED_USER,
        ).add_to_hass(hass)

        resp = await client.get("/api/config/config_entries/entry")
        assert resp.status == HTTPStatus.OK
        data = await resp.json()
        for entry in data:
            entry.pop("entry_id")
        assert data == [
            {
                "domain": "comp1",
                "title": "Test 1",
                "source": "bla",
                "state": core_ce.ConfigEntryState.NOT_LOADED.value,
                "supports_options": True,
                "supports_unload": True,
                "pref_disable_new_entities": False,
                "pref_disable_polling": False,
                "disabled_by": None,
                "reason": None,
            },
            {
                "domain": "comp2",
                "title": "Test 2",
                "source": "bla2",
                "state": core_ce.ConfigEntryState.SETUP_ERROR.value,
                "supports_options": False,
                "supports_unload": False,
                "pref_disable_new_entities": False,
                "pref_disable_polling": False,
                "disabled_by": None,
                "reason": "Unsupported API",
            },
            {
                "domain": "comp3",
                "title": "Test 3",
                "source": "bla3",
                "state": core_ce.ConfigEntryState.NOT_LOADED.value,
                "supports_options": False,
                "supports_unload": False,
                "pref_disable_new_entities": False,
                "pref_disable_polling": False,
                "disabled_by": core_ce.DISABLED_USER,
                "reason": None,
            },
        ]
예제 #4
0
async def test_get_entries(hass, client, clear_handlers):
    """Test get entries."""
    mock_integration(hass, MockModule("comp1"))
    mock_integration(
        hass,
        MockModule("comp2", partial_manifest={"integration_type": "helper"}))
    mock_integration(hass, MockModule("comp3"))

    @HANDLERS.register("comp1")
    class Comp1ConfigFlow:
        """Config flow with options flow."""
        @staticmethod
        @callback
        def async_get_options_flow(config_entry):
            """Get options flow."""
            pass

        @classmethod
        @callback
        def async_supports_options_flow(cls, config_entry):
            """Return options flow support for this handler."""
            return True

    config_entry_flow.register_discovery_flow("comp2", "Comp 2", lambda: None)

    entry = MockConfigEntry(
        domain="comp1",
        title="Test 1",
        source="bla",
    )
    entry.supports_unload = True
    entry.add_to_hass(hass)
    MockConfigEntry(
        domain="comp2",
        title="Test 2",
        source="bla2",
        state=core_ce.ConfigEntryState.SETUP_ERROR,
        reason="Unsupported API",
    ).add_to_hass(hass)
    MockConfigEntry(
        domain="comp3",
        title="Test 3",
        source="bla3",
        disabled_by=core_ce.ConfigEntryDisabler.USER,
    ).add_to_hass(hass)

    resp = await client.get("/api/config/config_entries/entry")
    assert resp.status == HTTPStatus.OK
    data = await resp.json()
    for entry in data:
        entry.pop("entry_id")
    assert data == [
        {
            "domain": "comp1",
            "title": "Test 1",
            "source": "bla",
            "state": core_ce.ConfigEntryState.NOT_LOADED.value,
            "supports_options": True,
            "supports_remove_device": False,
            "supports_unload": True,
            "pref_disable_new_entities": False,
            "pref_disable_polling": False,
            "disabled_by": None,
            "reason": None,
        },
        {
            "domain": "comp2",
            "title": "Test 2",
            "source": "bla2",
            "state": core_ce.ConfigEntryState.SETUP_ERROR.value,
            "supports_options": False,
            "supports_remove_device": False,
            "supports_unload": False,
            "pref_disable_new_entities": False,
            "pref_disable_polling": False,
            "disabled_by": None,
            "reason": "Unsupported API",
        },
        {
            "domain": "comp3",
            "title": "Test 3",
            "source": "bla3",
            "state": core_ce.ConfigEntryState.NOT_LOADED.value,
            "supports_options": False,
            "supports_remove_device": False,
            "supports_unload": False,
            "pref_disable_new_entities": False,
            "pref_disable_polling": False,
            "disabled_by": core_ce.ConfigEntryDisabler.USER,
            "reason": None,
        },
    ]

    resp = await client.get("/api/config/config_entries/entry?domain=comp3")
    assert resp.status == HTTPStatus.OK
    data = await resp.json()
    assert len(data) == 1
    assert data[0]["domain"] == "comp3"

    resp = await client.get(
        "/api/config/config_entries/entry?domain=comp3&type=helper")
    assert resp.status == HTTPStatus.OK
    data = await resp.json()
    assert len(data) == 0

    resp = await client.get(
        "/api/config/config_entries/entry?domain=comp3&type=integration")
    assert resp.status == HTTPStatus.OK
    data = await resp.json()
    assert len(data) == 1

    resp = await client.get("/api/config/config_entries/entry?type=integration"
                            )
    assert resp.status == HTTPStatus.OK
    data = await resp.json()
    assert len(data) == 2
    assert data[0]["domain"] == "comp1"
    assert data[1]["domain"] == "comp3"