From d652d3dfc48bc9c70de80f66f051bd8110fa4f53 Mon Sep 17 00:00:00 2001 From: jekalmin Date: Sat, 13 Jan 2024 23:37:32 +0900 Subject: [PATCH] [#86] "area_id" and "deviced_id" can be set to execute_service --- .../extended_openai_conversation/__init__.py | 18 ++---------------- .../extended_openai_conversation/exceptions.py | 4 ++-- .../extended_openai_conversation/helpers.py | 17 ++++++----------- 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/custom_components/extended_openai_conversation/__init__.py b/custom_components/extended_openai_conversation/__init__.py index 25fd1c3..8df472b 100644 --- a/custom_components/extended_openai_conversation/__init__.py +++ b/custom_components/extended_openai_conversation/__init__.py @@ -24,7 +24,6 @@ ConfigEntryNotReady, HomeAssistantError, TemplateError, - ServiceNotFound, ) from homeassistant.helpers import ( @@ -59,26 +58,13 @@ ) from .exceptions import ( - EntityNotFound, - EntityNotExposed, - CallServiceError, FunctionNotFound, - NativeNotFound, FunctionLoadFailed, ParseArgumentsFailed, InvalidFunction, ) from .helpers import ( - FUNCTION_EXECUTORS, - FunctionExecutor, - NativeFunctionExecutor, - ScriptFunctionExecutor, - TemplateFunctionExecutor, - RestFunctionExecutor, - ScrapeFunctionExecutor, - CompositeFunctionExecutor, - convert_to_template, validate_authentication, get_function_executor, is_azure, @@ -88,13 +74,13 @@ _LOGGER = logging.getLogger(__name__) CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) -AZURE_DOMAIN_PATTERN = r"\.openai\.azure\.com" # hass.data key for agent. DATA_AGENT = "agent" + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up OpenAI Conversation from a config entry.""" @@ -306,7 +292,7 @@ async def query( ) - _LOGGER.info("Response %s", response) + _LOGGER.info("Response %s", response.model_dump(exclude_none=True)) choice: Choice = response.choices[0] message = choice.message if choice.finish_reason == "function_call": diff --git a/custom_components/extended_openai_conversation/exceptions.py b/custom_components/extended_openai_conversation/exceptions.py index 4c513e3..7ed1f00 100644 --- a/custom_components/extended_openai_conversation/exceptions.py +++ b/custom_components/extended_openai_conversation/exceptions.py @@ -35,7 +35,7 @@ def __init__(self, domain: str, service: str, data: object) -> None: """Initialize error.""" super().__init__( self, - f"unable to call service {domain}.{service} with data {data}. 'entity_id' is required", + f"unable to call service {domain}.{service} with data {data}. One of 'entity_id', 'area_id', or 'device_id' is required", ) self.domain = domain self.service = service @@ -43,7 +43,7 @@ def __init__(self, domain: str, service: str, data: object) -> None: def __str__(self) -> str: """Return string representation.""" - return f"unable to call service {self.domain}.{self.service} with data {self.data}. 'entity_id' is required" + return f"unable to call service {self.domain}.{self.service} with data {self.data}. One of 'entity_id', 'area_id', or 'device_id' is required" class FunctionNotFound(HomeAssistantError): diff --git a/custom_components/extended_openai_conversation/helpers.py b/custom_components/extended_openai_conversation/helpers.py index 6012316..7ff835d 100644 --- a/custom_components/extended_openai_conversation/helpers.py +++ b/custom_components/extended_openai_conversation/helpers.py @@ -18,7 +18,6 @@ automation, rest, scrape, - history, conversation, recorder, ) @@ -38,13 +37,7 @@ from homeassistant.core import HomeAssistant, State from homeassistant.helpers import config_validation as cv from homeassistant.helpers.template import Template -from homeassistant.helpers.script import ( - Script, - SCRIPT_MODE_SINGLE, - SCRIPT_MODE_PARALLEL, - DEFAULT_MAX, - DEFAULT_MAX_EXCEEDED, -) +from homeassistant.helpers.script import Script from homeassistant.exceptions import HomeAssistantError, ServiceNotFound @@ -231,16 +224,18 @@ async def execute_service( "service_data", service_argument.get("data", {}) ) entity_id = service_data.get("entity_id", service_argument.get("entity_id")) + area_id = service_data.get("area_id") + device_id = service_data.get("device_id") if isinstance(entity_id, str): entity_id = [e.strip() for e in entity_id.split(",")] service_data["entity_id"] = entity_id - if entity_id is None: + if entity_id is None and area_id is None and device_id is None: raise CallServiceError(domain, service, service_data) if not hass.services.has_service(domain, service): raise ServiceNotFound(domain, service) - self.validate_entity_ids(hass, entity_id, exposed_entities) + self.validate_entity_ids(hass, entity_id or [], exposed_entities) try: await hass.services.async_call( @@ -249,7 +244,7 @@ async def execute_service( service_data=service_data, ) result.append(True) - except HomeAssistantError: + except HomeAssistantError as e: _LOGGER.error(e) result.append(False)