2018-04-24 13:52:18 +00:00
---
title: "Handling intents"
---
Any component can register to handle intents. This allows a single component to handle intents fired from multiple voice assistants.
A component has to register an intent handler for each type that it wants to handle. Intent handlers have to extend `homeassistant.helpers.intent.IntentHandler`
```python
from homeassistant.helpers import intent
2020-01-13 19:55:41 +00:00
DATA_KEY = "example_key"
2018-04-24 13:52:18 +00:00
2023-01-09 02:32:17 +00:00
async def async_setup(hass, config):
2018-04-24 13:52:18 +00:00
hass.data[DATA_KEY] = 0
intent.async_register(hass, CountInvocationIntent())
class CountInvocationIntent(intent.IntentHandler):
"""Handle CountInvocationIntent intents."""
# Type of intent to handle
2020-01-13 19:55:41 +00:00
intent_type = "CountInvocationIntent"
2018-04-24 13:52:18 +00:00
2024-06-01 03:21:56 +00:00
description = "Count how often it has been called"
2018-04-24 13:52:18 +00:00
# Optional. A validation schema for slots
# slot_schema = {
# 'item': cv.string
# }
2023-01-09 02:32:17 +00:00
async def async_handle(self, intent_obj):
2018-04-24 13:52:18 +00:00
"""Handle the intent."""
intent_obj.hass.data[DATA_KEY] += 1
2023-01-09 15:22:51 +00:00
count = intent_obj.hass.data[DATA_KEY]
2018-04-24 13:52:18 +00:00
response = intent_obj.create_response()
response.async_set_speech(
2023-01-09 15:22:51 +00:00
f"This intent has been invoked {count} times"
2020-01-13 19:55:41 +00:00
)
2018-04-24 13:52:18 +00:00
return response
```