diff --git a/custom_components/mistral_ai_api/__init__.py b/custom_components/mistral_ai_api/__init__.py index 81bea1a..3d7180e 100644 --- a/custom_components/mistral_ai_api/__init__.py +++ b/custom_components/mistral_ai_api/__init__.py @@ -4,8 +4,10 @@ from homeassistant.const import CONF_API_KEY, CONF_NAME from homeassistant.core import HomeAssistant import homeassistant.helpers.config_validation as cv +from homeassistant.util.json import JsonObjectType +from homeassistant.core import HomeAssistant, ServiceCall, ServiceResponse, SupportsResponse -from .api import send_prompt_command +from .api import send_prompt_command, retrieve_last_prompt from .const import ( ATTR_IDENTIFIER, ATTR_LAST_PROMPT, @@ -57,7 +59,7 @@ async def async_setup(hass: HomeAssistant, config: dict): async def setup_common(hass: HomeAssistant, conf: dict) -> bool: api_key = conf[CONF_API_KEY] - async def send_prompt(call): + async def send_prompt(call: ServiceCall): prompt = call.data.get("prompt") agent_id = call.data.get("agent_id") identifier = call.data.get("identifier") @@ -69,6 +71,12 @@ async def send_prompt(call): ) hass.services.async_register(DOMAIN, "send_prompt", send_prompt) + + async def retrieve_last_command(call: ServiceCall) -> ServiceResponse: + return await retrieve_last_prompt(hass) + + hass.services.async_register(DOMAIN, "retrieve_last", retrieve_last_command, supports_response=SupportsResponse.ONLY) + return True diff --git a/custom_components/mistral_ai_api/api.py b/custom_components/mistral_ai_api/api.py index 77bab28..b7fa518 100644 --- a/custom_components/mistral_ai_api/api.py +++ b/custom_components/mistral_ai_api/api.py @@ -7,6 +7,10 @@ ATTR_LAST_PROMPT, ATTR_LAST_RESPONSE, ATTR_IDENTIFIER, + ATTR_TIMESTAMP, + STATE_IDLE, + STATE_PROCESSING, + EV_PROVIDE_RESPONSE ) _LOGGER = logging.getLogger(__name__) @@ -23,9 +27,9 @@ async def send_prompt_command( ): sensor = hass.data[DOMAIN].get("sensor") if sensor: - sensor.set_state("processing") - sensor.set_attribute(ATTR_LAST_PROMPT, prompt) - sensor.set_attribute(ATTR_IDENTIFIER, identifier) + sensor.set_state(STATE_PROCESSING) + sensor.last_prompt = prompt + sensor.identifier = identifier sensor.refresh_timestamp() sensor.async_write_ha_state() else: @@ -72,8 +76,8 @@ def make_request(): message_content = response_data["choices"][0]["message"]["content"] if sensor: - sensor.set_state("idle") - sensor.set_attribute(ATTR_LAST_RESPONSE, message_content) + sensor.set_state(STATE_IDLE) + sensor.last_response = message_content sensor.refresh_timestamp() sensor.async_write_ha_state() @@ -82,7 +86,8 @@ def make_request(): "identifier": identifier, "agent_id": agent_id if agent_id else "", } - hass.bus.async_fire("mistral_ai_response", event_data) + + hass.bus.async_fire(EV_PROVIDE_RESPONSE, event_data) _LOGGER.error(f"Unexpected response structure: {response_data}") except asyncio.TimeoutError: @@ -91,3 +96,19 @@ def make_request(): _LOGGER.error(f"REST command error: {e}") except KeyError as e: _LOGGER.error(f"KeyError: {e}") + +async def retrieve_last_prompt(hass: HomeAssistant): + sensor = hass.data[DOMAIN].get("sensor") + if sensor: + + response = { + ATTR_IDENTIFIER: sensor.identifier, + ATTR_LAST_PROMPT: sensor.last_prompt, + ATTR_LAST_RESPONSE: sensor.last_response, + ATTR_TIMESTAMP: sensor.timestamp + } + + _LOGGER.debug(f"Response {response}") + return response + + return {} \ No newline at end of file diff --git a/custom_components/mistral_ai_api/const.py b/custom_components/mistral_ai_api/const.py index 5c575e9..107db0c 100644 --- a/custom_components/mistral_ai_api/const.py +++ b/custom_components/mistral_ai_api/const.py @@ -1,7 +1,12 @@ -VERSION = "0.4.0" +VERSION = "0.5.0" DOMAIN = "mistral_ai_api" ATTR_LAST_PROMPT = "last_prompt" ATTR_LAST_RESPONSE = "last_response" ATTR_TIMESTAMP = "timestamp" ATTR_IDENTIFIER = "identifier" + +STATE_PROCESSING = "processing" +STATE_IDLE = "idle" + +EV_PROVIDE_RESPONSE = "mistral_ai_response" \ No newline at end of file diff --git a/custom_components/mistral_ai_api/manifest.json b/custom_components/mistral_ai_api/manifest.json index 5eec03b..4055c4b 100644 --- a/custom_components/mistral_ai_api/manifest.json +++ b/custom_components/mistral_ai_api/manifest.json @@ -4,7 +4,7 @@ "documentation": "https://github.com/BlaXun/home_assistant_mistral_ai", "dependencies": [], "codeowners": ["@blaxun"], - "version": "0.4.0", + "version": "0.5.0", "requirements": [], "config_flow": false, "single_config_entry": true, diff --git a/custom_components/mistral_ai_api/sensor.py b/custom_components/mistral_ai_api/sensor.py index 9938002..fff4a05 100644 --- a/custom_components/mistral_ai_api/sensor.py +++ b/custom_components/mistral_ai_api/sensor.py @@ -18,7 +18,7 @@ class MistralAiSensor(Entity): def __init__(self, hass: HomeAssistant, data): self.entity_id = generate_entity_id( - entity_id_format="sensor.{}", name="mistral_ai_api", hass=hass + entity_id_format="sensor.{}", name=DOMAIN, hass=hass ) self.hass = hass @@ -58,6 +58,34 @@ def state(self): def extra_state_attributes(self): return self._attributes + @property + def identifier(self): + return self._attributes[ATTR_IDENTIFIER] + + @identifier.setter + def identifier(self, value): + self._attributes[ATTR_IDENTIFIER] = value + + @property + def last_response(self): + return self._attributes[ATTR_LAST_RESPONSE] + + @last_response.setter + def last_response(self, value): + self._attributes[ATTR_LAST_RESPONSE] = value + + @property + def last_prompt(self): + return self._attributes[ATTR_LAST_PROMPT] + + @last_prompt.setter + def last_prompt(self, value): + self._attributes[ATTR_LAST_PROMPT] = value + + @property + def timestamp(self): + return self._attributes[ATTR_TIMESTAMP] + def set_state(self, state: str): self._state = state diff --git a/custom_components/mistral_ai_api/services.yaml b/custom_components/mistral_ai_api/services.yaml index 9dc9941..95508e6 100644 --- a/custom_components/mistral_ai_api/services.yaml +++ b/custom_components/mistral_ai_api/services.yaml @@ -44,3 +44,6 @@ send_prompt: number: min: 10 max: 320 + +# Retrieve Last Communication +retrieve_last: \ No newline at end of file diff --git a/custom_components/mistral_ai_api/translations/en.json b/custom_components/mistral_ai_api/translations/en.json index 191d66c..bde3857 100644 --- a/custom_components/mistral_ai_api/translations/en.json +++ b/custom_components/mistral_ai_api/translations/en.json @@ -25,6 +25,10 @@ "description": "Timeout in seconds. 60 should be default." } } + }, + "retrieve_last": { + "name": "Retrieve last communication", + "description": "Retrieves the last sent prompt and response (if it exists)" } } } \ No newline at end of file