From ccdafcdfab5117bed6d3e0b2eecd9e21766cd89f Mon Sep 17 00:00:00 2001
From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com>
Date: Mon, 29 Apr 2024 16:29:11 +0100
Subject: [PATCH] Allow changing between C and F for weather temp
---
docs/configuration.md | 5 +++++
internal/assets/templates/weather.html | 2 +-
internal/feed/openmeteo.go | 11 +++++++++--
internal/widget/weather.go | 9 ++++++++-
4 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/docs/configuration.md b/docs/configuration.md
index 0fc76df..9c3bc13 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -502,6 +502,7 @@ Example:
```yaml
- type: weather
+ units: metric
location: London, United Kingdom
```
@@ -516,11 +517,15 @@ Each bar represents a 2 hour interval. The yellow background represents sunrise
| Name | Type | Required | Default |
| ---- | ---- | -------- | ------- |
| location | string | yes | |
+| units | string | no | metric |
| hide-location | boolean | no | false |
##### `location`
The name of the city and country to fetch weather information for. Attempting to launch the applcation with an invalid location will result in an error. You can use the [gecoding API page](https://open-meteo.com/en/docs/geocoding-api) to search for your specific location. Glance will use the first result from the list if there are multiple.
+##### `units`
+Whether to show the temperature in celsius or fahrenheit, possible values are `metric` or `imperial`.
+
##### `hide-location`
Optionally don't display the location name on the widget.
diff --git a/internal/assets/templates/weather.html b/internal/assets/templates/weather.html
index 1cccdc0..b251186 100644
--- a/internal/assets/templates/weather.html
+++ b/internal/assets/templates/weather.html
@@ -2,7 +2,7 @@
{{ define "widget-content" }}
{{ .Weather.WeatherCodeAsString }}
-Feels like {{ .Weather.ApparentTemperature }}°C
+Feels like {{ .Weather.ApparentTemperature }}°{{ if eq .Units "metric" }}C{{ else }}F{{ end }}
{{ range $i, $column := .Weather.Columns }}
diff --git a/internal/feed/openmeteo.go b/internal/feed/openmeteo.go
index 604ac05..71438f0 100644
--- a/internal/feed/openmeteo.go
+++ b/internal/feed/openmeteo.go
@@ -79,9 +79,15 @@ func barIndexFromHour(h int) int {
}
// TODO: bunch of spaget, refactor
-// TODO: allow changing between C and F
-func FetchWeatherForPlace(place *PlaceJson) (*Weather, error) {
+func FetchWeatherForPlace(place *PlaceJson, units string) (*Weather, error) {
query := url.Values{}
+ var temperatureUnit string
+
+ if units == "imperial" {
+ temperatureUnit = "fahrenheit"
+ } else {
+ temperatureUnit = "celsius"
+ }
query.Add("latitude", fmt.Sprintf("%f", place.Latitude))
query.Add("longitude", fmt.Sprintf("%f", place.Longitude))
@@ -91,6 +97,7 @@ func FetchWeatherForPlace(place *PlaceJson) (*Weather, error) {
query.Add("current", "temperature_2m,apparent_temperature,weather_code,wind_speed_10m")
query.Add("hourly", "temperature_2m,precipitation_probability")
query.Add("daily", "sunrise,sunset")
+ query.Add("temperature_unit", temperatureUnit)
requestUrl := "https://api.open-meteo.com/v1/forecast?" + query.Encode()
request, _ := http.NewRequest("GET", requestUrl, nil)
diff --git a/internal/widget/weather.go b/internal/widget/weather.go
index 390f9d7..a59b9b9 100644
--- a/internal/widget/weather.go
+++ b/internal/widget/weather.go
@@ -13,6 +13,7 @@ type Weather struct {
widgetBase `yaml:",inline"`
Location string `yaml:"location"`
HideLocation bool `yaml:"hide-location"`
+ Units string `yaml:"units"`
Place *feed.PlaceJson `yaml:"-"`
Weather *feed.Weather `yaml:"-"`
TimeLabels [12]string `yaml:"-"`
@@ -24,6 +25,12 @@ func (widget *Weather) Initialize() error {
widget.withTitle("Weather").withCacheOnTheHour()
widget.TimeLabels = timeLabels
+ if widget.Units == "" {
+ widget.Units = "metric"
+ } else if widget.Units != "metric" && widget.Units != "imperial" {
+ return fmt.Errorf("invalid units '%s' for weather, must be either metric or imperial", widget.Units)
+ }
+
place, err := feed.FetchPlaceFromName(widget.Location)
if err != nil {
@@ -36,7 +43,7 @@ func (widget *Weather) Initialize() error {
}
func (widget *Weather) Update(ctx context.Context) {
- weather, err := feed.FetchWeatherForPlace(widget.Place)
+ weather, err := feed.FetchWeatherForPlace(widget.Place, widget.Units)
if !widget.canContinueUpdateAfterHandlingErr(err) {
return