Skip to content

Commit 935b1a4

Browse files
authored
Handle a lot more things with padatious
1 parent 8a11bae commit 935b1a4

6 files changed

+73
-47
lines changed

__init__.py

+53-47
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from multi_key_dict import multi_key_dict
2222
from mycroft.dialog import DialogLoader
2323
from mycroft.api import Api
24-
from mycroft.skills.core import MycroftSkill, intent_handler
24+
from mycroft.skills.core import MycroftSkill, intent_handler, intent_file_handler
2525
from mycroft.util.log import LOG
2626
from mycroft.util.parse import extract_datetime
2727
from mycroft.util.format import nice_number
@@ -54,6 +54,9 @@ def __init__(self):
5454
self.observation = ObservationParser()
5555
self.forecast = ForecastParser()
5656

57+
def initialize(self):
58+
self.register_entity_file('location.entity')
59+
5760
def build_query(self, params):
5861
params.get("query").update({"lang": self.lang})
5962
return params.get("query")
@@ -140,8 +143,7 @@ def __init__(self):
140143
self.owm = OWMApi()
141144

142145
# Handle: what is the weather like?
143-
@intent_handler(IntentBuilder("CurrentWeatherIntent").require(
144-
"Weather").optionally("Location").build())
146+
@intent_file_handler('weather.intent')
145147
def handle_current_weather(self, message):
146148
try:
147149
# Get a date from requests like "weather for next Tuesday"
@@ -175,13 +177,12 @@ def handle_current_weather(self, message):
175177

176178
self.__report_weather("current", report)
177179
except HTTPError as e:
178-
self.__api_error(e)
180+
self.speak_dialog('location.not.found', data={"location":report['location']})
179181
except Exception as e:
180182
LOG.error("Error: {0}".format(e))
181183

182184
# Handle: What is the weather forecast?
183-
@intent_handler(IntentBuilder("WeatherForecast").require(
184-
"Forecast").optionally("Location").build())
185+
@intent_file_handler('forecast.intent')
185186
def handle_forecast(self, message):
186187
try:
187188
report = self.__initialize_report(message)
@@ -215,15 +216,13 @@ def handle_forecast(self, message):
215216

216217
self.__report_weather("forecast", report)
217218
except HTTPError as e:
218-
self.__api_error(e)
219+
self.speak_dialog('location.not.found', data={"location":report['location']})
219220
except Exception as e:
220221
LOG.error("Error: {0}".format(e))
221222

222223
# Handle: When will it rain again? | Will it rain on Tuesday?
223-
@intent_handler(IntentBuilder("NextPrecipitationIntent").require(
224-
"Next").require("Precipitation").optionally("Location").build())
225-
@intent_handler(IntentBuilder("CurrentRainSnowIntent").require(
226-
"Query").require("Precipitation").optionally("Location").build())
224+
@intent_file_handler('next.precipitation.intent')
225+
@intent_file_handler('current.precipitation.intent')
227226
def handle_next_precipitation(self, message):
228227
report = self.__initialize_report(message)
229228

@@ -243,43 +242,48 @@ def handle_next_precipitation(self, message):
243242
}
244243

245244
# search the forecast for precipitation
246-
for weather in self.owm.daily_forecast(
247-
report['full_location'],
248-
report['lat'],
249-
report['lon']).get_forecast().get_weathers():
245+
try:
246+
for weather in self.owm.daily_forecast(
247+
report['full_location'],
248+
report['lat'],
249+
report['lon']).get_forecast().get_weathers():
250250

