NuWeather: make temperature display units configurable

This commit is contained in:
James Lu 2018-12-30 21:28:50 -08:00
parent c0d15face0
commit 57c8658adf
2 changed files with 26 additions and 4 deletions

View File

@ -48,6 +48,14 @@ def configure(advanced):
NuWeather = conf.registerPlugin('NuWeather')
conf.registerGroup(NuWeather, 'apikeys')
conf.registerGroup(NuWeather, 'units')
class NuWeatherTemperatureDisplayMode(registry.OnlySomeStrings):
validStrings = ('F/C', 'C/F', 'F', 'C')
conf.registerChannelValue(NuWeather.units, 'temperature',
NuWeatherTemperatureDisplayMode('F/C', _("""Determines how temperatures will be displayed.
F/C means show "50F/10C", C means display only Celsius, and so on.""")))
BACKENDS = ('apixu',)
class NuWeatherBackend(registry.OnlySomeStrings):

View File

@ -56,8 +56,7 @@ class NuWeather(callbacks.Plugin):
self.db.flush()
super().die()
@staticmethod
def _format_temp(f, c=None):
def _format_temp(self, f, c=None, msg=None):
f = float(f)
if f < 10:
color = 'light blue'
@ -75,9 +74,24 @@ class NuWeather(callbacks.Plugin):
color = 'orange'
else:
color = 'red'
# Round to nearest tenth for display purposes
if c is None:
c = round((f - 32) * 5/9)
string = '%sC/%sF' % (c, f)
c = round((f - 32) * 5/9, 1)
else:
c = round(c, 1)
f = round(f, 1)
displaymode = self.registryValue('units.temperature', msg.args[0] if msg else None)
if displaymode == 'F/C':
string = '%sF/%sC' % (f, c)
elif displaymode == 'C/F':
string = '%sC/%sF' % (c, f)
elif displaymode == 'F':
string = '%sF' % f
elif displaymode == 'C':
string = '%sC' % c
else:
raise ValueError("Unknown display mode for temperature.")
return ircutils.mircColor(string, color)
@staticmethod