Add auto slider/box mode to number entity (#10272)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Franck Nijhof 2021-10-21 07:12:44 +02:00 committed by GitHub
parent f1a0623447
commit d05c76356f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 5 deletions

View File

@ -81,7 +81,11 @@ class HuiNumberEntityRow extends LitElement implements LovelaceRow {
return html` return html`
<hui-generic-entity-row .hass=${this.hass} .config=${this._config}> <hui-generic-entity-row .hass=${this.hass} .config=${this._config}>
${stateObj.attributes.mode === "slider" ${stateObj.attributes.mode === "slider" ||
(stateObj.attributes.mode === "auto" &&
(Number(stateObj.attributes.max) - Number(stateObj.attributes.min)) /
Number(stateObj.attributes.step) <=
256)
? html` ? html`
<div class="flex"> <div class="flex">
<ha-slider <ha-slider

View File

@ -6,6 +6,7 @@ import { html } from "@polymer/polymer/lib/utils/html-tag";
/* eslint-plugin-disable lit */ /* eslint-plugin-disable lit */
import { PolymerElement } from "@polymer/polymer/polymer-element"; import { PolymerElement } from "@polymer/polymer/polymer-element";
import "../components/entity/state-info"; import "../components/entity/state-info";
import "../components/ha-slider";
class StateCardNumber extends mixinBehaviors( class StateCardNumber extends mixinBehaviors(
[IronResizableBehavior], [IronResizableBehavior],
@ -15,6 +16,9 @@ class StateCardNumber extends mixinBehaviors(
return html` return html`
<style include="iron-flex iron-flex-alignment"></style> <style include="iron-flex iron-flex-alignment"></style>
<style> <style>
ha-slider {
margin-left: auto;
}
.state { .state {
@apply --paper-font-body1; @apply --paper-font-body1;
color: var(--primary-text-color); color: var(--primary-text-color);
@ -22,6 +26,12 @@ class StateCardNumber extends mixinBehaviors(
text-align: right; text-align: right;
line-height: 40px; line-height: 40px;
} }
.sliderstate {
min-width: 45px;
}
ha-slider[hidden] {
display: none !important;
}
paper-input { paper-input {
text-align: right; text-align: right;
margin-left: auto; margin-left: auto;
@ -30,6 +40,19 @@ class StateCardNumber extends mixinBehaviors(
<div class="horizontal justified layout" id="number_card"> <div class="horizontal justified layout" id="number_card">
${this.stateInfoTemplate} ${this.stateInfoTemplate}
<ha-slider
min="[[min]]"
max="[[max]]"
value="{{value}}"
step="[[step]]"
hidden="[[hiddenslider]]"
pin
on-change="selectedValueChanged"
on-click="stopPropagation"
id="slider"
ignore-bar-touch=""
>
</ha-slider>
<paper-input <paper-input
no-label-float="" no-label-float=""
auto-validate="" auto-validate=""
@ -41,9 +64,19 @@ class StateCardNumber extends mixinBehaviors(
type="number" type="number"
on-change="selectedValueChanged" on-change="selectedValueChanged"
on-click="stopPropagation" on-click="stopPropagation"
hidden="[[hiddenbox]]"
> >
</paper-input> </paper-input>
<div class="state">[[stateObj.attributes.unit_of_measurement]]</div> <div class="state" hidden="[[hiddenbox]]">
[[stateObj.attributes.unit_of_measurement]]
</div>
<div
id="sliderstate"
class="state sliderstate"
hidden="[[hiddenslider]]"
>
[[value]] [[stateObj.attributes.unit_of_measurement]]
</div>
</div> </div>
`; `;
} }
@ -58,9 +91,31 @@ class StateCardNumber extends mixinBehaviors(
`; `;
} }
ready() {
super.ready();
if (typeof ResizeObserver === "function") {
const ro = new ResizeObserver((entries) => {
entries.forEach(() => {
this.hiddenState();
});
});
ro.observe(this.$.number_card);
} else {
this.addEventListener("iron-resize", () => this.hiddenState());
}
}
static get properties() { static get properties() {
return { return {
hass: Object, hass: Object,
hiddenbox: {
type: Boolean,
value: true,
},
hiddenslider: {
type: Boolean,
value: true,
},
inDialog: { inDialog: {
type: Boolean, type: Boolean,
value: false, value: false,
@ -83,17 +138,44 @@ class StateCardNumber extends mixinBehaviors(
}, },
step: Number, step: Number,
value: Number, value: Number,
mode: String,
}; };
} }
hiddenState() {
if (this.mode !== "slider") return;
const sliderwidth = this.$.slider.offsetWidth;
if (sliderwidth < 100) {
this.$.sliderstate.hidden = true;
} else if (sliderwidth >= 145) {
this.$.sliderstate.hidden = false;
}
}
stateObjectChanged(newVal) { stateObjectChanged(newVal) {
const prevMode = this.mode;
const min = Number(newVal.attributes.min);
const max = Number(newVal.attributes.max);
const step = Number(newVal.attributes.step);
const range = (max - min) / step;
this.setProperties({ this.setProperties({
min: Number(newVal.attributes.min), min: min,
max: Number(newVal.attributes.max), max: max,
step: Number(newVal.attributes.step), step: step,
value: Number(newVal.state), value: Number(newVal.state),
mode: String(newVal.attributes.mode),
maxlength: String(newVal.attributes.max).length, maxlength: String(newVal.attributes.max).length,
hiddenbox:
newVal.attributes.mode === "slider" ||
(newVal.attributes.mode === "auto" && range <= 256),
hiddenslider:
newVal.attributes.mode === "box" ||
(newVal.attributes.mode === "auto" && range > 256),
}); });
if (this.mode === "slider" && prevMode !== "slider") {
this.hiddenState();
}
} }
selectedValueChanged() { selectedValueChanged() {