Skip to content

Commit

Permalink
add time specific requests (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
emphasize committed Aug 15, 2023
1 parent 9731dd4 commit 817e479
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 123 deletions.
110 changes: 36 additions & 74 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,35 @@ def use_24h(self) -> bool:
return self.config_core.get("time_format", "full") == "full"

@intent_handler(
IntentBuilder("current_weather")
IntentBuilder("weather")
.optionally("query")
.one_of("weather", "forecast")
.optionally("relative-time")
.optionally("relative-day")
.optionally("today")
.optionally("location")
.optionally("unit")
)
def handle_current_weather(self, message: Message):
"""Handle current weather requests such as: what is the weather like?
.optionally("unit"))
def handle_weather(self, message: Message):
"""
Handle weather requests of various timeframes.
The intents gets routed accordingly
Examples:
"What's the weather like?" (current)
"How's the weather tomorrow?" (daily)
"What's the forecast for friday 9 pm?" (hourly)
"what's tomorrow's forecast in Seattle?"
Args:
message: Message Bus event information from the intent parser
"""
self._report_current_weather(message)
intent = self._get_intent_data(message)
if intent.timeframe == DAILY:
self._report_one_day_forecast(intent)
elif intent.timeframe == HOURLY:
self._report_hourly_weather(intent)
else:
self._report_current_weather(intent)

@intent_handler(
IntentBuilder("outside")
Expand All @@ -110,12 +126,14 @@ def handle_current_weather(self, message: Message):
.optionally("unit")
)
def handle_outside(self, message: Message):
"""Handle current weather requests such as: what's it like outside?
"""
Handle current weather requests such as: what's it like outside?
Args:
message: Message Bus event information from the intent parser
"""
self._report_current_weather(message)
intent = self._get_intent_data(message)
self._report_current_weather(intent)

@intent_handler(
IntentBuilder("N_days_forecast")
Expand Down Expand Up @@ -143,60 +161,6 @@ def handle_number_days_forecast(self, message: Message):
days = int(extract_number(message.data["utterance"], lang=self.lang))
self._report_multi_day_forecast(message, days)

@intent_handler(
IntentBuilder("one_day_forecast")
.optionally("query")
.one_of("weather", "forecast")
.one_of("relative-day", "today")
.optionally("location")
.optionally("unit")
)
def handle_one_day_forecast(self, message):
"""Handle forecast for a single day.
Examples:
"What is the weather forecast tomorrow?"
"What is the weather forecast on Tuesday in Baltimore?"
Args:
message: Message Bus event information from the intent parser
"""
self._report_one_day_forecast(message)

@intent_handler(
IntentBuilder("weather_later")
.require("query")
.require("weather")
.require("later")
.optionally("location")
.optionally("unit")
)
def handle_weather_later(self, message: Message):
"""Handle future weather requests such as: what's the weather later?
Args:
message: Message Bus event information from the intent parser
"""
self._report_one_hour_weather(message)

@intent_handler(
IntentBuilder("weather_at_time")
.optionally("query")
.one_of("weather", "forecast")
.require("relative-time")
.optionally("relative-day")
.optionally("today")
.optionally("location")
.optionally("unit")
)
def handle_weather_at_time(self, message: Message):
"""Handle future weather requests such as: what's the weather tonight?
Args:
message: Message Bus event information from the intent parser
"""
self._report_one_hour_weather(message)

@intent_handler(
IntentBuilder("weekend_forecast")
.require("query")
Expand Down Expand Up @@ -602,13 +566,12 @@ def _display_sunrise_sunset(self, forecast: Weather, weather_location: str):
self.gui["sunset"] = self._format_time(forecast.sunset)
self.gui.show_page("SunriseSunset.qml")

def _report_current_weather(self, message: Message):
def _report_current_weather(self, intent_data: WeatherIntent):
"""Handles all requests for current weather conditions.
Args:
message: Message Bus event information from the intent parser
"""
intent_data = self._get_intent_data(message)
weather = self._get_weather(intent_data)
if weather is not None:
self._display_current_conditions(weather, intent_data.display_location)
Expand Down Expand Up @@ -653,13 +616,12 @@ def _display_current_conditions(self, weather: WeatherReport, weather_location:
weather.current.condition.code, weather.current.temperature
)

def _report_one_hour_weather(self, message: Message):
def _report_hourly_weather(self, intent_data: WeatherIntent):
"""Handles requests for a one hour forecast.
Args:
message: Message Bus event information from the intent parser
"""
intent_data = self._get_intent_data(message)
weather = self._get_weather(intent_data)
if weather is not None:
try:
Expand Down Expand Up @@ -708,14 +670,12 @@ def _display_hourly_forecast(self, weather: List[Weather], weather_location: str
self.gui["hourlyForecast"] = dict(hours=hourly_forecast)
self.gui.show_page("HourlyForecast.qml")

def _report_one_day_forecast(self, message: Message):
def _report_one_day_forecast(self, intent_data: WeatherIntent):
"""Handles all requests for a single day forecast.
Args:
message: Message Bus event information from the intent parser
"""
weather_config = self._get_weather_config(message)
intent_data = WeatherIntent(message, weather_config)
weather = self._get_weather(intent_data)
if weather is not None:
forecast = weather.get_forecast_for_date(intent_data)
Expand Down Expand Up @@ -961,11 +921,13 @@ def _get_intent_data(self, message: Message) -> WeatherIntent:
self.speak_dialog("cant-get-forecast")
else:
unit = message.data.get("unit")
if self.voc_match(intent_data.utterance, "relative-day") or \
self.voc_match(intent_data.utterance, "today"):
intent_data.timeframe = DAILY
if self.voc_match(intent_data.utterance, "relative-time"):
intent_data.timeframe = HOURLY
_dt = intent_data.intent_datetime

if _dt != intent_data.location_datetime: # ie current
if _dt.hour == 0 and _dt.minute == 0:
intent_data.timeframe = DAILY
else:
intent_data.timeframe = HOURLY
elif self.voc_match(intent_data.utterance, "later"):
intent_data.timeframe = HOURLY

Expand Down
48 changes: 0 additions & 48 deletions locale/de-de/vocabulary/date-time/relative-time.voc
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,3 @@ nacht
nachts
mitternacht
mitternachts
01:00 uhr
1 uhr
02:00 uhr
2 uhr
03:00 uhr
3 uhr
04:00 uhr
4 uhr
05:00 uhr
5 uhr
06:00 uhr
6 uhr
07:00 uhr
7 uhr
08:00 uhr
8 uhr
09:00 uhr
9 uhr
10:00 uhr
10 uhr
11:00 uhr
11 uhr
12:00 uhr
12 uhr
13:00 uhr
13 uhr
14:00 uhr
14 uhr
15:00 uhr
15 uhr
16:00 uhr
16 uhr
17:00 uhr
17 uhr
18:00 uhr
18 uhr
19:00 uhr
19 uhr
20:00 uhr
20 uhr
21:00 uhr
21 uhr
22:00 uhr
22 uhr
23:00 uhr
23 uhr
24:00 uhr
24 uhr
3 changes: 2 additions & 1 deletion locale/en-us/vocabulary/date-time/relative-time.voc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
morning
noon
afternoon
evening
night
tonight
overnight
midnight
1 change: 1 addition & 0 deletions weather_helpers/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, message, weather_config: WeatherConfig):
:param message: Intent data from the message bus
:param language: The configured language of the device
"""
self.message = message
self.utterance = normalize(message.data["utterance"],
weather_config.lang,
remove_articles=False)
Expand Down

0 comments on commit 817e479

Please sign in to comment.