Skip to content

Commit e7538a2

Browse files
mrwizard1000KristjanESPERANTO
authored andcommitted
feat: add weather alerts to current block (#29)
1 parent f76e280 commit e7538a2

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

MMM-OneCallWeather.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
& .day,
7373
& .max-temp,
7474
& .min-temp,
75-
& .wind-speed {
75+
& .wind-speed,
76+
& .alert {
7677
text-align: center;
7778
}
7879

MMM-OneCallWeather.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Module.register("MMM-OneCallWeather", {
3737
roundTemp: true,
3838
showCurrent: true,
3939
showForecast: true,
40+
showAlerts: false,
4041
forecastLayout: "columns", // "columns" (days as columns) or "rows" (days as rows)
4142
arrangement: "vertical", // "vertical" (forecast below current) or "horizontal" (forecast next to current)
4243

@@ -138,8 +139,14 @@ Module.register("MMM-OneCallWeather", {
138139
weatherType: this.convertWeatherType(data.current.weather[0].icon),
139140
humidity: data.current.humidity,
140141
feelsLikeTemp: data.current.feels_like.toFixed(1),
141-
precipitation: current.rain + current.snow
142+
precipitation: current.rain + current.snow,
143+
alerts: []
142144
};
145+
146+
if (Object.hasOwn(data, "alerts")) {
147+
currently.alerts = data.alerts;
148+
}
149+
143150
current.push(currently);
144151
Log.debug(`current weather is ${JSON.stringify(currently)}`);
145152
}
@@ -658,6 +665,24 @@ Module.register("MMM-OneCallWeather", {
658665
currentRow3.appendChild(currentCell3);
659666
table.appendChild(currentRow3);
660667

668+
// Row 4: Current weather alerts
669+
if (this.config.showAlerts && currentWeather.alerts.length > 0) {
670+
const currentRow4 = document.createElement("tr");
671+
const currentCell4 = document.createElement("td");
672+
currentCell4.colSpan = colspan;
673+
currentCell4.className = "alert";
674+
let alertText = "";
675+
for (const weatherAlert of currentWeather.alerts) {
676+
if (alertText !== "") {
677+
alertText += "<br />";
678+
}
679+
alertText += weatherAlert.event;
680+
}
681+
currentCell4.innerHTML = alertText;
682+
currentRow4.appendChild(currentCell4);
683+
table.appendChild(currentRow4);
684+
}
685+
661686
return table;
662687
},
663688

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ The following properties can be configured:
9292
| `windUnits` | The units to use for wind speed.<br><br>**Possible values:** `"mph"` (miles per hour), `"kmph"` (kilometers per hour), `"ms"` (meters per second)<br>**Default value:** `"mph"`<br>This value is optional. By default the weatherforecast module will display wind speed in miles per hour. |
9393
| `roundTemp` | Round temperature values to nearest integer.<br><br>**Possible values:** `true` (round to integer) or `false` (display exact value with decimal point)<br>**Default value:** `false` |
9494
| `showCurrent` | Show the current weather section.<br><br>**Possible values:** `true` or `false`<br>**Default value:** `true` |
95+
| `showAlerts` | Show the current weather alerts.<br><br>**Possible values:** `true` or `false`<br>**Default value:** `false` |
9596
| `showForecast` | Show the forecast section.<br><br>**Possible values:** `true` or `false`<br>**Default value:** `true` |
9697
| `forecastLayout` | Defines how the forecast is structured internally.<br><br>**Possible values:** `"columns"` (days displayed as table columns), `"rows"` (days displayed as table rows)<br>**Default value:** `"columns"` |
9798
| `arrangement` | How current weather and forecast are positioned relative to each other (only relevant when both `showCurrent` and `showForecast` are `true`).<br><br>**Possible values:** `"vertical"` (forecast below current weather), `"horizontal"` (forecast next to current weather)<br>**Default value:** `"vertical"` |

0 commit comments

Comments
 (0)