|
@@ -30,6 +30,7 @@ import time
|
30
|
30
|
|
31
|
31
|
import requests
|
32
|
32
|
|
|
33
|
+from kayleevc.pluginutils import *
|
33
|
34
|
from . import PluginBase, Handler
|
34
|
35
|
|
35
|
36
|
|
|
@@ -112,37 +113,6 @@ class DarkSkyHandler(Handler):
|
112
|
113
|
with open(self._cache_filename, 'r') as f:
|
113
|
114
|
return json.load(f)
|
114
|
115
|
|
115
|
|
- def _format_temperature(self, temperature, unit='Fahrenheit'):
|
116
|
|
- """Format a temperature for the TTS system"""
|
117
|
|
- return f'{temperature:.{self._temp_precision}f} degrees {unit}'
|
118
|
|
-
|
119
|
|
- def _format_percent(self, value):
|
120
|
|
- """Format a percentage value for the TTS system"""
|
121
|
|
- return f'{100*value:.0f} percent'
|
122
|
|
-
|
123
|
|
- def _format_time(self, epoch_time):
|
124
|
|
- """Format a time in seconds since the epoch for the TTS system"""
|
125
|
|
- t = time.localtime(epoch_time)
|
126
|
|
-
|
127
|
|
- # Format each part of the time to be pronounced nicely
|
128
|
|
- hour = time.strftime('%I', t)
|
129
|
|
- if hour[0] == '0':
|
130
|
|
- hour = hour[1]
|
131
|
|
-
|
132
|
|
- minute = time.strftime('%M', t)
|
133
|
|
- if minute[0] == '0':
|
134
|
|
- if minute[1] == '0':
|
135
|
|
- minute = "o'clock"
|
136
|
|
- else:
|
137
|
|
- minute = 'O' + minute[1]
|
138
|
|
-
|
139
|
|
- ante_post = time.strftime('%p', t)
|
140
|
|
- if ante_post[0] == 'A':
|
141
|
|
- ante_post = 'AE ' + ante_post[1]
|
142
|
|
-
|
143
|
|
- # Put the parts together
|
144
|
|
- return f'{hour} {minute} {ante_post}'
|
145
|
|
-
|
146
|
116
|
def __call__(self, tts):
|
147
|
117
|
"""Update the cache if necessary and load it"""
|
148
|
118
|
self._update_cache_if_stale()
|
|
@@ -155,7 +125,8 @@ class DarkSkyTemperatureHandler(DarkSkyHandler):
|
155
|
125
|
def __call__(self, tts):
|
156
|
126
|
"""Speak the current temperature"""
|
157
|
127
|
super().__call__(tts)
|
158
|
|
- tts(self._format_temperature(self.cache['currently']['temperature']))
|
|
128
|
+ tts(format_temperature(self.cache['currently']['temperature'],
|
|
129
|
+ self._temp_precision))
|
159
|
130
|
|
160
|
131
|
|
161
|
132
|
class DarkSkyTodaysHighHandler(DarkSkyHandler):
|
|
@@ -164,8 +135,9 @@ class DarkSkyTodaysHighHandler(DarkSkyHandler):
|
164
|
135
|
def __call__(self, tts):
|
165
|
136
|
"""Speak today's high"""
|
166
|
137
|
super().__call__(tts)
|
167
|
|
- tts(self._format_temperature(
|
168
|
|
- self.cache['daily']['data'][0]['temperatureMax']))
|
|
138
|
+ tts(format_temperature(
|
|
139
|
+ self.cache['daily']['data'][0]['temperatureMax'],
|
|
140
|
+ self._temp_precision))
|
169
|
141
|
|
170
|
142
|
|
171
|
143
|
class DarkSkyTodaysLowHandler(DarkSkyHandler):
|
|
@@ -174,8 +146,9 @@ class DarkSkyTodaysLowHandler(DarkSkyHandler):
|
174
|
146
|
def __call__(self, tts):
|
175
|
147
|
"""Speak today's low"""
|
176
|
148
|
super().__call__(tts)
|
177
|
|
- tts(self._format_temperature(
|
178
|
|
- self.cache['daily']['data'][0]['temperatureMin']))
|
|
149
|
+ tts(format_temperature(
|
|
150
|
+ self.cache['daily']['data'][0]['temperatureMin'],
|
|
151
|
+ self._temp_precision))
|
179
|
152
|
|
180
|
153
|
|
181
|
154
|
class DarkSkyTomorrowsHighHandler(DarkSkyHandler):
|
|
@@ -184,8 +157,9 @@ class DarkSkyTomorrowsHighHandler(DarkSkyHandler):
|
184
|
157
|
def __call__(self, tts):
|
185
|
158
|
"""Speak tomorrow's high"""
|
186
|
159
|
super().__call__(tts)
|
187
|
|
- tts(self._format_temperature(
|
188
|
|
- self.cache['daily']['data'][1]['temperatureMax']))
|
|
160
|
+ tts(format_temperature(
|
|
161
|
+ self.cache['daily']['data'][1]['temperatureMax'],
|
|
162
|
+ self._temp_precision))
|
189
|
163
|
|
190
|
164
|
|
191
|
165
|
class DarkSkyHumidityHandler(DarkSkyHandler):
|
|
@@ -194,7 +168,7 @@ class DarkSkyHumidityHandler(DarkSkyHandler):
|
194
|
168
|
def __call__(self, tts):
|
195
|
169
|
"""Speak the current relative humidity"""
|
196
|
170
|
super().__call__(tts)
|
197
|
|
- tts(self._format_percent(self.cache['currently']['humidity']))
|
|
171
|
+ tts(format_percent(self.cache['currently']['humidity']))
|
198
|
172
|
|
199
|
173
|
|
200
|
174
|
class DarkSkyCurrentConditionsHandler(DarkSkyHandler):
|
|
@@ -204,9 +178,9 @@ class DarkSkyCurrentConditionsHandler(DarkSkyHandler):
|
204
|
178
|
"""Speak the current weather conditions"""
|
205
|
179
|
super().__call__(tts)
|
206
|
180
|
summary = self.cache['currently']['summary']
|
207
|
|
- temperature = self._format_temperature(
|
208
|
|
- self.cache['currently']['temperature'])
|
209
|
|
- humidity = self._format_percent(self.cache['currently']['humidity'])
|
|
181
|
+ temperature = format_temperature(
|
|
182
|
+ self.cache['currently']['temperature'], self._temp_precision)
|
|
183
|
+ humidity = format_percent(self.cache['currently']['humidity'])
|
210
|
184
|
tts(f'{summary}, {temperature}, relative humidity {humidity}')
|
211
|
185
|
|
212
|
186
|
|
|
@@ -216,7 +190,7 @@ class DarkSkySunsetHandler(DarkSkyHandler):
|
216
|
190
|
def __call__(self, tts):
|
217
|
191
|
"""Speak the time of sunset"""
|
218
|
192
|
super().__call__(tts)
|
219
|
|
- tts(self._format_time(self.cache['daily']['data'][0]['sunsetTime']))
|
|
193
|
+ tts(format_time(self.cache['daily']['data'][0]['sunsetTime']))
|
220
|
194
|
|
221
|
195
|
|
222
|
196
|
class DarkSkyProviderHandler(DarkSkyHandler):
|