const LitElement = Object.getPrototypeOf(
customElements.get("ha-panel-lovelace")
);
const html = LitElement.prototype.html;
const css = LitElement.prototype.css;
function hasConfigOrEntityChanged(element, changedProps) {
if (changedProps.has("_config")) {
return true;
}
const oldHass = changedProps.get("hass");
if (!oldHass) {
return true;
}
for (const entity of element._config.entities) {
const entityId = typeof entity === 'string' ? entity : entity.entity;
if (oldHass.states[entityId] !== element.hass.states[entityId]) {
return true;
}
}
return false;
}
class HvvCard extends LitElement {
static get properties() {
return {
_config: {},
hass: {},
_timeOffset: { type: Number }
};
}
static getConfigElement() {
return document.createElement("hvv-card-editor");
}
static getStubConfig() {
return {
entities: [],
title: "HVV Departures",
max: 5,
show_title: true,
show_name: true,
show_time: false,
show_time_filter: true
};
}
constructor() {
super();
this._timeOffset = 0;
}
_onTimeOffsetChange(e) {
this._timeOffset = parseInt(e.target.value, 10) || 0;
}
setConfig(config) {
if (config.entity) {
throw new Error("The entity property is deprecated, please use entities instead.")
}
if (!config.entities) {
throw new Error("The entities property is required.")
}
this._config = config;
}
shouldUpdate(changedProps) {
return hasConfigOrEntityChanged(this, changedProps);
}
render() {
if (!this._config || !this.hass) {
return html ``;
}
var title = this._config.title ? this._config.title : "HVV Departures";
var showTitle = this._config.show_title !== false;
var showName = this._config.show_name !== false;
var showTimeFilter = this._config.show_time_filter !== false;
return html `
No departures${title}
`
: html``
}
${showTimeFilter ? html`
${displayName}
`
: ""}
${displayName}
`
: ""}
${displayName}
`
: ""
}
${filteredDepartures.map(attr => {
const direction = attr['direction'];
const line = attr['line'];
const type = attr['type'];
const delay_seconds = attr['delay'];
const delay_minutes = (delay_seconds / 60);
const departure = new Date(attr["departure"]);
const cancelled = attr['cancelled'] || false;
const diffMs = departure - today;
const departureHours = Math.floor((diffMs / (1000*60*60)) % 24);
const departureMins = Math.round((diffMs / (1000*60)) % 60);
return html`
`;
})}
${line}
${cancelled ? html`
${direction}` : direction}
${cancelled
? html`Cancelled`
: html`
${this._config.show_time ?
departure.toLocaleTimeString(
this.hass.locale.language,
{
hour: '2-digit',
minute: '2-digit',
hour12: this.hass.locale.time_format === '12'
}
) :
departureHours > 0 ?
departureHours + `:` + departureMins :
departureMins
}
${delay_minutes > 0 ?
html`+${delay_minutes}` :
``}
${delay_minutes <= 0 && this._config.show_time ?
`` :
departureHours > 0 ?
`h:min` :
`min`
}
`
}