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
36 changes: 29 additions & 7 deletions src/data/weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export interface WeatherEntity extends HassEntityBase {
attributes: WeatherEntityAttributes;
}

export const WEATHER_TEMPERATURE_ATTRIBUTES = new Set<string>([
"temperature",
"apparent_temperature",
"dew_point",
]);

export const weatherSVGs = new Set<string>([
"clear-night",
"cloudy",
Expand Down Expand Up @@ -256,9 +262,15 @@ export const getWeatherUnit = (
export const getSecondaryWeatherAttribute = (
hass: HomeAssistant,
stateObj: WeatherEntity,
forecast: ForecastAttribute[]
forecast: ForecastAttribute[],
temperatureFractionDigits?: number
): TemplateResult | undefined => {
const extrema = getWeatherExtrema(hass, stateObj, forecast);
const extrema = getWeatherExtrema(
hass,
stateObj,
forecast,
temperatureFractionDigits
);

if (extrema) {
return extrema;
Expand Down Expand Up @@ -298,7 +310,8 @@ export const getSecondaryWeatherAttribute = (
const getWeatherExtrema = (
hass: HomeAssistant,
stateObj: WeatherEntity,
forecast: ForecastAttribute[]
forecast: ForecastAttribute[],
temperatureFractionDigits?: number
): TemplateResult | undefined => {
if (!forecast?.length) {
return undefined;
Expand All @@ -313,13 +326,22 @@ const getWeatherExtrema = (
break;
}
if (!tempHigh || fc.temperature > tempHigh) {
tempHigh = fc.temperature;
tempHigh =
temperatureFractionDigits === undefined
? fc.temperature
: round(fc.temperature, temperatureFractionDigits);
}
if (!tempLow || (fc.templow && fc.templow < tempLow)) {
tempLow = fc.templow;
if (fc.templow !== undefined && (!tempLow || fc.templow < tempLow)) {
tempLow =
temperatureFractionDigits === undefined
? fc.templow
: round(fc.templow, temperatureFractionDigits);
}
if (!fc.templow && (!tempLow || fc.temperature < tempLow)) {
tempLow = fc.temperature;
tempLow =
temperatureFractionDigits === undefined
? fc.temperature
: round(fc.temperature, temperatureFractionDigits);
}
Comment thread
flixlix marked this conversation as resolved.
}

Expand Down
50 changes: 45 additions & 5 deletions src/panels/lovelace/cards/hui-weather-forecast-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import {
subscribeForecast,
weatherAttrIcons,
weatherSVGStyles,
WEATHER_TEMPERATURE_ATTRIBUTES,
} from "../../../data/weather";
import type { HomeAssistant } from "../../../types";
import { round } from "../../../common/number/round";
import { actionHandler } from "../common/directives/action-handler-directive";
import { computeLovelaceEntityName } from "../common/entity/compute-lovelace-entity-name";
import { findEntities } from "../common/find-entities";
Expand Down Expand Up @@ -266,6 +268,20 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
this._config.name
);

const temperatureFractionDigits = this._config.round_temperature
? 0
: undefined;

const isSecondaryInfoAttributeTemperature =
this._config?.secondary_info_attribute &&
WEATHER_TEMPERATURE_ATTRIBUTES.has(this._config.secondary_info_attribute);

const isSecondaryInfoNumber =
this._config.secondary_info_attribute &&
!Number.isNaN(
+stateObj.attributes[this._config.secondary_info_attribute]
);

return html`
<ha-card
class=${classMap({
Expand Down Expand Up @@ -312,7 +328,11 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
? html`
${formatNumber(
stateObj.attributes.temperature,
this.hass.locale
this.hass.locale,
{
maximumFractionDigits:
temperatureFractionDigits,
}
)}&nbsp;<span
>${getWeatherUnit(
this.hass.config,
Expand Down Expand Up @@ -350,14 +370,26 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
: html`
${this.hass.formatEntityAttributeValue(
stateObj,
this._config.secondary_info_attribute
this._config.secondary_info_attribute,
temperatureFractionDigits === 0 &&
isSecondaryInfoNumber &&
isSecondaryInfoAttributeTemperature
? round(
stateObj.attributes[
this._config
.secondary_info_attribute
],
temperatureFractionDigits
)
: undefined
)}
`}
`
: getSecondaryWeatherAttribute(
this.hass,
stateObj,
forecast!
forecast!,
temperatureFractionDigits
)}
</div>
</div>
Expand Down Expand Up @@ -425,15 +457,23 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
${this._showValue(item.temperature)
? html`${formatNumber(
item.temperature,
this.hass!.locale
this.hass!.locale,
{
Comment thread
flixlix marked this conversation as resolved.
maximumFractionDigits:
temperatureFractionDigits,
}
)}°`
: "—"}
</div>
<div class="templow">
${this._showValue(item.templow)
? html`${formatNumber(
item.templow!,
this.hass!.locale
this.hass!.locale,
{
maximumFractionDigits:
temperatureFractionDigits,
}
)}°`
: hourly
? ""
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/cards/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ export interface WeatherForecastCardConfig extends LovelaceCardConfig {
forecast_type?: ForecastType;
forecast_slots?: number;
secondary_info_attribute?: keyof TranslationDict["ui"]["card"]["weather"]["attributes"];
round_temperature?: boolean;
theme?: string;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const cardConfigStruct = assign(
forecast_type: optional(string()),
forecast_slots: optional(number()),
secondary_info_attribute: optional(string()),
round_temperature: optional(boolean()),
tap_action: optional(actionConfigStruct),
hold_action: optional(actionConfigStruct),
double_tap_action: optional(actionConfigStruct),
Expand Down Expand Up @@ -156,6 +157,10 @@ export class HuiWeatherForecastCardEditor
},
context: { entity: "entity" },
},
{
name: "round_temperature",
selector: { boolean: {} },
},
{
name: "",
type: "grid",
Expand Down
2 changes: 2 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@
},
"weather": {
"attributes": {
"dew_point": "Dew point",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I also noticed that this was missing before in the translation file, leading to an error in my change at https://github.com/home-assistant/frontend/pull/28103/files#diff-4f7195942cc7cdfbd5ad6149411b2d6fb641035063b8711026e641d7f1a2a606R276

"air_pressure": "Air pressure",
"humidity": "Humidity",
"temperature": "Temperature",
Expand Down Expand Up @@ -8016,6 +8017,7 @@
"suggested_cards": "Suggested cards",
"other_cards": "Other cards",
"custom_cards": "Custom cards",
"round_temperature": "Round temperature",
"features": "Features",
"actions": "Actions",
"content": "Content"
Expand Down
Loading