Support for media_player and cover
This commit is contained in:
parent
2d9e000fa9
commit
d8004152a2
@ -2,16 +2,16 @@ class SliderEntityRow extends Polymer.Element {
|
|||||||
static get template() {
|
static get template() {
|
||||||
let slider = Polymer.html`
|
let slider = Polymer.html`
|
||||||
<paper-slider
|
<paper-slider
|
||||||
min="[[min]]"
|
min="0"
|
||||||
max="[[max]]"
|
max="100"
|
||||||
value="{{value}}"
|
value="{{value}}"
|
||||||
step="[[step]]"
|
step="5"
|
||||||
pin
|
pin
|
||||||
on-change="selectedValue"
|
on-change="selectedValue"
|
||||||
ignore-bar-touch
|
ignore-bar-touch
|
||||||
on-click="stopPropagation">
|
on-click="stopPropagation">
|
||||||
</paper-slider>
|
</paper-slider>
|
||||||
`
|
`;
|
||||||
return Polymer.html`
|
return Polymer.html`
|
||||||
<style>
|
<style>
|
||||||
hui-generic-entity-row {
|
hui-generic-entity-row {
|
||||||
@ -40,7 +40,7 @@ class SliderEntityRow extends Polymer.Element {
|
|||||||
></ha-entity-toggle>
|
></ha-entity-toggle>
|
||||||
</template>
|
</template>
|
||||||
<template is='dom-if' if='{{displayStatus}}'>
|
<template is='dom-if' if='{{displayStatus}}'>
|
||||||
<div>
|
<div style="min-width: 40px">
|
||||||
[[statusString(stateObj)]]
|
[[statusString(stateObj)]]
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@ -62,19 +62,84 @@ class SliderEntityRow extends Polymer.Element {
|
|||||||
breakSlider: { type: Boolean, value: false },
|
breakSlider: { type: Boolean, value: false },
|
||||||
hideWhenOff: { type: Boolean, value: false },
|
hideWhenOff: { type: Boolean, value: false },
|
||||||
showValue: { type: Boolean, value: false },
|
showValue: { type: Boolean, value: false },
|
||||||
isOn: { type: Boolean },
|
|
||||||
stateObj: { type: Object, value: null },
|
stateObj: { type: Object, value: null },
|
||||||
min: { type: Number, value: 0 },
|
|
||||||
max: { type: Number, value: 100 },
|
|
||||||
step: { type: Number, value: 5 },
|
|
||||||
attribute: { type: String, value: 'brightness' },
|
|
||||||
value: Number,
|
value: Number,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
setConfig(config)
|
setConfig(config)
|
||||||
{
|
{
|
||||||
|
const CONTROLLERS = {
|
||||||
|
light: {
|
||||||
|
set: (stateObj, value) => {
|
||||||
|
value = Math.ceil(value/100.0*255);
|
||||||
|
if (value)
|
||||||
|
this._hass.callService('light', 'turn_on', { entity_id: stateObj.entity_id, brightness: value });
|
||||||
|
else
|
||||||
|
this._hass.callService('light', 'turn_off', { entity_id: stateObj.entity_id });
|
||||||
|
},
|
||||||
|
get: (stateObj) => {
|
||||||
|
return (stateObj.state === 'on')?Math.ceil(stateObj.attributes.brightness*100.0/255):0;
|
||||||
|
},
|
||||||
|
supported: (stateObj) => {
|
||||||
|
if('brightness' in stateObj.attributes) return true;
|
||||||
|
if(('supported_features' in stateObj.attributes) &&
|
||||||
|
(stateObj.attributes.supported_features & 1)) return true;
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
string: (stateObj, l18n) => {
|
||||||
|
if(stateObj.state === 'off') return l18n['state.default.off'];
|
||||||
|
return `${this.controller.get(stateObj)} %`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
media_player: {
|
||||||
|
set: (stateObj, value) => {
|
||||||
|
value = value/100.0;
|
||||||
|
this._hass.callService('media_player', 'volume_set', { entity_id: stateObj.entity_id, volume_level: value });
|
||||||
|
},
|
||||||
|
get: (stateObj) => {
|
||||||
|
return (stateObj.attributes.is_volume_muted)?0:Math.ceil(stateObj.attributes.volume_level*100.0);
|
||||||
|
},
|
||||||
|
supported: (stateObj) => {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
string: (stateObj, l18n) => {
|
||||||
|
if (stateObj.attributes.is_volume_muted) return '-';
|
||||||
|
return `${this.controller.get(stateObj)} %`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
cover: {
|
||||||
|
set: (stateObj, value) => {
|
||||||
|
if (value)
|
||||||
|
this._hass.callService('cover', 'set_cover_position', { entity_id: stateObj.entity_id, position: value });
|
||||||
|
else
|
||||||
|
this._hass.callService('cover', 'close_cover', { entity_id: stateObj.entity_id });
|
||||||
|
},
|
||||||
|
get: (stateObj) => {
|
||||||
|
return (stateObj.state === 'open')?stateObj.attributes.current_position:0;
|
||||||
|
},
|
||||||
|
supported: (stateObj) => {
|
||||||
|
if('current_position' in stateObj.attributes) return true;
|
||||||
|
if(('supported_features' in stateObj.attributes) &&
|
||||||
|
(stateObj.attributes.supported_features & 4)) return true;
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
string: (stateObj, l18n) => {
|
||||||
|
if (!this.controller.supported(stateObj)) return '';
|
||||||
|
if (stateObj.state === 'closed') return l18n['state.cover.closed'];
|
||||||
|
return `${this.controller.get(stateObj)} %`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
this._config = config;
|
this._config = config;
|
||||||
|
const domain = config.entity.split('.')[0];
|
||||||
|
this.controller = CONTROLLERS[domain];
|
||||||
|
if(!this.controller) throw new Error('Unsupported entity domain: ' + domain);
|
||||||
|
|
||||||
this.hideToggle = config.hide_control || config.hide_toggle || false;
|
this.hideToggle = config.hide_control || config.hide_toggle || false;
|
||||||
this.breakSlider = config.break_slider || false;
|
this.breakSlider = config.break_slider || false;
|
||||||
this.hideWhenOff = config.hide_when_off || false;
|
this.hideWhenOff = config.hide_when_off || false;
|
||||||
@ -83,13 +148,8 @@ class SliderEntityRow extends Polymer.Element {
|
|||||||
|
|
||||||
statusString(stateObj) {
|
statusString(stateObj) {
|
||||||
let l18n = this._hass.resources[this._hass.language];
|
let l18n = this._hass.resources[this._hass.language];
|
||||||
if(stateObj.state === 'on') {
|
if(!stateObj) return l18n['state.default.unavailable'];
|
||||||
return Math.ceil(stateObj.attributes[this.attribute]/2.55).toString(10);
|
return this.controller.string(stateObj, l18n);
|
||||||
} else if (stateObj.state === 'off') {
|
|
||||||
return l18n['state.default.off'];
|
|
||||||
} else {
|
|
||||||
return l18n['state.default.unavailable'];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateSliders()
|
updateSliders()
|
||||||
@ -116,11 +176,8 @@ class SliderEntityRow extends Polymer.Element {
|
|||||||
this.displayTop = !this.breakSlider;
|
this.displayTop = !this.breakSlider;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!(this.attribute in this.stateObj.attributes)) {
|
if(!this.controller.supported(this.stateObj)) {
|
||||||
if(!('supported_features' in this.stateObj.attributes) ||
|
this.displayTop = this.displayBottom = false;
|
||||||
!(this.stateObj.attributes['supported_features'] & 1)) {
|
|
||||||
this.displayTop = this.displayBottom = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -129,27 +186,13 @@ class SliderEntityRow extends Polymer.Element {
|
|||||||
this._hass = hass;
|
this._hass = hass;
|
||||||
this.stateObj = this._config.entity in hass.states ? hass.states[this._config.entity] : null;
|
this.stateObj = this._config.entity in hass.states ? hass.states[this._config.entity] : null;
|
||||||
if(this.stateObj) {
|
if(this.stateObj) {
|
||||||
if(this.stateObj.state === 'on') {
|
this.value = this.controller.get(this.stateObj);
|
||||||
this.value = this.stateObj.attributes[this.attribute]/2.55;
|
|
||||||
this.isOn = true;
|
|
||||||
} else {
|
|
||||||
this.value = this.min;
|
|
||||||
this.isOn = false;
|
|
||||||
}
|
|
||||||
this.updateSliders();
|
this.updateSliders();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
selectedValue(ev) {
|
selectedValue(ev) {
|
||||||
const value = Math.ceil(parseInt(this.value, 10)*2.55);
|
this.controller.set(this.stateObj, parseInt(this.value, 10));
|
||||||
const param = {entity_id: this.stateObj.entity_id };
|
|
||||||
if(Number.isNaN(value)) return;
|
|
||||||
if(value === 0) {
|
|
||||||
this._hass.callService('light', 'turn_off', param);
|
|
||||||
} else {
|
|
||||||
param[this.attribute] = value;
|
|
||||||
this._hass.callService('light', 'turn_on', param);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stopPropagation(ev) {
|
stopPropagation(ev) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user