diff --git a/package.json b/package.json index d09ad280..91bc1277 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/clock-weather-card.ts b/src/clock-weather-card.ts index a2ff3f59..5cee0f26 100644 --- a/src/clock-weather-card.ts +++ b/src/clock-weather-card.ts @@ -64,9 +64,10 @@ export class ClockWeatherCard extends LitElement { @state() private config!: MergedClockWeatherCardConfig @state() private currentDate!: DateTime - @state() private forecastSubscriber?: () => Promise @state() private forecasts?: WeatherForecast[] @state() private error?: TemplateResult + private forecastSubscriber?: () => Promise + private forecastSubscriberLock = false constructor () { super() @@ -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() } } @@ -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() } } @@ -593,39 +596,59 @@ export class ClockWeatherCard extends LitElement { } } - private subscribeForecastEvents (): void { - this.unsubscribeForecastEvents() + private async subscribeForecastEvents (): Promise { + 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(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(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 { 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 + } } }