Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified public/static/images/weather/night.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/static/images/weather/partly-cloudy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/static/images/weather/sunny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 91 additions & 5 deletions src/data/weather.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { HomeAssistant } from "../types";
import { HomeAssistant, WeatherEntity } from "../types";

export const weatherImages = {
"clear-night": "/static/images/weather/night.png",
cloudy: "/static/images/weather/cloudy.png",
fog: "/static/images/weather/cloudy.png",
hail: "/static/images/weather/rainy.png",
lightning: "/static/images/weather/lightning.png",
"lightning-rainy": "/static/images/weather/lightning-rainy.png",
partlycloudy: "/static/images/weather/partly-cloudy.png",
pouring: "/static/images/weather/pouring.png",
rainy: "/static/images/weather/rainy.png",
snowy: "/static/images/weather/snowy.png",
"snowy-rainy": "/static/images/weather/rainy.png",
sunny: "/static/images/weather/sunny.png",
windy: "/static/images/weather/windy.png",
"windy-variant": "/static/images/weather/windy.png",
};

export const weatherIcons = {
exceptional: "hass:alert-circle-outline",
fog: "hass:weather-fog",
hail: "hass:weather-hail",
"snowy-rainy": "hass:weather-snowy-rainy",
"windy-variant": "hass:weather-windy-variant",
};

export const cardinalDirections = [
Expand Down Expand Up @@ -78,3 +78,89 @@ export const getWeatherUnit = (
return hass.config.unit_system[measure] || "";
}
};

export const getSecondaryWeatherAttribute = (
hass: HomeAssistant,
stateObj: WeatherEntity
): string | undefined => {
const extrema = getWeatherExtrema(hass, stateObj);

if (extrema) {
return extrema;
}

let value: number;
let attribute: string;

if (
stateObj.attributes.forecast?.length &&
stateObj.attributes.forecast[0].precipitation !== undefined &&
stateObj.attributes.forecast[0].precipitation !== null
) {
value = stateObj.attributes.forecast[0].precipitation!;
attribute = "precipitation";
} else if ("humidity" in stateObj.attributes) {
value = stateObj.attributes.humidity!;
attribute = "humidity";
} else {
return undefined;
}

return `
${hass!.localize(
`ui.card.weather.attributes.${attribute}`
)} ${value} ${getWeatherUnit(hass!, attribute)}
`;
};

const getWeatherExtrema = (
hass: HomeAssistant,
stateObj: WeatherEntity
): string | undefined => {
if (!stateObj.attributes.forecast?.length) {
return undefined;
}

let tempLow: number | undefined;
let tempHigh: number | undefined;
const today = new Date().getDate();

for (const forecast of stateObj.attributes.forecast!) {
if (new Date(forecast.datetime).getDate() !== today) {
break;
}
if (!tempHigh || forecast.temperature > tempHigh) {
tempHigh = forecast.temperature;
}
if (!tempLow || (forecast.templow && forecast.templow < tempLow)) {
tempLow = forecast.templow;
}
if (!forecast.templow && (!tempLow || forecast.temperature < tempLow)) {
tempLow = forecast.temperature;
}
}

if (!tempLow && !tempHigh) {
return undefined;
}

const unit = getWeatherUnit(hass!, "temperature");

return `
${
tempHigh
? `
${hass!.localize(`ui.card.weather.high`)} ${tempHigh} ${unit}
`
: ""
}
${tempLow && tempHigh ? " / " : ""}
${
tempLow
? `
${hass!.localize(`ui.card.weather.low`)} ${tempLow} ${unit}
`
: ""
}
`;
};
Loading