Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

✨ Support city ID for weather (#1394)

Alicia Sykesfdd47fc1f7ff
diff --git a/docs/widgets.md b/docs/widgets.md
index 668ef2af..d774b4cc 100644
--- a/docs/widgets.md
+++ b/docs/widgets.md
@@ -160,10 +160,11 @@ A simple, live-updating local weather component, showing temperature, conditions
--- | --- | --- | ---
**`apiKey`** | `string` | Required | Your OpenWeatherMap API key. You can get one for free at [openweathermap.org](https://openweathermap.org/)
**`city`** | `string` | Required | A city name to use for fetching weather. This can also be a state code or country code, following the ISO-3166 format
+**`cityId`** | `number` | _Optional_ | An OpenWeatherMap numeric city ID, used to disambiguate cities that share a name. You can find the ID in the URL of the city's page on [openweathermap.org](https://openweathermap.org/) (e.g. `2643743` for London, GB). If provided, this will override the `city` option
**`units`** | `string` | _Optional_ | The units to use for displaying data, can be either `metric` or `imperial`. Defaults to `metric`
**`hideDetails`** | `boolean` | _Optional_ | If set to `true`, the additional details (wind, humidity, pressure, etc) will not be shown. Defaults to `false`
-**`lat`** | `number` | _Optional_ | To show weather for a specific location, you can provide the latitude and longitude coordinates. If provided, this will override the `city` option
-**`lon`** | `number` | _Optional_ | To show weather for a specific location, you can provide the latitude and longitude coordinates. If provided, this will override the `city` option
+**`lat`** | `number` | _Optional_ | To show weather for a specific location, you can provide the latitude and longitude coordinates. If provided, this will override the `city` and `cityId` options
+**`lon`** | `number` | _Optional_ | To show weather for a specific location, you can provide the latitude and longitude coordinates. If provided, this will override the `city` and `cityId` options
#### Example
@@ -197,6 +198,9 @@ Displays the weather (temperature and conditions) for the next few days for a gi
--- | --- | --- | ---
**`apiKey`** | `string` | Required | Your OpenWeatherMap API key. You can get one at [openweathermap.org](https://openweathermap.org/) or for free via the [OWM Student Plan](https://home.openweathermap.org/students)
**`city`** | `string` | Required | A city name to use for fetching weather. This can also be a state code or country code, following the ISO-3166 format
+**`cityId`** | `number` | _Optional_ | An OpenWeatherMap numeric city ID, used to disambiguate cities that share a name. You can find the ID in the URL of the city's page on [openweathermap.org](https://openweathermap.org/) (e.g. `2643743` for London, GB). If provided, this will override the `city` option
+**`lat`** | `number` | _Optional_ | Latitude for a specific location. If provided alongside `lon`, this will override the `city` and `cityId` options
+**`lon`** | `number` | _Optional_ | Longitude for a specific location. If provided alongside `lat`, this will override the `city` and `cityId` options
**`numDays`** | `number` | _Optional_ | The number of days to display of forecast info to display. Defaults to `4`, max `16` days
**`units`** | `string` | _Optional_ | The units to use for displaying data, can be either `metric` or `imperial`. Defaults to `metric`
**`hideDetails`** | `boolean` | _Optional_ | If set to `true`, the additional details (wind, humidity, pressure, etc) will not be shown. Defaults to `false`
diff --git a/src/components/Widgets/Weather.vue b/src/components/Widgets/Weather.vue
index cde8d01d..f6d3ebad 100644
--- a/src/components/Widgets/Weather.vue
+++ b/src/components/Widgets/Weather.vue
@@ -47,11 +47,16 @@ export default {
},
endpoint() {
const apiKey = this.parseAsEnvVar(this.options.apiKey);
- const { city, lat, lon } = this.options;
- const params = (lat && lon)
- ? `lat=${lat}&lon=${lon}&appid=${apiKey}&units=${this.units}`
- : `q=${city}&appid=${apiKey}&units=${this.units}`;
- return `${widgetApiEndpoints.weather}?${params}`;
+ const { city, cityId, lat, lon } = this.options;
+ let locationParams;
+ if (lat && lon) {
+ locationParams = `lat=${lat}&lon=${lon}`;
+ } else if (cityId) {
+ locationParams = `id=${cityId}`;
+ } else {
+ locationParams = `q=${city}`;
+ }
+ return `${widgetApiEndpoints.weather}?${locationParams}&appid=${apiKey}&units=${this.units}`;
},
tempDisplayUnits() {
switch (this.units) {
@@ -111,8 +116,8 @@ export default {
const ops = this.options;
if (!ops.apiKey) this.error('Missing API key for OpenWeatherMap');
- if ((!ops.lat || !ops.lon) && !ops.city) {
- this.error('A city name or lat + lon is required to fetch weather');
+ if ((!ops.lat || !ops.lon) && !ops.city && !ops.cityId) {
+ this.error('A city name, city ID or lat + lon is required to fetch weather');
}
if (ops.units && ops.units !== 'metric' && ops.units !== 'imperial') {
diff --git a/src/components/Widgets/WeatherForecast.vue b/src/components/Widgets/WeatherForecast.vue
index 87071288..733df63c 100644
--- a/src/components/Widgets/WeatherForecast.vue
+++ b/src/components/Widgets/WeatherForecast.vue
@@ -57,9 +57,17 @@ export default {
},
endpoint() {
const apiKey = this.parseAsEnvVar(this.options.apiKey);
- const { city } = this.options;
+ const { city, cityId, lat, lon } = this.options;
const cnt = this.numDays * 8; // API returns 3-hourly entries; request enough to cover numDays
- const params = `?q=${city}&cnt=${cnt}&units=${this.units}&appid=${apiKey}`;
+ let locationParams;
+ if (lat && lon) {
+ locationParams = `lat=${lat}&lon=${lon}`;
+ } else if (cityId) {
+ locationParams = `id=${cityId}`;
+ } else {
+ locationParams = `q=${city}`;
+ }
+ const params = `?${locationParams}&cnt=${cnt}&units=${this.units}&appid=${apiKey}`;
return `${widgetApiEndpoints.weatherForecast}${params}`;
},
tempDisplayUnits() {
@@ -149,7 +157,9 @@ export default {
checkProps() {
const ops = this.options;
if (!ops.apiKey) this.error('Missing API key for OpenWeatherMap');
- if (!ops.city) this.error('A city name is required to fetch weather');
+ if (!ops.city && !ops.cityId && !(ops.lat && ops.lon)) {
+ this.error('A city name, city ID or lat + lon is required to fetch weather');
+ }
if (ops.units && ops.units !== 'metric' && ops.units !== 'imperial') {
this.error('Invalid units specified, must be either \'metric\' or \'imperial\'');
}