Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Allow slider inversion via yaml config #134

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.vscode/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Available options:
| `full_row` | `true`/`false` | Hide the icon and name and stretch slider to full width | `false` |
| `attribute` | (see below) | Which attribute the slider should control | |
| `dir` | `ltr`/`rtl` | Use this to override your languages Right-To-Left or Left-To-Right setting | language |
| `inverted` | `true`/`false` | Inverts slider percentage for a more natural cover slider from closed to open | `false` |

Most general Entities row options like `name`, `icon` and `tap_action` et.al. are also supported.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "slider-entity-row",
"private": true,
"version": "17.2.1",
"version": "17.3.0",
"description": "slider-entity-row =================",
"scripts": {
"build": "rollup -c",
Expand Down
28 changes: 14 additions & 14 deletions slider-entity-row.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ControllerConfig {
attribute?: string;
grow?: boolean;
dir?: string;
inverted?: boolean;
}

export abstract class Controller {
Expand Down
25 changes: 19 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,34 @@ class SliderEntityRow extends LitElement {
? false
: true;

const formatValue = () =>
this._config.inverted
? (() => {
let matches = /[0-9]{2}/.exec(c.string) || [];
if (!matches.length) {
return c.string;
}
return c.string.replace(matches[0], (100 - c.value).toString());
})()
: c.string;

const content = html`
<div class="wrapper" @click=${(ev) => ev.stopPropagation()}>
<div class="wrapper" @click=${(ev: Event) => ev.stopPropagation()}>
${showSlider
? html`
<ha-slider
.min=${c.min}
.max=${c.max}
.step=${c.step}
.value=${c.value}
.value=${this._config?.inverted ? 100 - c.value : c.value}
.dir=${dir}
pin
@change=${(ev) =>
(c.value = (
@change=${(ev: Event) => {
const target = (
this.shadowRoot.querySelector("ha-slider") as any
).value)}
).value;
c.value = this._config.inverted ? 100 - target : target;
}}
class=${this._config.full_row || this._config.grow
? "full"
: ""}
Expand All @@ -96,7 +109,7 @@ class SliderEntityRow extends LitElement {
? html`<span class="state">
${c.stateObj.state === "unavailable"
? this.hass.localize("state.default.unavailable")
: c.string}
: formatValue()}
</span>`
: ""}
</div>
Expand Down
1 change: 1 addition & 0 deletions test/views/1_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ cards:
- type: custom:slider-entity-row
entity: cover.hall_window
name: cover
inverted: true
- type: custom:slider-entity-row
entity: fan.ceiling_fan
name: fan
Expand Down