diff --git a/src/components/ha-slider.js b/src/components/ha-slider.js new file mode 100644 index 000000000..c2144693c --- /dev/null +++ b/src/components/ha-slider.js @@ -0,0 +1,31 @@ +import '@polymer/paper-slider'; + +const PaperSliderClass = customElements.get('paper-slider'); + +class HaSlider extends PaperSliderClass { + _calcStep(value) { + if (!this.step) { + return parseFloat(value); + } + + const numSteps = Math.round((value - this.min) / this.step); + const stepStr = this.step.toString(); + const stepPointAt = stepStr.indexOf('.'); + if (stepPointAt !== -1) { + /** + * For small values of this.step, if we calculate the step using + * For non-integer values of this.step, if we calculate the step using + * `Math.round(value / step) * step` we may hit a precision point issue + * eg. 0.1 * 0.2 = 0.020000000000000004 + * http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html + * + * as a work around we can round with the decimal precision of `step` + */ + const precision = 10 ** (stepStr.length - stepPointAt - 1); + return Math.round((numSteps * this.step + this.min) * precision) / precision; + } + + return numSteps * this.step + this.min; + } +} +customElements.define('ha-slider', HaSlider); diff --git a/src/panels/lovelace/entity-rows/hui-input-number-entity-row.js b/src/panels/lovelace/entity-rows/hui-input-number-entity-row.js index 17a0de280..3b4d354ca 100644 --- a/src/panels/lovelace/entity-rows/hui-input-number-entity-row.js +++ b/src/panels/lovelace/entity-rows/hui-input-number-entity-row.js @@ -1,11 +1,11 @@ import { html } from '@polymer/polymer/lib/utils/html-tag.js'; import { PolymerElement } from '@polymer/polymer/polymer-element.js'; import '@polymer/paper-input/paper-input.js'; -import '@polymer/paper-slider/paper-slider.js'; import { IronResizableBehavior } from '@polymer/iron-resizable-behavior/iron-resizable-behavior.js'; import { mixinBehaviors } from '@polymer/polymer/lib/legacy/class.js'; import '../components/hui-generic-entity-row.js'; +import '../../../components/ha-slider'; class HuiInputNumberEntityRow extends mixinBehaviors([IronResizableBehavior], PolymerElement) { static get template() { @@ -44,7 +44,7 @@ class HuiInputNumberEntityRow extends mixinBehaviors([IronResizableBehavior], Po