Last active
March 1, 2019 21:36
-
-
Save thomasloven/a488eab3f1da851ce99e3bbc02aab41e to your computer and use it in GitHub Desktop.
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 DimImageElement extends Polymer.Element { | |
async setConfig(config) { | |
this.dim_entity = config.dim_entity; | |
this.img = document.createElement('hui-image-element') | |
this.img.setConfig(config); | |
this.appendChild(this.img) | |
} | |
set hass(hass) { | |
this.img.hass = hass; | |
this.img.updateComplete.then(() => { | |
if(this.img.shadowRoot) { | |
let value; | |
let state = hass.states[this.dim_entity] | |
if(this.dim_entity.startsWith("light.")) { | |
value = (state.state === 'on') ? state.attributes['brightness']/2.55 : 0; | |
} else { | |
value = state.state; | |
} | |
this.img.shadowRoot.querySelector("hui-image").$.image.style.filter = `opacity(${value}%)`; | |
} | |
}); | |
} | |
} | |
customElements.define('dim-image-element', DimImageElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment