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
57 changes: 38 additions & 19 deletions src/data/weather.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { SVGTemplateResult, svg, html, TemplateResult, css } from "lit-element";
import {
mdiGauge,
mdiWaterPercent,
mdiWeatherFog,
mdiWeatherRainy,
mdiWeatherWindy,
} from "@mdi/js";
import { css, html, svg, SVGTemplateResult, TemplateResult } from "lit-element";
import { styleMap } from "lit-html/directives/style-map";

import "../components/ha-icon";
import "../components/ha-svg-icon";
import type { HomeAssistant, WeatherEntity } from "../types";
import { roundWithOneDecimal } from "../util/calculate";

Expand All @@ -25,6 +33,15 @@ export const weatherIcons = {
exceptional: "hass:alert-circle-outline",
};

export const weatherAttrIcons = {
humidity: mdiWaterPercent,
wind_bearing: mdiWeatherWindy,
wind_speed: mdiWeatherWindy,
pressure: mdiGauge,
visibility: mdiWeatherFog,
precipitation: mdiWeatherRainy,
};

const cloudyStates = new Set<string>([
"partlycloudy",
"cloudy",
Expand Down Expand Up @@ -110,6 +127,7 @@ export const getWeatherUnit = (
return lengthUnit === "km" ? "hPa" : "inHg";
case "wind_speed":
return `${lengthUnit}/h`;
case "visibility":
case "length":
return lengthUnit;
case "precipitation":
Expand All @@ -125,7 +143,7 @@ export const getWeatherUnit = (
export const getSecondaryWeatherAttribute = (
hass: HomeAssistant,
stateObj: WeatherEntity
): string | undefined => {
): TemplateResult | undefined => {
const extrema = getWeatherExtrema(hass, stateObj);

if (extrema) {
Expand All @@ -149,17 +167,22 @@ export const getSecondaryWeatherAttribute = (
return undefined;
}

return `
${hass!.localize(
`ui.card.weather.attributes.${attribute}`
)} ${roundWithOneDecimal(value)} ${getWeatherUnit(hass!, attribute)}
const weatherAttrIcon = weatherAttrIcons[attribute];

return html`
${weatherAttrIcon
? html`
<ha-svg-icon class="attr-icon" .path=${weatherAttrIcon}></ha-svg-icon>
`
: hass!.localize(`ui.card.weather.attributes.${attribute}`)}
${roundWithOneDecimal(value)} ${getWeatherUnit(hass!, attribute)}
`;
};

const getWeatherExtrema = (
hass: HomeAssistant,
stateObj: WeatherEntity
): string | undefined => {
): TemplateResult | undefined => {
if (!stateObj.attributes.forecast?.length) {
return undefined;
}
Expand Down Expand Up @@ -189,22 +212,18 @@ const getWeatherExtrema = (

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

return `
${
tempHigh
? `
return html`
${tempHigh
? `
${tempHigh} ${unit}
`
: ""
}
: ""}
${tempLow && tempHigh ? " / " : ""}
${
tempLow
? `
${tempLow
? `
${tempLow} ${unit}
`
: ""
}
: ""}
`;
};

Expand Down
27 changes: 21 additions & 6 deletions src/panels/lovelace/cards/hui-weather-forecast-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
CSSResult,
customElement,
html,
internalProperty,
LitElement,
property,
internalProperty,
PropertyValues,
TemplateResult,
} from "lit-element";
Expand All @@ -21,19 +21,20 @@ import "../../../components/ha-icon";
import { UNAVAILABLE } from "../../../data/entity";
import {
getSecondaryWeatherAttribute,
getWeatherUnit,
getWeatherStateIcon,
getWeatherUnit,
getWind,
weatherAttrIcons,
weatherSVGStyles,
} from "../../../data/weather";
import type { HomeAssistant, WeatherEntity } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive";
import { findEntities } from "../common/find-entites";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import { installResizeObserver } from "../common/install-resize-observer";
import { createEntityNotFoundWarning } from "../components/hui-warning";
import type { LovelaceCard, LovelaceCardEditor } from "../types";
import type { WeatherForecastCardConfig } from "./types";
import { installResizeObserver } from "../common/install-resize-observer";

const DAY_IN_MILLISECONDS = 86400000;

Expand Down Expand Up @@ -222,9 +223,19 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
<div class="attribute">
${this._config.secondary_info_attribute !== undefined
? html`
${this.hass!.localize(
`ui.card.weather.attributes.${this._config.secondary_info_attribute}`
)}
${this._config.secondary_info_attribute in
weatherAttrIcons
? html`
<ha-svg-icon
class="attr-icon"
.path=${weatherAttrIcons[
this._config.secondary_info_attribute
]}
></ha-svg-icon>
`
: this.hass!.localize(
`ui.card.weather.attributes.${this._config.secondary_info_attribute}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still use the other translations elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the More info

)}
${this._config.secondary_info_attribute === "wind_speed"
? getWind(
this.hass,
Expand Down Expand Up @@ -479,6 +490,10 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
--mdc-icon-size: 40px;
}

.attr-icon {
--mdc-icon-size: 20px;
}

.attribute,
.templow,
.daynight,
Expand Down