Skip to content

Commit

Permalink
Add speed config for custom effect
Browse files Browse the repository at this point in the history
It seems we cannot adjust the effect speed after-the-fact, so need to
include it here (for use amongst the service call)

This commit also introduced a small `default` config data structure (as
existing uses won't have this value by default)
  • Loading branch information
kizza committed Feb 23, 2024
1 parent 7ba2892 commit fe11a67
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions cypress/e2e/card.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('magic home party', () => {
expect(hass.callService).to.be.calledWith('flux_led', 'set_custom_effect', {
entity_id: cardConfig.entities,
colors: cardConfig.colours,
speed_pct: 20,
transition: 'gradual',
});
});
Expand Down
6 changes: 5 additions & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { css } from 'lit';
import { Colours } from './types';
import { Colours, MagicHomePartyConfig } from './types';

export const DEFAULT_CONFIG: Partial<MagicHomePartyConfig> = {
speed: 20,
}

export const CARD_VERSION = '0.0.1';

Expand Down
20 changes: 19 additions & 1 deletion src/editor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fireEvent, HomeAssistant } from 'custom-card-helpers';
import { css, html, LitElement } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { colours } from './const';
import { colours, DEFAULT_CONFIG } from './const';
import './elements/chip';
import './elements/entities-picker';
import './elements/palette';
Expand All @@ -16,16 +16,19 @@ export class MagicHomePartyEditor extends LitElement {
@state() private config!: MagicHomePartyConfig;
@state() private selectedColours: Colour[] = [];
@state() private selectedEntities: string[] = [];
@state() private speed: number = 50;

public setConfig(config: MagicHomePartyConfig) {
this.config = {
...DEFAULT_CONFIG,
...config,
colours: config.colours || [],
entities: config.entities || [],
};

this.selectedColours = this.config.colours;
this.selectedEntities = this.config.entities;
this.speed = this.config.speed;
}

public render() {
Expand Down Expand Up @@ -62,6 +65,14 @@ export class MagicHomePartyEditor extends LitElement {
@value-changed=${this._entitiesChanged}
>
</magic-home-party-entities-picker>
<h3>Transition speed</h3>
<ha-selector
.label="Speed"
.selector=${{number: {min: 1, max: 100, step: 1, mode: "slider", unit_of_measurement: "%"}}}
.value=${this.speed}
@value-changed=${this._speedChanged}
></ha-selector>
`;
}

Expand Down Expand Up @@ -92,11 +103,18 @@ export class MagicHomePartyEditor extends LitElement {
this._updateConfig();
};

private _speedChanged(event: any) {
event.stopPropagation();
this.speed = event.detail.value;
this._updateConfig();
}

private _updateConfig() {
const newConfig = {
...this.config,
colours: this.selectedColours,
entities: this.selectedEntities,
speed: this.speed,
};

this.config = newConfig;
Expand Down
4 changes: 3 additions & 1 deletion src/magic-home-party-card.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HomeAssistant, LovelaceCardEditor } from 'custom-card-helpers';
import { css, html, LitElement } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { CARD_VERSION, RADIUS } from './const';
import { CARD_VERSION, DEFAULT_CONFIG, RADIUS } from './const';
import './elements/palette';
import { MagicHomePartyConfig } from './types';
import { labelColour, linearGradient } from './util';
Expand All @@ -28,6 +28,7 @@ export class MagicHomeParty extends LitElement {

setConfig(config: MagicHomePartyConfig) {
this.config = {
...DEFAULT_CONFIG,
...config,
title: '',
entities: config.entities || [],
Expand Down Expand Up @@ -82,6 +83,7 @@ export class MagicHomeParty extends LitElement {
this.hass.callService('flux_led', 'set_custom_effect', {
entity_id: this.config.entities,
colors: this.config.colours,
speed_pct: this.config.speed,
transition: 'gradual',
});

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface MagicHomePartyConfig extends LovelaceCardConfig {
title?: string
entities: string[]
colours: Colour[]
speed: number
}

export type EntityFilter = (entity: {entity_id: string}) => boolean
Expand Down

0 comments on commit fe11a67

Please sign in to comment.