Skip to content

Commit

Permalink
Fix subscription not working for openweathermap
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Kissling authored and pkissling committed Dec 1, 2023
1 parent 2dd8056 commit 9f474a5
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/clock-weather-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WeatherForecastEvent>(callback, {
type: 'weather/subscribe_forecast',
forecast_type: hourly ? 'hourly' : 'daily',
forecast_type: forecastType,
entity_id: this.config.entity
})
.then(forecastSubscriber => { this.forecastSubscriber = forecastSubscriber })
Expand Down Expand Up @@ -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'
}
}
}

0 comments on commit 9f474a5

Please sign in to comment.