251-
forecastDate = datetime.fromtimestamp(weather.get_reference_time())
252-
253-
# TODO: "will it rain tomorrow" returns forecast for today, if it rains today
254-
if when != today:
255-
# User asked about a specific date, is this it?
256-
whenGMT = self.__to_GMT(when)
257-
if forecastDate.date() != whenGMT.date():
258-
continue
259-
260-
rain = weather.get_rain()
261-
if rain and rain["all"] > 0:
262-
data["precip"] = "rain"
263-
data["day"] = self.__to_day(forecastDate)
264-
if rain["all"] < 10:
265-
data["modifier"] = self.__translate("light")
266-
elif rain["all"] > 20:
267-
data["modifier"] = self.__translate("heavy")
268-
269-
break
270-
271-
snow = weather.get_snow()
272-
if snow and snow["all"] > 0:
273-
data["precip"] = "snow"
274-
data["day"] = self.__to_day(forecastDate)
275-
if snow["all"] < 10:
276-
data["modifier"] = self.__translate("light")
277-
elif snow["all"] > 20:
278-
data["modifier"] = self.__translate("heavy")
279-
280-
break
251+
forecastDate = datetime.fromtimestamp(weather.get_reference_time())
281252

282-
self.__report_precipitation(timeframe, data)
253+
# TODO: "will it rain tomorrow" returns forecast for today, if it rains today
254+
if when != today:
255+
# User asked about a specific date, is this it?
256+
whenGMT = self.__to_GMT(when)
257+
if forecastDate.date() != whenGMT.date():
258+
continue
259+
260+
rain = weather.get_rain()
261+
if rain and rain["all"] > 0:
262+
data["precip"] = "rain"
263+
data["day"] = self.__to_day(forecastDate)
264+
if rain["all"] < 10:
265+
data["modifier"] = self.__translate("light")
266+
elif rain["all"] > 20:
267+
data["modifier"] = self.__translate("heavy")
268+
269+
break
270+
271+
snow = weather.get_snow()
272+
if snow and snow["all"] > 0:
273+
data["precip"] = "snow"
274+
data["day"] = self.__to_day(forecastDate)
275+
if snow["all"] < 10:
276+
data["modifier"] = self.__translate("light")
277+
elif snow["all"] > 20:
278+
data["modifier"] = self.__translate("heavy")
279+
280+
break
281+
282+
self.__report_precipitation(timeframe, data)
283+
except HTTPError as e:
284+
self.speak_dialog('location.not.found', data={"location":report['location']})
285+
except Exception as e:
286+
LOG.error("Error: {0}".format(e))
283287

284288
# Handle: What's the weather later?
285289
@intent_handler(IntentBuilder("NextHoursWeatherIntent").require(
@@ -306,7 +310,7 @@ def handle_next_hour(self, message):
306310

307311
self.__report_weather("hour", report)
308312
except HTTPError as e:
309-
self.__api_error(e)
313+
self.speak_dialog('location.not.found', data={"location":report['location']})
310314
except Exception as e:
311315
LOG.error("Error: {0}".format(e))
312316

@@ -464,6 +468,8 @@ def __get_location(self, message):
464468
# is found return the default location instead.
465469
try:
466470
location = message.data.get("Location", None)
471+
if location is None:
472+
location = message.data.get("location", None)
467473
if location:
468474
return None, None, location, location
469475

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
is it (raining|snowing)
2+
does it (snow|rain)
3+
is there (rain|snow)
4+
is it (raining|snowing) (in|at) {location}
5+
does it (snow|rain) (in|at) {location}
6+
is there (rain|snow) (in|at) {location}
7+
should i wear (a coat|an umbrella)
8+
should i take an umbrella (with me|)

vocab/en-us/forecast.intent

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
what is the forecast :0
2+
what is the weather (going to be like|forecast)

vocab/en-us/location.entity

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:0

vocab/en-us/next.precipitation.intent

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(when |)will it (rain|snow|be raining|be snowing)( again|)( in {location}|)( on|)
2+
when (will|is) (the next|there be)( a|) (rain|storm|snow|snow storm|thunder|thunder storm)( going to|) be( again|)( in {location}|)

vocab/en-us/weather.intent

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
what is the weather (like|) (here|around|in {location}|)
2+
how (cold|hot|warm|chilly) is it (here|around|outside |in {location}|)
3+
how is the weather (in {location}|)
4+
what is the (outside|) temperature (here|around|outside |in {location}|)
5+
how is it outside
6+
what is it like outside
7+
what will the weather be like

0 commit comments

Comments
 (0)