21
21
from multi_key_dict import multi_key_dict
22
22
from mycroft .dialog import DialogLoader
23
23
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
25
25
from mycroft .util .log import LOG
26
26
from mycroft .util .parse import extract_datetime
27
27
from mycroft .util .format import nice_number
@@ -54,6 +54,9 @@ def __init__(self):
54
54
self .observation = ObservationParser ()
55
55
self .forecast = ForecastParser ()
56
56
57
+ def initialize (self ):
58
+ self .register_entity_file ('location.entity' )
59
+
57
60
def build_query (self , params ):
58
61
params .get ("query" ).update ({"lang" : self .lang })
59
62
return params .get ("query" )
@@ -140,8 +143,7 @@ def __init__(self):
140
143
self .owm = OWMApi ()
141
144
142
145
# Handle: what is the weather like?
143
- @intent_handler (IntentBuilder ("CurrentWeatherIntent" ).require (
144
- "Weather" ).optionally ("Location" ).build ())
146
+ @intent_file_handler ('weather.intent' )
145
147
def handle_current_weather (self , message ):
146
148
try :
147
149
# Get a date from requests like "weather for next Tuesday"
@@ -175,13 +177,12 @@ def handle_current_weather(self, message):
175
177
176
178
self .__report_weather ("current" , report )
177
179
except HTTPError as e :
178
- self .__api_error ( e )
180
+ self .speak_dialog ( 'location.not.found' , data = { "location" : report [ 'location' ]} )
179
181
except Exception as e :
180
182
LOG .error ("Error: {0}" .format (e ))
181
183
182
184
# Handle: What is the weather forecast?
183
- @intent_handler (IntentBuilder ("WeatherForecast" ).require (
184
- "Forecast" ).optionally ("Location" ).build ())
185
+ @intent_file_handler ('forecast.intent' )
185
186
def handle_forecast (self , message ):
186
187
try :
187
188
report = self .__initialize_report (message )
@@ -215,15 +216,13 @@ def handle_forecast(self, message):
215
216
216
217
self .__report_weather ("forecast" , report )
217
218
except HTTPError as e :
218
- self .__api_error ( e )
219
+ self .speak_dialog ( 'location.not.found' , data = { "location" : report [ 'location' ]} )
219
220
except Exception as e :
220
221
LOG .error ("Error: {0}" .format (e ))
221
222
222
223
# 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' )
227
226
def handle_next_precipitation (self , message ):
228
227
report = self .__initialize_report (message )
229
228
@@ -243,43 +242,48 @@ def handle_next_precipitation(self, message):
243
242
}
244
243
245
244
# 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 ():
250
250
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 ())
281
252
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 ))
283
287
284
288
# Handle: What's the weather later?
285
289
@intent_handler (IntentBuilder ("NextHoursWeatherIntent" ).require (
@@ -306,7 +310,7 @@ def handle_next_hour(self, message):
306
310
307
311
self .__report_weather ("hour" , report )
308
312
except HTTPError as e :
309
- self .__api_error ( e )
313
+ self .speak_dialog ( 'location.not.found' , data = { "location" : report [ 'location' ]} )
310
314
except Exception as e :
311
315
LOG .error ("Error: {0}" .format (e ))
312
316
@@ -464,6 +468,8 @@ def __get_location(self, message):
464
468
# is found return the default location instead.
465
469
try :
466
470
location = message .data .get ("Location" , None )
471
+ if location is None :
472
+ location = message .data .get ("location" , None )
467
473
if location :
468
474
return None , None , location , location
469
475
0 commit comments