Skip to content

Instantly share code, notes, and snippets.

@balloob
Last active June 21, 2018 13:52
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/d86afbecab97b34f9eaeb33b0c0be424 to your computer and use it in GitHub Desktop.
Save balloob/d86afbecab97b34f9eaeb33b0c0be424 to your computer and use it in GitHub Desktop.
Home Assistant Lovelace + WiredJS
# In Home Assistant UI, navigate to dev-info page and click on "Try out the new Lovelace UI" (HASS 0.72+)
# Reference the file in your configuration file
frontend:
extra_html_url:
- /local/wired-cards.html
# Example entities used in sample ui-lovelacy.yaml below
input_boolean:
switch_ac_kitchen:
name: AC kitchen
switch_ac_livingroom:
name: AC living room
switch_tv:
name: TV
# Example ui-lovelace.yaml. Create this file as <config>/ui-lovelace.yaml
views:
- name: Wired
cards:
- type: "custom:my-wired-toggle"
entities:
- input_boolean.switch_ac_kitchen
- input_boolean.switch_ac_livingroom
- input_boolean.switch_tv
<script>
console.log('loaded wired-cards :)');
// lit-element expects more modern version of ShadyCSS that we don't have.
if (window.ShadyCSS) {
if (!window.ShadyCSS.prepareTemplateDom) {
window.ShadyCSS.prepareTemplateDom = () => {};
}
if (!window.ShadyCSS.prepareTemplateStyles) {
window.ShadyCSS.prepareTemplateStyles = () => {};
}
}
</script>
<link href="https://fonts.googleapis.com/css?family=Gloria+Hallelujah" rel="stylesheet">
<script src='https://unpkg.com/wired-elements@0.6.4/dist/wired-elements.bundled.js' async></script>
<script>
class HAWiredCard extends Polymer.Element {
static get template() {
return Polymer.html`
<style>
:host {
font-family: 'Gloria Hallelujah', cursive;
}
wired-card {
background-color: white;
padding: 16px;
display: block;
font-size: 18px;
}
.state {
display: flex;
justify-content: space-between;
padding: 8px;
align-items: center;
}
wired-toggle {
margin-left: 8px;
}
</style>
<wired-card elevation="2">
<template is="dom-repeat" items="[[_computeEntities(hass, config)]]">
<div class='state'>
[[item.attributes.friendly_name]]
<wired-toggle
checked="[[_computeChecked(item)]]"
on-change="_checkedChanged"
></wired-toggle>
</div>
</template>
</wired-card>
`;
}
static get properties() {
return {
hass: {
type: Object,
},
config: {
type: Object,
},
}
}
_computeEntities(hass, config) {
return config.entities.map(ent => hass.states[ent]);
}
_computeChecked(stateObj) {
return stateObj.state === 'on';
}
_checkedChanged(ev) {
this.hass.callService('homeassistant', 'toggle', {
entity_id: ev.model.item.entity_id
});
}
// The height of your card. Home Assistant uses this to automatically
// distribute all cards over the available columns.
getCardSize() {
return this.config.entities.length + 1;
}
}
customElements.define('my-wired-toggle', HAWiredCard);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment