Hello, I add the “off” button to the card, but seems not working. I click the “Off” button is only can trigger the on-tap event, but cannot disable the button. Anything i doing wrong. Please help!!!
class CustomFanCard extends Polymer.Element {
static get template() {
return Polymer.html`
<style is="custom-style" include="iron-flex iron-flex-alignment"></style>
<style>
:host {
line-height: 1.5;
}
.speed {
min-width: 40px;
max-width: 40px;
margin-left: 5px;
margin-right: 2px;
}
ha-entity-toggle {
margin-left: 16px;
}
</style>
<hui-generic-entity-row hass="[[hass]]" config="[[_config]]">
<div class='horizontal justified layout' on-click="stopPropagation">
<paper-button
class='speed'
raised noink name="low"
on-tap='setSpeed'
disabled='[[_isLowSpeed]]'>Lo</paper-button>
<paper-button
class='speed'
raised noink name="medium"
on-tap='setSpeed'
disabled='[[_isMedSpeed]]'>Med</paper-button>
<paper-button
class='speed'
raised noink name="high"
on-tap='setSpeed'
disabled='[[_isHghSpeed]]'>Hi</paper-button>
<paper-button
class='speed'
raised noink name="off"
on-tap='turnOff'
disabled='[[_isOff]]'>Off</paper-button>
<ha-entity-toggle hass="[[hass]]" state-obj="[[_stateObj]]"></ha-entity-toggle>
</div>
</hui-generic-entity-row>
`;
}
static get properties() {
return {
hass: {
type: Object,
observer: 'hassChanged'
},
_config: Object,
_stateObj: Object,
_isOff: Boolean,
_isLowSpeed: Boolean,
_isMedSpeed: Boolean,
_isHghSpeed: Boolean,
}
}
setConfig(config) {
this._config = config;
}
hassChanged(hass) {
const config = this._config;
const stateObj = hass.states[config.entity];
let speed;
if (stateObj && stateObj.attributes) {
speed = stateObj.attributes.speed || 'off';
}
this.setProperties({
_stateObj: stateObj,
_isOff: speed === 'off',
_isLowSpeed: speed === 'low',
_isMedSpeed: speed === 'medium',
_isHghSpeed: speed === 'high',
});
}
stopPropagation(e) {
e.stopPropagation();
}
turnOff(e) {
const speed = e.currentTarget.getAttribute('name');
this.hass.callService('script', 'fan_control', {
mqtt_topic: 'ha/dining room fan/cmd/switch',
command: speed
});
}
setSpeed(e) {
const speed = e.currentTarget.getAttribute('name');
this.hass.callService('script', 'fan_control', {
mqtt_topic: 'ha/dining room fan/cmd/level',
command: speed
});
}
}
customElements.define('custom-fan-card', CustomFanCard);