async def async_setup_entry(opp: OpenPeerPower,
                            entry: config_entries.ConfigEntry):
    """Set up Almond config entry."""
    websession = aiohttp_client.async_get_clientsession(opp)

    if entry.data["type"] == TYPE_LOCAL:
        auth = AlmondLocalAuth(entry.data["host"], websession)
    else:
        # OAuth2
        implementation = (
            await
            config_entry_oauth2_flow.async_get_config_entry_implementation(
                opp, entry))
        oauth_session = config_entry_oauth2_flow.OAuth2Session(
            opp, entry, implementation)
        auth = AlmondOAuth(entry.data["host"], websession, oauth_session)

    api = WebAlmondAPI(auth)
    agent = AlmondAgent(opp, api, entry)

    # Opp.io does its own configuration.
    if not entry.data.get("is_oppio"):
        # If we're not starting or local, set up Almond right away
        if opp.state != CoreState.not_running or entry.data[
                "type"] == TYPE_LOCAL:
            await _configure_almond_for_ha(opp, entry, api)

        else:
            # OAuth2 implementations can potentially rely on the HA Cloud url.
            # This url is not be available until 30 seconds after boot.

            async def configure_almond(_now):
                try:
                    await _configure_almond_for_ha(opp, entry, api)
                except ConfigEntryNotReady:
                    _LOGGER.warning(
                        "Unable to configure Almond to connect to Open Peer Power"
                    )

            async def almond_opp_start(_event):
                event.async_call_later(opp, ALMOND_SETUP_DELAY,
                                       configure_almond)

            opp.bus.async_listen_once(EVENT_OPENPEERPOWER_START,
                                      almond_opp_start)

    conversation.async_set_agent(opp, agent)
    return True
Exemple #2
0
async def test_custom_agent(opp, opp_client, opp_admin_user):
    """Test a custom conversation agent."""

    calls = []

    class MyAgent(conversation.AbstractConversationAgent):
        """Test Agent."""
        async def async_process(self, text, context, conversation_id):
            """Process some text."""
            calls.append((text, context, conversation_id))
            response = intent.IntentResponse()
            response.async_set_speech("Test response")
            return response

    conversation.async_set_agent(opp, MyAgent())

    assert await async_setup_component(opp, "conversation", {})

    client = await opp_client()

    resp = await client.post(
        "/api/conversation/process",
        json={
            "text": "Test Text",
            "conversation_id": "test-conv-id"
        },
    )
    assert resp.status == 200
    assert await resp.json() == {
        "card": {},
        "speech": {
            "plain": {
                "extra_data": None,
                "speech": "Test response"
            }
        },
    }

    assert len(calls) == 1
    assert calls[0][0] == "Test Text"
    assert calls[0][1].user_id == opp_admin_user.id
    assert calls[0][2] == "test-conv-id"
async def async_unload_entry(opp, entry):
    """Unload Almond."""
    conversation.async_set_agent(opp, None)
    return True