Skip to content

Commit

Permalink
Add HassGetDate and HassGetTime (#2300)
Browse files Browse the repository at this point in the history
  • Loading branch information
synesthesiam authored Jul 10, 2024
1 parent 3b877c2 commit 698fc4e
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 0 deletions.
10 changes: 10 additions & 0 deletions intents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ HassSetPosition:
- "domain"
- "device_class"

HassGetCurrentDate:
supported: true
domain: homeassistant
description: "Gets the current date"

HassGetCurrentTime:
supported: true
domain: homeassistant
description: "Gets the current time"

# -----------------------------------------------------------------------------
# light
# -----------------------------------------------------------------------------
Expand Down
39 changes: 39 additions & 0 deletions responses/en/HassGetCurrentDate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
language: en
responses:
intents:
HassGetCurrentDate:
default: >
{% set ordinal = {
1: '1st',
2: '2nd',
3: '3rd',
4: '4th',
5: '5th',
6: '6th',
7: '7th',
8: '8th',
9: '9th',
10: '10th',
11: '11th',
12: '12th',
13: '13th',
14: '14th',
15: '15th',
16: '16th',
17: '17th',
18: '18th',
19: '19th',
20: '20th',
21: '21st',
22: '22nd',
23: '23rd',
24: '24th',
25: '25th',
26: '26th',
27: '27th',
28: '28th',
29: '29th',
30: '30th',
31: '31st',
} %}
{{ slots.date.strftime('%B') }} {{ ordinal[slots.date.day] }}, {{ slots.date.year }}
11 changes: 11 additions & 0 deletions responses/en/HassGetCurrentTime.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: en
responses:
intents:
HassGetCurrentTime:
default: >
{% set time_str = slots.time.strftime('%I:%M %p') %}
{% if time_str.startswith('0'): -%}
{{ time_str[1:] }}
{% else: %}
{{ time_str }}
{% endif %}
38 changes: 38 additions & 0 deletions script/intentfest/generate_day_ordinals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Generate a map for day numbers to ordinal words."""

from __future__ import annotations

import argparse
import json
from typing import Dict

from unicode_rbnf import RbnfEngine, RulesetName

from .const import LANGUAGES
from .util import get_base_arg_parser


def get_arguments() -> argparse.Namespace:
"""Get parsed passed in arguments."""
parser = get_base_arg_parser()
parser.add_argument(
"--language",
required=True,
type=str,
choices=LANGUAGES,
help="The language to validate.",
)
return parser.parse_args()


def run() -> int:
args = get_arguments()

engine = RbnfEngine.for_language(args.language)
ordinals: Dict[int, str] = {}
for day in range(1, 32):
ordinals[day] = engine.format_number(day, ruleset_name=RulesetName.ORDINAL)

print(json.dumps(ordinals, ensure_ascii=False, indent=2))

return 0
1 change: 1 addition & 0 deletions script/intentfest/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def get_base_arg_parser() -> argparse.ArgumentParser:
"website_summary",
"validate",
"language_table",
"generate_day_ordinals",
],
)
parser.add_argument("--debug", action="store_true", help="Enable log output")
Expand Down
4 changes: 4 additions & 0 deletions script/intentfest/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
from collections import Counter, defaultdict
from collections.abc import Callable
from datetime import datetime
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -572,6 +573,9 @@ def validate_language(
# For timer intents
slots["timers"] = []

# For date/time intents
slots["date"] = slots["time"] = datetime.now()

for response_key, response_template in intent_responses.items():
possible_response_keys.add(response_key)
if response_key not in used_intent_response_keys:
Expand Down
7 changes: 7 additions & 0 deletions sentences/en/homeassistant_HassGetCurrentDate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: en
intents:
HassGetCurrentDate:
data:
- sentences:
- "(<what_is>|tell me) (todays|today's|the current) date"
- "(<what_is>|tell me) the date[ today]"
7 changes: 7 additions & 0 deletions sentences/en/homeassistant_HassGetCurrentTime.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: en
intents:
HassGetCurrentTime:
data:
- sentences:
- "(<what_is>|tell me) the [current] time"
- "what time is it[ [right ]now]"
7 changes: 7 additions & 0 deletions shared/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass, field
from datetime import datetime
from functools import partial
from typing import Any, Dict, Optional, List, Set, Tuple

Expand All @@ -8,6 +9,8 @@
from hassil.sample import sample_expression
from jinja2 import BaseLoader, Environment, StrictUndefined

_TEST_DATETIME = datetime(year=2013, month=9, day=17, hour=1, minute=2)


@dataclass
class State:
Expand Down Expand Up @@ -274,6 +277,10 @@ def render_response(
else:
slots["timers"] = []

# For date/time intents
slots["date"] = _TEST_DATETIME
slots["time"] = _TEST_DATETIME

return env.from_string(response_template).render(
{
"slots": slots,
Expand Down
10 changes: 10 additions & 0 deletions tests/en/homeassistant_HassGetCurrentDate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: en
tests:
- sentences:
- "what's the date"
- "what's the date today"
- "what is today's date"
- "tell me the date"
intent:
name: HassGetCurrentDate
response: September 17th, 2013
10 changes: 10 additions & 0 deletions tests/en/homeassistant_HassGetCurrentTime.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: en
tests:
- sentences:
- "what's the time"
- "what time is it"
- "what time is it right now"
- "tell me the time"
intent:
name: HassGetCurrentTime
response: 1:02 AM

0 comments on commit 698fc4e

Please sign in to comment.