Skip to content

Instantly share code, notes, and snippets.

@jfierstein
Created April 22, 2019 05:18
Show Gist options
  • Save jfierstein/b6993254c8e0e9e39c2f8413ee46d00f to your computer and use it in GitHub Desktop.
Save jfierstein/b6993254c8e0e9e39c2f8413ee46d00f to your computer and use it in GitHub Desktop.
Custom Lovelace card for displaying Amazon Deliveries via check_mail command line sensor in Home Assistant
class DeliveriesCard extends HTMLElement {
set hass(hass) {
const entityId = this.config.entity;
const stateObj = hass.states[entityId];
const stateStr = stateObj ? stateObj.state : 'unavailable';
const items = JSON.parse(stateStr);
const itemCount = items.length;
if (!this.content) {
const card = document.createElement('ha-card');
card.header = document.createElement('div');
card.id = "deliveriesHeader";
card.header.innerHTML = `<ha-icon style="padding-right: 5px;" icon="mdi:truck-fast"></ha-icon> Deliver${itemCount > 1 ? 'ies' : 'y'} Today (${itemCount} item${itemCount > 1 ? 's' : ''})`;
this.content = document.createElement('div');
this.content.style.padding = '0px 16px 16px';
card.appendChild(this.content);
this.appendChild(card);
}
let innerHtml = ``;
for(let i = 0; i< items.length; i++) {
innerHtml = innerHtml + items[i] + '<br />';
}
this.content.innerHTML = innerHtml;
}
setConfig(config) {
if (!config.entity) {
throw new Error('You need to define an entity');
}
this.config = config;
}
// The height of your card. Home Assistant uses this to automatically
// distribute all cards over the available columns.
getCardSize() {
return 3;
}
}
customElements.define('deliveries-card', DeliveriesCard);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment