2016-05-29 01:03:51 +02:00
|
|
|
<script>
|
2016-07-19 11:25:26 +02:00
|
|
|
/** @polymerBehavior */
|
2016-05-29 01:03:51 +02:00
|
|
|
window.hassBehavior = {
|
|
|
|
attached: function attached() {
|
|
|
|
var hass = this.hass;
|
|
|
|
|
|
|
|
if (!hass) {
|
|
|
|
throw new Error('No hass property found on ' + this.nodeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.nuclearUnwatchFns = Object.keys(this.properties).reduce(
|
|
|
|
function bindGetters(unwatchFns, key) {
|
2016-07-18 08:18:48 +02:00
|
|
|
var getter;
|
|
|
|
|
2016-05-29 01:03:51 +02:00
|
|
|
if (!('bindNuclear' in this.properties[key])) {
|
|
|
|
return unwatchFns;
|
|
|
|
}
|
|
|
|
|
2016-07-18 08:18:48 +02:00
|
|
|
getter = this.properties[key].bindNuclear(hass);
|
2016-05-29 01:03:51 +02:00
|
|
|
|
|
|
|
if (!getter) {
|
2016-07-31 23:19:27 +02:00
|
|
|
throw new Error('Undefined getter specified for key ' + key +
|
|
|
|
' on ' + this.nodeName);
|
2016-05-29 01:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this[key] = hass.reactor.evaluate(getter);
|
|
|
|
|
|
|
|
return unwatchFns.concat(hass.reactor.observe(getter, function updateAttribute(val) {
|
|
|
|
this[key] = val;
|
|
|
|
}.bind(this)));
|
|
|
|
}.bind(this), []);
|
|
|
|
},
|
|
|
|
|
|
|
|
detached: function detached() {
|
|
|
|
while (this.nuclearUnwatchFns.length) {
|
|
|
|
this.nuclearUnwatchFns.shift()();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|