Skip to content

Commit

Permalink
Migrate forecast_days to forecast_rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Kissling committed Oct 7, 2023
1 parent fbcb7bf commit ad30aca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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
Expand All @@ -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` |
Expand Down
12 changes: 6 additions & 6 deletions src/clock-weather-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Record<number, WeatherForecast[]>>((forecasts, forecast) => {
const d = new Date(forecast.datetime)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ad30aca

Please sign in to comment.