Allow changing between C and F for weather temp

This commit is contained in:
Svilen Markov 2024-04-29 16:29:11 +01:00
parent 78572a534b
commit ccdafcdfab
4 changed files with 23 additions and 4 deletions

View File

@ -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.

View File

@ -2,7 +2,7 @@
{{ define "widget-content" }}
<div class="size-h2 color-highlight text-center">{{ .Weather.WeatherCodeAsString }}</div>
<div class="size-h4 text-center">Feels like {{ .Weather.ApparentTemperature }}°C</div>
<div class="size-h4 text-center">Feels like {{ .Weather.ApparentTemperature }}°{{ if eq .Units "metric" }}C{{ else }}F{{ end }}</div>
<div class="weather-columns flex margin-top-15 justify-center">
{{ range $i, $column := .Weather.Columns }}

View File

@ -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)

View File

@ -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