Skip to content

Commit

Permalink
feat: Per entity animation configuration overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Sese-Schneider committed Feb 9, 2023
1 parent bfeaf6d commit 3ac9136
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,26 @@ A simple card that shows your current energy usage.

#### PowerEntity

| Name | Type | Requirement | Description | Default |
|----------------|--------------|--------------|-------------------------------|----------------------------------------|
| power | state entity | **Required** | State entity for power | |
| current | state entity | *Optional* | State entity for current | |
| voltage | state entity | *Optional* | State entity for voltage | |
| power_factor | state entity | *Optional* | State entity for power_factor | |
| label_leading | string | *Optional* | Leading label | |
| label_trailing | string | *Optional* | Trailing label | |
| icon_leading | string | *Optional* | Leading MD icon | `mdi:transmission-tower` |
| icon_trailing | string | *Optional* | Trailing MD icon | `mdi:home-lightning-bolt` |
| color | string | *Optional* | CSS color | `var(--energy-grid-consumption-color)` |
| Name | Type | Requirement | Description | Default |
|----------------|-----------------|--------------|------------------------------------|----------------------------------------|
| power | state entity | **Required** | State entity for power | |
| current | state entity | *Optional* | State entity for current | |
| voltage | state entity | *Optional* | State entity for voltage | |
| power_factor | state entity | *Optional* | State entity for power_factor | |
| label_leading | string | *Optional* | Leading label | |
| label_trailing | string | *Optional* | Trailing label | |
| icon_leading | string | *Optional* | Leading MD icon | `mdi:transmission-tower` |
| icon_trailing | string | *Optional* | Trailing MD icon | `mdi:home-lightning-bolt` |
| color | string | *Optional* | CSS color | `var(--energy-grid-consumption-color)` |
| animation | AnimationConfig | *Optional* | Overwrite for the global animation | |

#### AnimationConfig

| Name | Type | Requirement | Description | Default |
|--------------|--------|-------------|--------------------------------------------------------------------|---------|
| power | number | *Optional* | Wattage level at which the animation runs at `min_duration` speed. | 1000 |
| min_duration | number | *Optional* | Minimum duration of the animation at `>= power W`. | 1 |
| max_duration | number | *Optional* | Maximum duration of the animation at `> 0 W`. | 10 |
| Name | Type | Requirement | Description | Default |
|--------------|--------|-------------|-------------------------------------------------------------------|---------|
| power | number | *Optional* | Wattage level at which the animation runs at `min_duration` speed | 1000 |
| min_duration | number | *Optional* | Minimum duration of the animation at `>= power W` | 1 |
| max_duration | number | *Optional* | Maximum duration of the animation at `> 0 W` | 10 |

### Example configuration

Expand All @@ -74,6 +75,10 @@ entities:
label_leading: 'P'
label_trailing: 'B'
color: '#7dbff5'
animation:
power: 100
min_duration: 2
max_duration: 5
- power: sensor.c_p
current: sensor.c_c
voltage: sensor.c_v
Expand Down
12 changes: 6 additions & 6 deletions src/energy-overview-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ class EnergyOverviewCard extends LitElement {
label_leading: entity.label_leading ? entity.label_leading : '',
icon_trailing: entity.icon_trailing ? entity.icon_trailing : 'mdi:home-lightning-bolt',
icon_leading: entity.icon_leading ? entity.icon_leading : 'mdi:transmission-tower',
animation: entity.animation ?? this._config?.animation,
});
});

const {animation} = this._config;
const min = animation?.min_duration ? animation.min_duration : 1;
const max = animation?.max_duration ? animation.max_duration : 10;
const power = animation?.power ? animation.power : 1000;

return html`
<ha-card .header="Energy Overview">
${entities.map((entity) => {
${entities.map((entity, i) => {
const {animation} = entity;
const min = animation?.min_duration ? animation.min_duration : 1;
const max = animation?.max_duration ? animation.max_duration : 10;
const power = animation?.power ? animation.power : 1000;
// a linear function which is max at x=0 and min at x=power is defined by:
// f(x) = (-(max-min)/power) * x + max
const x = parseInt(entity.power, 10);
Expand Down
13 changes: 7 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export interface EnergyOverviewAnimation {
min_duration?: number;
max_duration?: number;
power?: number;
}

export interface EnergyOverviewEntity {
power: string;
current?: string;
Expand All @@ -8,12 +14,7 @@ export interface EnergyOverviewEntity {
icon_trailing?: string;
icon_leading?: string;
color?: string;
}

export interface EnergyOverviewAnimation {
min_duration?: number;
max_duration?: number;
power?: number;
animation?: EnergyOverviewAnimation;
}

export interface EnergyOverviewConfig {
Expand Down

0 comments on commit 3ac9136

Please sign in to comment.