Skip to content

Instantly share code, notes, and snippets.

@balloob
Last active June 18, 2018 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balloob/0e9536b1c577c473709059302f29c9db to your computer and use it in GitHub Desktop.
Save balloob/0e9536b1c577c473709059302f29c9db to your computer and use it in GitHub Desktop.
Example custom card for the experimental UI in Home Assistant 0.72
views:
- name: Example
cards:
- type: 'custom:my-element'
entity_id: input_boolean.switch_ac_kitchen
class CustomCard extends HTMLElement {
constructor() {
super();
this.config = null;
this._hass = null;
this._connected = false;
}
// Called when the element is connected to the DOM.
// We assume that both hass and config properties have been set.
connectedCallback() {
this._connected = true;
this.innerHTML = `
<div style='background-color: lightsalmon; padding: 8px;'>
${this.config.entity_id}
<input type='checkbox'>
</div>
`;
this._checkbox = this.querySelector('input');
this._checkbox.addEventListener('change', () =>
this._hass.callService('homeassistant', 'toggle', { entity_id: this.config.entity_id }));
this._render();
}
getCardSize() {
return 1;
}
set hass(value) {
this._hass = value;
if (this._connected) {
this._render();
}
}
_render() {
const state = this._hass.states[this.config.entity_id];
this._checkbox.checked = state.state === 'on';
}
}
customElements.define('my-element', CustomCard);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment