Add example usage for unregistering an LLM API (#2524)

* Add example usage for unregistering an LLM API

* Update example to be based on a config entry
This commit is contained in:
Allen Porter 2025-01-09 08:52:36 -08:00 committed by GitHub
parent 0abbc5dcbe
commit 5ffe47872d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -242,6 +242,7 @@ The `ToolInput` has following attributes:
The API object allows creating API instances. An API Instance represents a collection of tools that will be made available to the LLM.
```python
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helper import llm
from homeassistant.util import dt as dt_util
@ -251,14 +252,6 @@ from homeassistant.util.json import JsonObjectType
class MyAPI(API):
"""My own API for LLMs."""
def __init__(self, hass: HomeAssistant) -> None:
"""Init the class."""
super().__init__(
hass=hass,
id="my_unique_key",
name="My own API",
)
async def async_get_api_instance(self, llm_context: LLMContext) -> APIInstance:
"""Return the instance of the API."""
return APIInstance(
@ -269,9 +262,15 @@ class MyAPI(API):
)
async def async_setup_api(hass: HomeAssistant) -> None:
async def async_setup_api(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Register the API with Home Assistant."""
llm.async_register_api(hass, MyAPI())
# If the API is associated with a Config Entry, the LLM API must be
# unregistered when the config entry is unloaded.
unreg = llm.async_register_api(
hass,
MyAPI(hass, f"my_unique_key-{entry.entry_id}", entry.title)
)
entry.async_on_unload(unreg)
```
The `llm.API` class has the following attributes: