Skip to content

Commit

Permalink
[BUGFIX] Change weather forecast to use websockets
Browse files Browse the repository at this point in the history
Resolves: #42
  • Loading branch information
Rudy Gnodde authored and FamousWolf committed Apr 5, 2024
1 parent 6dfcf08 commit 47f4be6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "Week planner card",
"content_in_root": false,
"render_readme": true,
"filename": "week-planner-card.js"
"filename": "week-planner-card.js",
"homeassistant": "2023.9"
}
19 changes: 18 additions & 1 deletion src/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class WeekPlannerCard extends LitElement {
_locationLink;
_startDate;
_hideWeekend;
_weatherForecast = null;

/**
* Get properties
Expand Down Expand Up @@ -390,6 +391,18 @@ export class WeekPlannerCard extends LitElement {
this._updateEvents();
}

_subscribeToWeatherForecast() {
this._loading++;
this.hass.connection.subscribeMessage((event) => {
this._weatherForecast = event.forecast ?? [];
this._loading--;
}, {
type: 'weather/subscribe_forecast',
forecast_type: 'daily',
entity_id: this._weather.entity
});
}

_updateEvents() {
if (this._loading > 0) {
return;
Expand All @@ -403,6 +416,10 @@ export class WeekPlannerCard extends LitElement {
let startDate = this._startDate;
let endDate = this._startDate.plus({ days: this._numberOfDays });

if (this._weatherForecast === null) {
this._subscribeToWeatherForecast();
}

this._calendars.forEach(calendar => {
this._loading++;
this.hass.callApi(
Expand Down Expand Up @@ -485,7 +502,7 @@ export class WeekPlannerCard extends LitElement {

const weatherState = this._weather ? this.hass.states[this._weather.entity] : null;
let weatherForecast = {};
weatherState?.attributes?.forecast?.forEach((forecast) => {
this._weatherForecast?.forEach((forecast) => {
const dateKey = DateTime.fromISO(forecast.datetime).toISODate();
weatherForecast[dateKey] = {
icon: this._getWeatherIcon(forecast),
Expand Down

0 comments on commit 47f4be6

Please sign in to comment.