Add HassGetCurrentDate and HassGetCurrentTime intents (#121707)

This commit is contained in:
Michael Hansen 2024-07-10 17:17:04 -05:00 committed by GitHub
parent 0462582da3
commit 417abda649
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 65 additions and 7 deletions

View File

@ -6,5 +6,5 @@
"documentation": "https://www.home-assistant.io/integrations/conversation",
"integration_type": "system",
"quality_scale": "internal",
"requirements": ["hassil==1.7.3", "home-assistant-intents==2024.7.3"]
"requirements": ["hassil==1.7.4", "home-assistant-intents==2024.7.10"]
}

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from datetime import datetime
import logging
from typing import Any, Protocol
@ -120,6 +121,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
intent.async_register(hass, PauseTimerIntentHandler())
intent.async_register(hass, UnpauseTimerIntentHandler())
intent.async_register(hass, TimerStatusIntentHandler())
intent.async_register(hass, GetCurrentDateIntentHandler())
intent.async_register(hass, GetCurrentTimeIntentHandler())
return True
@ -370,6 +373,30 @@ class SetPositionIntentHandler(intent.DynamicServiceIntentHandler):
raise intent.IntentHandleError(f"Domain not supported: {state.domain}")
class GetCurrentDateIntentHandler(intent.IntentHandler):
"""Gets the current date."""
intent_type = intent.INTENT_GET_CURRENT_DATE
description = "Gets the current date"
async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse:
response = intent_obj.create_response()
response.async_set_speech_slots({"date": datetime.now().date()})
return response
class GetCurrentTimeIntentHandler(intent.IntentHandler):
"""Gets the current time."""
intent_type = intent.INTENT_GET_CURRENT_TIME
description = "Gets the current time"
async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse:
response = intent_obj.create_response()
response.async_set_speech_slots({"time": datetime.now().time()})
return response
async def _async_process_intent(
hass: HomeAssistant, domain: str, platform: IntentPlatformProtocol
) -> None:

View File

@ -54,6 +54,8 @@ INTENT_DECREASE_TIMER = "HassDecreaseTimer"
INTENT_PAUSE_TIMER = "HassPauseTimer"
INTENT_UNPAUSE_TIMER = "HassUnpauseTimer"
INTENT_TIMER_STATUS = "HassTimerStatus"
INTENT_GET_CURRENT_DATE = "HassGetCurrentDate"
INTENT_GET_CURRENT_TIME = "HassGetCurrentTime"
SLOT_SCHEMA = vol.Schema({}, extra=vol.ALLOW_EXTRA)

View File

@ -277,6 +277,8 @@ class AssistAPI(API):
intent.INTENT_GET_STATE,
intent.INTENT_NEVERMIND,
intent.INTENT_TOGGLE,
intent.INTENT_GET_CURRENT_DATE,
intent.INTENT_GET_CURRENT_TIME,
}
def __init__(self, hass: HomeAssistant) -> None:

View File

@ -30,10 +30,10 @@ ha-av==10.1.1
ha-ffmpeg==3.2.0
habluetooth==3.1.3
hass-nabucasa==0.81.1
hassil==1.7.3
hassil==1.7.4
home-assistant-bluetooth==1.12.2
home-assistant-frontend==20240710.0
home-assistant-intents==2024.7.3
home-assistant-intents==2024.7.10
httpx==0.27.0
ifaddr==0.2.0
Jinja2==3.1.4

View File

@ -1062,7 +1062,7 @@ hass-nabucasa==0.81.1
hass-splunk==0.1.1
# homeassistant.components.conversation
hassil==1.7.3
hassil==1.7.4
# homeassistant.components.jewish_calendar
hdate==0.10.9
@ -1099,7 +1099,7 @@ holidays==0.52
home-assistant-frontend==20240710.0
# homeassistant.components.conversation
home-assistant-intents==2024.7.3
home-assistant-intents==2024.7.10
# homeassistant.components.home_connect
homeconnect==0.7.2

View File

@ -876,7 +876,7 @@ habluetooth==3.1.3
hass-nabucasa==0.81.1
# homeassistant.components.conversation
hassil==1.7.3
hassil==1.7.4
# homeassistant.components.jewish_calendar
hdate==0.10.9
@ -904,7 +904,7 @@ holidays==0.52
home-assistant-frontend==20240710.0
# homeassistant.components.conversation
home-assistant-intents==2024.7.3
home-assistant-intents==2024.7.10
# homeassistant.components.home_connect
homeconnect==0.7.2

View File

@ -1,7 +1,9 @@
"""Test intents for the default agent."""
from datetime import datetime
from unittest.mock import patch
from freezegun import freeze_time
import pytest
from homeassistant.components import (
@ -413,3 +415,28 @@ async def test_todo_add_item_fr(
assert mock_handle.call_args.args
intent_obj = mock_handle.call_args.args[0]
assert intent_obj.slots.get("item", {}).get("value", "").strip() == "farine"
@freeze_time(datetime(year=2013, month=9, day=17, hour=1, minute=2))
async def test_date_time(
hass: HomeAssistant,
init_components,
) -> None:
"""Test the date and time intents."""
result = await conversation.async_converse(
hass, "what is the date", None, Context(), None
)
await hass.async_block_till_done()
response = result.response
assert response.response_type == intent.IntentResponseType.ACTION_DONE
assert response.speech["plain"]["speech"] == "September 17th, 2013"
result = await conversation.async_converse(
hass, "what time is it", None, Context(), None
)
await hass.async_block_till_done()
response = result.response
assert response.response_type == intent.IntentResponseType.ACTION_DONE
assert response.speech["plain"]["speech"] == "1:02 AM"