forked from gadgetchnnel/lovelace-text-input-row
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lovelace-text-input-row.js
62 lines (54 loc) · 1.66 KB
/
lovelace-text-input-row.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class TextInputRow extends Polymer.Element {
static get template() {
return Polymer.html`
<paper-input
label="[[label]]"
value="[[value]]"
minlength="[[minlength]]"
maxlength="[[maxlength]]"
autoValidate="[[pattern]]"
pattern="[[pattern]]"
type="[[mode]]"
on-input="valueChanged"
id="textinput"
placeholder=""
></paper-input>
`;
}
ready() {
super.ready();
this.$.textinput.addEventListener('click', ev => ev.stopPropagation());
}
setConfig(config) {
this._config = config;
}
valueChanged(ev) {
const newValue = this.$.textinput.value;
const param = {
entity_id: this._config.entity,
value: newValue,
};
this._hass.callService('input_text', 'set_value', param);
}
computeObjectId(entityId) {
return entityId.substr(entityId.indexOf(".") + 1);
}
computeStateName(stateObj){
return stateObj.attributes.friendly_name === undefined
? this.computeObjectId(stateObj.entity_id).replace(/_/g, " ")
: stateObj.attributes.friendly_name || "";
}
set hass(hass) {
this._hass = hass;
this.stateObj = hass.states[this._config.entity];
if(this.stateObj) {
this.value = this.stateObj.state;
this.minlength = this.stateObj.attributes.min;
this.maxlength = this.stateObj.attributes.max;
this.pattern = this.stateObj.attributes.pattern;
this.mode = this.minlength = this.stateObj.attributes.mode;
this.label = this._config.name ? this._config.name : this.computeStateName(this.stateObj);
}
}
}
customElements.define('text-input-row', TextInputRow);