Skip to content

Commit

Permalink
Fix issue where card would have multiple forecast subscriptions in pa…
Browse files Browse the repository at this point in the history
…rallel
  • Loading branch information
Patrick Kissling authored and pkissling committed Dec 15, 2023
1 parent 027beb0 commit edf056d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
"@babel/core": "^7.15.0",
"@babel/plugin-proposal-class-properties": "^7.14.5",
"@babel/plugin-proposal-decorators": "^7.14.5",
"@rollup/plugin-babel": "^6.0.0",
"@rollup/plugin-image": "^3.0.0",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@types/luxon": "^3.3.2",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.0.0",
Expand All @@ -36,10 +38,8 @@
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
"eslint-plugin-promise": "^6.0.0",
"rollup": "^4.0.0",
"@rollup/plugin-babel": "^6.0.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-gzip": "^3.0.1",
"@rollup/plugin-node-resolve": "^15.0.0",
"rollup-plugin-serve": "^3.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.36.0",
Expand Down
65 changes: 44 additions & 21 deletions src/clock-weather-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ export class ClockWeatherCard extends LitElement {

@state() private config!: MergedClockWeatherCardConfig
@state() private currentDate!: DateTime
@state() private forecastSubscriber?: () => Promise<void>
@state() private forecasts?: WeatherForecast[]
@state() private error?: TemplateResult
private forecastSubscriber?: () => Promise<void>
private forecastSubscriberLock = false

constructor () {
super()
Expand Down Expand Up @@ -138,8 +139,8 @@ export class ClockWeatherCard extends LitElement {

protected updated (changedProps: PropertyValues): void {
super.updated(changedProps)
if (changedProps.has('config') || !this.forecastSubscriber) {
this.subscribeForecastEvents()
if (changedProps.has('config')) {
void this.subscribeForecastEvents()
}
}

Expand Down Expand Up @@ -188,19 +189,21 @@ export class ClockWeatherCard extends LitElement {
public connectedCallback (): void {
super.connectedCallback()
if (this.hasUpdated) {
this.subscribeForecastEvents()
console.log('clock-weather-card 2.1.11 - connectedCallback')
void this.subscribeForecastEvents()
}
}

public disconnectedCallback (): void {
console.log('clock-weather-card 2.1.11 - disconnectedCallback')
super.disconnectedCallback()
this.unsubscribeForecastEvents()
void this.unsubscribeForecastEvents()
}

protected willUpdate (changedProps: PropertyValues): void {
super.willUpdate(changedProps)
if (!this.forecastSubscriber) {
this.subscribeForecastEvents()
void this.subscribeForecastEvents()
}
}

Expand Down Expand Up @@ -593,39 +596,59 @@ export class ClockWeatherCard extends LitElement {
}
}

private subscribeForecastEvents (): void {
this.unsubscribeForecastEvents()
private async subscribeForecastEvents (): Promise<void> {
if (this.forecastSubscriberLock) {
return
}
this.forecastSubscriberLock = true
await this.unsubscribeForecastEvents()
if (this.isLegacyWeather()) {
this.forecastSubscriber = async () => {}
this.forecastSubscriberLock = false
return
}

if (!this.isConnected || !this.config || !this.hass) {
this.forecastSubscriberLock = false
return
}

const forecastType = this.determineForecastType()
if (forecastType === 'hourly_not_supported') {
this.forecastSubscriber = async () => {}
this.forecastSubscriberLock = false
throw this.createError(`Weather entity [${this.config.entity}] does not support hourly forecast.`)
}
const callback = (event: WeatherForecastEvent): void => {
this.forecasts = event.forecast
try {
const callback = (event: WeatherForecastEvent): void => {
this.forecasts = event.forecast
console.log('clock-weather-card 2.1.11 - Recevied weather forecast event', this.config.entity)
}
const options = { resubscribe: false }
const message = {
type: 'weather/subscribe_forecast',
forecast_type: forecastType,
entity_id: this.config.entity
}
this.forecastSubscriber = await this.hass.connection.subscribeMessage<WeatherForecastEvent>(callback, message, options)
console.log('clock-weather-card 2.1.11 - Subscribed to weather forecast', this.config.entity)
} catch (e: unknown) {
console.error('clock-weather-card 2.1.11 - Error when subscribing to weather forecast: ', JSON.stringify(e))
} finally {
this.forecastSubscriberLock = false
}
this.hass.connection.subscribeMessage<WeatherForecastEvent>(callback, {
type: 'weather/subscribe_forecast',
forecast_type: forecastType,
entity_id: this.config.entity
})
.then(forecastSubscriber => { this.forecastSubscriber = forecastSubscriber })
.catch(e => { console.error('clock-weather-card - Error when subscribing to weather forecast: ', e) })
}

private unsubscribeForecastEvents (): void {
private async unsubscribeForecastEvents (): Promise<void> {
if (this.forecastSubscriber) {
this.forecastSubscriber()
.then(() => { this.forecastSubscriber = undefined })
.catch(e => { console.error('clock-weather-card - Error when unsubscribing weather forecast: ', e) })
try {
await this.forecastSubscriber()
console.log('clock-weather-card 2.1.11 - Unsubscribed from weather forecast', this.config.entity)
} catch (e: unknown) {
console.error('clock-weather-card 2.1.11 - Error when unsubscribing from weather forecast: ', JSON.stringify(e))
} finally {
this.forecastSubscriber = undefined
}
}
}

Expand Down

0 comments on commit edf056d

Please sign in to comment.