From ad30acade401f741c5d1cc81121a3c9cf95ac946 Mon Sep 17 00:00:00 2001 From: Patrick Kissling Date: Tue, 3 Oct 2023 16:19:55 +0200 Subject: [PATCH] Migrate `forecast_days` to `forecast_rows` --- README.md | 13 ++++++++----- src/clock-weather-card.ts | 12 ++++++------ src/types.ts | 5 +++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e07a55e4..746efd0f 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,14 @@ showing the current date, time and a weather forecast. Credits go to [basmilius](https://github.com/basmilius) for the awesome [weather icons](https://github.com/basmilius/weather-icons). +## Migrating from v1 to v2 + +* Configuration property `forecast_days` was renamed to `forecast_rows` to indicate that this attribute does not only work for daily, but also for hourly forecasts. + ## FAQ -- [Why don't I see the current day in my weather forecast?](#why-dont-i-see-the-current-day-in-my-weather-forecast) -- [Why does the forecast show less days than expected?](#why-does-the-forecast-show-less-days-than-expected) -- [What does the card actually display?](#what-does-the-card-actually-display) +* [Why don't I see the current day in my weather forecast?](#why-dont-i-see-the-current-day-in-my-weather-forecast) +* [What does the card actually display?](#what-does-the-card-actually-display) ### Why don't I see the current day in my weather forecast? @@ -96,7 +99,7 @@ sun_entity: sun.sun temperature_sensor: sensor.outdoor_temp weather_icon_type: line animated_icon: true -forecast_days: 5 +forecast_rows: 5 locale: en-GB time_format: 24 date_pattern: P @@ -120,7 +123,7 @@ time_zone: null | temperature_sensor | string | **Optional** | ID of the temperature sensor entity. Used to show the current temperature based on a sensor value instead of the weather forecast | `''` | | weather_icon_type | `line` \| `fill` | **Optional** | Appearance of the large weather icon | `line` | | animated_icon | boolean | **Optional** | Whether the large weather icon should be animated | `true` | -| forecast_days | number | **Optional** | Days of weather forecast to show | `5` | +| forecast_rows | number | **Optional** | The amount of weather forecast rows to show. Depending on `hourly_forecast` each row either corresponds to a day or an hour | `5` | | locale | string[^2] | **Optional** | Language to use for language specific text. If not provided, falls back to the locale set in HA | `en-GB` | | time_format | `24` \| `12` | **Optional** | Format used to display the time. If not provided, falls back to the time format set in HA | `24` | | date_pattern | string | **Optional** | Pattern to use for time formatting. If not provided, falls back to the default date formatting of the configured language. See [date-fns](https://date-fns.org/v2.29.3/docs/format) for valid patterns | `P` | diff --git a/src/clock-weather-card.ts b/src/clock-weather-card.ts index df26c7cf..5f44e862 100644 --- a/src/clock-weather-card.ts +++ b/src/clock-weather-card.ts @@ -224,11 +224,11 @@ export class ClockWeatherCard extends LitElement { private renderForecast (): TemplateResult[] { const weather = this.getWeather() const currentTemp = roundIfNotNull(this.getCurrentTemperature()) - const maxItemsCount = this.config.forecast_days + const maxRowsCount = this.config.forecast_rows const hourly = this.config.hourly_forecast const temperatureUnit = weather.attributes.temperature_unit - const forecasts = this.mergeForecasts(maxItemsCount, hourly) + const forecasts = this.mergeForecasts(maxRowsCount, hourly) const minTemps = forecasts.map((f) => f.templow) const maxTemps = forecasts.map((f) => f.temperature) @@ -385,7 +385,7 @@ export class ClockWeatherCard extends LitElement { sun_entity: config.sun_entity ?? 'sun.sun', temperature_sensor: config.temperature_sensor, weather_icon_type: config.weather_icon_type ?? 'line', - forecast_days: config.forecast_days ?? 5, + forecast_rows: config.forecast_rows ?? 5, hourly_forecast: config.hourly_forecast ?? false, animated_icon: config.animated_icon ?? true, time_format: config.time_format?.toString() as '12' | '24' | undefined, @@ -527,7 +527,7 @@ export class ClockWeatherCard extends LitElement { return localize(key, this.getLocale()) } - private mergeForecasts (maxItemsCount: number, hourly: boolean): MergedWeatherForecast[] { + private mergeForecasts (maxRowsCount: number, hourly: boolean): MergedWeatherForecast[] { const forecasts = this.getWeather().attributes.forecast ?? this.forecasts ?? [] const agg = forecasts.reduce>((forecasts, forecast) => { const d = new Date(forecast.datetime) @@ -545,7 +545,7 @@ export class ClockWeatherCard extends LitElement { return agg }, []) .sort((a, b) => a.datetime.getTime() - b.datetime.getTime()) - .slice(0, maxItemsCount) + .slice(0, maxRowsCount) } private toZonedDate (date: Date): Date { @@ -621,7 +621,7 @@ export class ClockWeatherCard extends LitElement { return (this.getWeather().attributes.forecast?.length ?? 0) > 0 } - private supportsFeature (feature: WeatherEntityFeature): boolean { + private supportsFeature(feature: WeatherEntityFeature): boolean { return (this.getWeather().attributes.supported_features & feature) !== 0 } } diff --git a/src/types.ts b/src/types.ts index df449643..25da491f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -12,9 +12,10 @@ export interface ClockWeatherCardConfig extends LovelaceCardConfig { entity: string title?: string sun_entity?: string + temperature_sensor?: string; weather_icon_type?: 'fill' | 'line' animated_icon?: boolean - forecast_days?: number + forecast_rows?: number locale?: string time_format?: '12' | '24' date_pattern?: string @@ -34,7 +35,7 @@ export interface MergedClockWeatherCardConfig extends LovelaceCardConfig { temperature_sensor?: string weather_icon_type: 'fill' | 'line' animated_icon: boolean - forecast_days: number + forecast_rows: number locale?: string time_format?: '12' | '24' date_pattern: string