async def async_setup_entry(opp, config): """Set up a config entry.""" try: bot = HangoutsBot( opp, config.data.get(CONF_REFRESH_TOKEN), opp.data[DOMAIN][CONF_INTENTS], opp.data[DOMAIN][CONF_DEFAULT_CONVERSATIONS], opp.data[DOMAIN][CONF_ERROR_SUPPRESSED_CONVERSATIONS], ) opp.data[DOMAIN][CONF_BOT] = bot except GoogleAuthError as exception: _LOGGER.error("Hangouts failed to log in: %s", str(exception)) return False dispatcher.async_dispatcher_connect( opp, EVENT_HANGOUTS_CONNECTED, bot.async_handle_update_users_and_conversations) dispatcher.async_dispatcher_connect(opp, EVENT_HANGOUTS_CONVERSATIONS_CHANGED, bot.async_resolve_conversations) dispatcher.async_dispatcher_connect( opp, EVENT_HANGOUTS_CONVERSATIONS_RESOLVED, bot.async_update_conversation_commands, ) config.async_on_unload( opp.bus.async_listen_once(EVENT_OPENPEERPOWER_STOP, bot.async_handle_opp_stop)) await bot.async_connect() opp.services.async_register( DOMAIN, SERVICE_SEND_MESSAGE, bot.async_handle_send_message, schema=MESSAGE_SCHEMA, ) opp.services.async_register( DOMAIN, SERVICE_UPDATE, bot.async_handle_update_users_and_conversations, schema=vol.Schema({}), ) opp.services.async_register(DOMAIN, SERVICE_RECONNECT, bot.async_handle_reconnect, schema=vol.Schema({})) intent.async_register(opp, HelpIntent(opp)) return True
async def test_http_processing_intent(opp, opp_client, opp_admin_user): """Test processing intent via HTTP API.""" class TestIntentHandler(intent.IntentHandler): """Test Intent Handler.""" intent_type = "OrderBeer" async def async_handle(self, intent): """Handle the intent.""" assert intent.context.user_id == opp_admin_user.id response = intent.create_response() response.async_set_speech("I've ordered a {}!".format( intent.slots["type"]["value"])) response.async_set_card( "Beer ordered", "You chose a {}.".format(intent.slots["type"]["value"])) return response intent.async_register(opp, TestIntentHandler()) result = await async_setup_component( opp, "conversation", { "conversation": { "intents": { "OrderBeer": ["I would like the {type} beer"] } } }, ) assert result client = await opp_client() resp = await client.post("/api/conversation/process", json={"text": "I would like the Grolsch beer"}) assert resp.status == 200 data = await resp.json() assert data == { "card": { "simple": { "content": "You chose a Grolsch.", "title": "Beer ordered" } }, "speech": { "plain": { "extra_data": None, "speech": "I've ordered a Grolsch!" } }, }
async def async_setup(opp, config): """Activate Alexa component.""" intents = copy.deepcopy(config[DOMAIN]) template.attach(opp, intents) for intent_type, conf in intents.items(): if CONF_ACTION in conf: conf[CONF_ACTION] = script.Script(opp, conf[CONF_ACTION], f"Intent Script {intent_type}", DOMAIN) intent.async_register(opp, ScriptIntentHandler(intent_type, conf)) return True
async def test_http_handle_intent(opp, opp_client, opp_admin_user): """Test handle intent via HTTP API.""" class TestIntentHandler(intent.IntentHandler): """Test Intent Handler.""" intent_type = "OrderBeer" async def async_handle(self, intent): """Handle the intent.""" assert intent.context.user_id == opp_admin_user.id response = intent.create_response() response.async_set_speech("I've ordered a {}!".format( intent.slots["type"]["value"])) response.async_set_card( "Beer ordered", "You chose a {}.".format(intent.slots["type"]["value"])) return response intent.async_register(opp, TestIntentHandler()) result = await async_setup_component(opp, "intent", {}) assert result client = await opp_client() resp = await client.post("/api/intent/handle", json={ "name": "OrderBeer", "data": { "type": "Belgian" } }) assert resp.status == 200 data = await resp.json() assert data == { "card": { "simple": { "content": "You chose a Belgian.", "title": "Beer ordered" } }, "speech": { "plain": { "extra_data": None, "speech": "I've ordered a Belgian!" } }, }
def async_mock_intent(opp, intent_typ): """Set up a fake intent handler.""" intents = [] class MockIntentHandler(intent.IntentHandler): intent_type = intent_typ async def async_handle(self, intent): """Handle the intent.""" intents.append(intent) return intent.create_response() intent.async_register(opp, MockIntentHandler()) return intents
async def test_snips_service_intent(opp, mqtt_mock): """Test ServiceIntentHandler via Snips.""" opp.states.async_set("light.kitchen", "off") calls = async_mock_service(opp, "light", "turn_on") result = await async_setup_component(opp, "snips", {"snips": {}}) assert result payload = """ { "input": "turn the light on", "intent": { "intentName": "Lights", "confidenceScore": 0.85 }, "siteId": "default", "slots": [ { "slotName": "name", "value": { "kind": "Custom", "value": "kitchen" }, "rawValue": "green" } ] } """ async_register( opp, ServiceIntentHandler("Lights", "light", "turn_on", "Turned {} on")) async_fire_mqtt_message(opp, "hermes/intent/Lights", payload) await opp.async_block_till_done() assert len(calls) == 1 assert calls[0].domain == "light" assert calls[0].service == "turn_on" assert calls[0].data["entity_id"] == "light.kitchen" assert "confidenceScore" not in calls[0].data assert "site_id" not in calls[0].data
async def async_setup_intents(opp): """Set up the Shopping List intents.""" intent.async_register(opp, AddItemIntent()) intent.async_register(opp, ListTopItemsIntent())