Skip to content

Commit

Permalink
Add config to return entities from intents
Browse files Browse the repository at this point in the history
  • Loading branch information
Shulyaka committed Aug 13, 2024
1 parent c6a0372 commit c4800aa
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 5 deletions.
14 changes: 12 additions & 2 deletions custom_components/powerllm/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
)
from homeassistant.util import yaml

from .const import CONF_PROMPT_ENTITIES, CONF_SCRIPT_EXPOSED_ONLY, DOMAIN
from .const import (
CONF_INTENT_ENTITIES,
CONF_PROMPT_ENTITIES,
CONF_SCRIPT_EXPOSED_ONLY,
DOMAIN,
)
from .llm_tools import PowerIntentTool, PowerLLMTool, PowerScriptTool
from .tools.script import DynamicScriptTool

Expand Down Expand Up @@ -183,6 +188,9 @@ def _async_get_tools(
intent.INTENT_TIMER_STATUS,
}

if not self.config_entry.options[CONF_INTENT_ENTITIES]:
ignore_intents.append(intent.INTENT_GET_STATE)

intent_handlers = [
intent_handler
for intent_handler in intent.async_get(self.hass)
Expand All @@ -204,7 +212,9 @@ def _async_get_tools(

tools: list[PowerLLMTool] = [
PowerIntentTool(
self.cached_slugify(intent_handler.intent_type), intent_handler
self.cached_slugify(intent_handler.intent_type),
intent_handler,
self.config_entry.options[CONF_INTENT_ENTITIES],
)
for intent_handler in intent_handlers
]
Expand Down
8 changes: 7 additions & 1 deletion custom_components/powerllm/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv

from .const import CONF_PROMPT_ENTITIES, CONF_SCRIPT_EXPOSED_ONLY, DOMAIN
from .const import (
CONF_INTENT_ENTITIES,
CONF_PROMPT_ENTITIES,
CONF_SCRIPT_EXPOSED_ONLY,
DOMAIN,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -30,6 +35,7 @@
OPTIONS_SCHEMA = vol.Schema(
{
vol.Required(CONF_PROMPT_ENTITIES, default=True): bool,
vol.Required(CONF_INTENT_ENTITIES, default=True): bool,
vol.Required(CONF_SCRIPT_EXPOSED_ONLY, default=True): bool,
}
)
Expand Down
1 change: 1 addition & 0 deletions custom_components/powerllm/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
DOMAIN = "powerllm"

CONF_PROMPT_ENTITIES = "prompt_entities"
CONF_INTENT_ENTITIES = "intent_entities"
CONF_SCRIPT_EXPOSED_ONLY = "script_exposed_only"
6 changes: 4 additions & 2 deletions custom_components/powerllm/llm_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@ def __init__(
self,
name: str,
intent_handler: intent.IntentHandler,
response_entities: bool = False,
) -> None:
"""Init the class."""
self.name = name
self.intent_handler = intent_handler
self._response_entities = response_entities
self.description = (
intent_handler.description or f"Execute Home Assistant {self.name} intent"
)
Expand Down Expand Up @@ -250,11 +252,11 @@ async def async_call(
device_id=llm_context.device_id,
)
response = intent_response.as_dict()
if intent_response.matched_states:
if self._response_entities and intent_response.matched_states:
response["data"]["matched_states"] = [
_format_state(hass, state) for state in intent_response.matched_states
]
if intent_response.unmatched_states:
if self._response_entities and intent_response.unmatched_states:
response["data"]["unmatched_states"] = [
_format_state(hass, state) for state in intent_response.unmatched_states
]
Expand Down
4 changes: 4 additions & 0 deletions custom_components/powerllm/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"init": {
"data": {
"prompt_entities": "Include exposed entities into api prompt",
"intent_entities": "Include relevant entities into intent tool response",
"script_exposed_only": "Only allow referencing exposed entities"
},
"data_description": {
"prompt_entities": "If disabled, LLM can still guess device name or query it using HssGetStates",
"intent_entities": "Also enables HassGetState intent for querying additional attributes of entities",
"script_exposed_only": "Disabling would allow more complicated scripts, but could affect unexposed entities"
}
}
Expand All @@ -23,10 +25,12 @@
"init": {
"data": {
"prompt_entities": "Include exposed entities into api prompt",
"intent_entities": "Include relevant entities into intent tool response",
"script_exposed_only": "Only allow referencing exposed entities"
},
"data_description": {
"prompt_entities": "If disabled, LLM can still guess device name or query it using HssGetStates",
"intent_entities": "Also enables HassGetState intent for querying additional attributes of entities",
"script_exposed_only": "Disabling would allow more complicated scripts, but could affect unexposed entities"
}
}
Expand Down
4 changes: 4 additions & 0 deletions custom_components/powerllm/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"init": {
"data": {
"prompt_entities": "Include exposed entities into api prompt",
"intent_entities": "Include relevant entities into intent tool response",
"script_exposed_only": "Only allow referencing exposed entities"
},
"data_description": {
"prompt_entities": "If disabled, LLM can still guess device name or query it using HssGetStates",
"intent_entities": "Also enables HassGetState intent for querying additional attributes of entities",
"script_exposed_only": "Disabling would allow more complicated scripts, but could affect unexposed entities"
}
}
Expand All @@ -23,10 +25,12 @@
"init": {
"data": {
"prompt_entities": "Include exposed entities into api prompt",
"intent_entities": "Include relevant entities into intent tool response",
"script_exposed_only": "Only allow referencing exposed entities"
},
"data_description": {
"prompt_entities": "If disabled, LLM can still guess device name or query it using HssGetStates",
"intent_entities": "Also enables HassGetState intent for querying additional attributes of entities",
"script_exposed_only": "Disabling would allow more complicated scripts, but could affect unexposed entities"
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from homeassistant.const import CONF_NAME

from custom_components.powerllm.const import (
CONF_INTENT_ENTITIES,
CONF_PROMPT_ENTITIES,
CONF_SCRIPT_EXPOSED_ONLY,
)
Expand All @@ -14,5 +15,6 @@

MOCK_OPTIONS_CONFIG = {
CONF_PROMPT_ENTITIES: True,
CONF_INTENT_ENTITIES: True,
CONF_SCRIPT_EXPOSED_ONLY: True,
}
3 changes: 3 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from homeassistant.core import HomeAssistant

from custom_components.powerllm.const import (
CONF_INTENT_ENTITIES,
CONF_PROMPT_ENTITIES,
CONF_SCRIPT_EXPOSED_ONLY,
DOMAIN,
Expand Down Expand Up @@ -71,10 +72,12 @@ async def test_options_flow(
options_flow["flow_id"],
{
CONF_PROMPT_ENTITIES: False,
CONF_INTENT_ENTITIES: False,
CONF_SCRIPT_EXPOSED_ONLY: False,
},
)
await hass.async_block_till_done()
assert options["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY
assert options["data"][CONF_PROMPT_ENTITIES] is False
assert options["data"][CONF_INTENT_ENTITIES] is False
assert options["data"][CONF_SCRIPT_EXPOSED_ONLY] is False

0 comments on commit c4800aa

Please sign in to comment.