From 9f474a5c09d64f1569eac6701db50a5368135eb3 Mon Sep 17 00:00:00 2001 From: Patrick Kissling Date: Fri, 1 Dec 2023 13:31:59 +0100 Subject: [PATCH] Fix subscription not working for openweathermap --- src/clock-weather-card.ts | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/clock-weather-card.ts b/src/clock-weather-card.ts index fd0d0db5..78e91761 100644 --- a/src/clock-weather-card.ts +++ b/src/clock-weather-card.ts @@ -595,23 +595,17 @@ export class ClockWeatherCard extends LitElement { return } - const hourly = this.config.hourly_forecast - if (hourly && !this.supportsFeature(WeatherEntityFeature.FORECAST_HOURLY)) { + const forecastType = this.determineForecastType() + if (forecastType === 'hourly_not_supported') { this.forecastSubscriber = async () => {} - throw this.createError(`Weather entity "${this.config.entity}" does not support hourly forecasts.`) + throw this.createError(`Weather entity [${this.config.entity}] does not support hourly forecast.`) } - - if (!hourly && !this.supportsFeature(WeatherEntityFeature.FORECAST_DAILY)) { - this.forecastSubscriber = async () => {} - throw this.createError(`Weather entity "${this.config.entity}" does not support daily forecasts.`) - } - const callback = (event: WeatherForecastEvent): void => { this.forecasts = event.forecast } this.hass.connection.subscribeMessage(callback, { type: 'weather/subscribe_forecast', - forecast_type: hourly ? 'hourly' : 'daily', + forecast_type: forecastType, entity_id: this.config.entity }) .then(forecastSubscriber => { this.forecastSubscriber = forecastSubscriber }) @@ -650,4 +644,23 @@ export class ClockWeatherCard extends LitElement { this.error = html`${errorCard}` return error } + + private determineForecastType (): 'hourly' | 'daily' | 'hourly_not_supported' { + const supportsDaily = this.supportsFeature(WeatherEntityFeature.FORECAST_DAILY) + const supportsHourly = this.supportsFeature(WeatherEntityFeature.FORECAST_HOURLY) + const hourly = this.config.hourly_forecast + if (supportsDaily && supportsHourly) { + return hourly ? 'hourly' : 'daily' + } else if (hourly && supportsHourly) { + return 'hourly' + } else if (!hourly && supportsDaily) { + return 'daily' + } else if (hourly && !supportsHourly) { + return 'hourly_not_supported' + } else { + // !hourly && !supportsDaily + console.warn(`clock-weather-card - Weather entity [${this.config.entity}] does not support daily forecast. Falling back to hourly forecast.`) + return 'hourly' + } + } }