Skip to content

Commit

Permalink
0.5.0 Added new service action to retrieve last communication
Browse files Browse the repository at this point in the history
  • Loading branch information
ilie1337 committed Dec 23, 2024
1 parent 4986b52 commit 4b7cd7d
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 11 deletions.
12 changes: 10 additions & 2 deletions custom_components/mistral_ai_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand All @@ -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


Expand Down
33 changes: 27 additions & 6 deletions custom_components/mistral_ai_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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:
Expand Down Expand Up @@ -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()

Expand All @@ -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:
Expand All @@ -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 {}
7 changes: 6 additions & 1 deletion custom_components/mistral_ai_api/const.py
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion custom_components/mistral_ai_api/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 29 additions & 1 deletion custom_components/mistral_ai_api/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions custom_components/mistral_ai_api/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ send_prompt:
number:
min: 10
max: 320

# Retrieve Last Communication
retrieve_last:
4 changes: 4 additions & 0 deletions custom_components/mistral_ai_api/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
}
}

0 comments on commit 4b7cd7d

Please sign in to comment.