Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RandomNumber and CurrentTime intents #5

Merged
merged 8 commits into from
Jun 16, 2024
67 changes: 67 additions & 0 deletions custom_components/custom_sentences/intent.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
"""Intent handlers."""

import datetime
import random
import typing

import homeassistant.helpers.config_validation as cv
import pytz
import voluptuous as vol
from homeassistant.core import HomeAssistant
from homeassistant.helpers import intent


async def async_setup_intents(hass: HomeAssistant) -> None:
"""Set up the custom intent handlers."""
intent.async_register(hass, ConversationProcessIntentHandler())
intent.async_register(hass, RandomNumberIntentHandler())
intent.async_register(hass, CurrentTimeIntentHandler())


class ConversationProcessIntentHandler(intent.IntentHandler):
Expand Down Expand Up @@ -74,3 +80,64 @@ async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse
response = intent_obj.create_response()
response.async_set_speech(f"{name} says '{response_text}'")
return response


class RandomNumberIntentHandler(intent.IntentHandler):
"""Handle RandomNumber intents."""

# Type of intent to handle
intent_type = "RandomNumber"

description = "Generate a random number in a range."

# Optional. A validation schema for slots
slot_schema: typing.ClassVar[dict] = {
"from": vol.All(vol.Coerce(int), vol.Range(min=0, max=999999999)),
"to": vol.All(vol.Coerce(int), vol.Range(min=0, max=999999999)),
}

async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse:
"""Handle the intent."""
# Extract the slot values
slots = intent_obj.slots
from_value = int(slots["from"]["value"])
to_value = int(slots["to"]["value"])

# Generate a random number
result = random.randint(from_value, to_value) # noqa: S311

# Create and return a response
response = intent_obj.create_response()
response.async_set_speech(f"{result}")
return response


class CurrentTimeIntentHandler(intent.IntentHandler):
"""Handle CurrentTime intents."""

# Type of intent to handle
intent_type = "CurrentTime"

description = "Get the current time."

async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse:
"""Handle the intent."""
# Retrieve the user's timezone from Home Assistant configuration
user_timezone = intent_obj.hass.config.time_zone

# Create a timezone object using pytz
timezone = pytz.timezone(user_timezone)

# Get the current time in UTC
utc_now = datetime.datetime.now(datetime.UTC)

# Convert the current UTC time to the user's timezone
user_time = utc_now.astimezone(timezone)

# Format the time as a string
result = user_time.strftime("%H:%M")

# Create and return a response
response = intent_obj.create_response()
response.async_set_speech(f"The time is {result}")
return response
23 changes: 21 additions & 2 deletions custom_sentences/en/custom_sentences.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
language: "en"
language: 'en'
intents:
ConversationProcess:
data:
- sentences:
- "(Ask | Tell) {name} [to] {text}"
- '(Ask | Tell) {name} [to] {text}'
requires_context:
domain: conversation
RandomNumber:
data:
- sentences:
- '(Pick|Generate) a [random] number between {random_range:from} and {random_range:to}'
- '(Pick|Generate) a [random] number [from] {random_range:from} (through | to) {random_range:to}'
- sentences:
- '(Pick|Generate) a [random] number (below | up to) {random_range:to}'
slots:
from: 1
CurrentTime:
data:
- sentences:
- "What's the time"
- 'What is the time'
- 'What time is it'
lists:
text:
wildcard: true
random_range:
range:
from: 0
to: 999999999
Loading