Last active
June 18, 2018 00:20
-
-
Save balloob/0e9536b1c577c473709059302f29c9db to your computer and use it in GitHub Desktop.
Example custom card for the experimental UI in Home Assistant 0.72
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
views: | |
- name: Example | |
cards: | |
- type: 'custom:my-element' | |
entity_id: input_boolean.switch_ac_kitchen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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