Skip to content

Commit

Permalink
Show card in the card picker #198
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwroc committed Oct 22, 2023
1 parent a66f9e5 commit 77069d5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ This card was inspired by [another great card](https://github.com/cbulock/lovela

## Config

### Default card config

When config is empty the card is initialized with the default config which you can find below. Once you start adding custom configuration the default configuration won't be applied hence if you wish to alter the default config please copy-paste it from the below listing.
```yaml
type: custom:battery-state-card
secondary_info: "{last_changed}"
filter:
include:
- name: "attributes.device_class"
value: "battery"
sort:
by: "state"
collapse: 8
bulk_rename:
- from: " Battery"
- from: " level"
```
### Card config
| Name | Type | Default | Since | Description |
|:-----|:-----|:-----|:-----|:-----|
Expand Down
23 changes: 23 additions & 0 deletions src/custom-elements/battery-state-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export class BatteryStateCard extends LovelaceCard<IBatteryCardConfig> {
async internalUpdate(configUpdated: boolean, hassUpdated: boolean) {

if (this.batteryProvider == undefined || configUpdated) {
// checking whether we should apply default config
if (Object.keys(this.config).length == 1) {
this.config = getDefaultConfig();
}

this.batteryProvider = new BatteryProvider(this.config);
}

Expand Down Expand Up @@ -119,4 +124,22 @@ export class BatteryStateCard extends LovelaceCard<IBatteryCardConfig> {
// +1 to account header
return size + 1;
}
}

const getDefaultConfig = () => <IBatteryStateCardConfig>{
sort: {
by: "state"
},
collapse: 8,
filter: {
include: [{
name: "attributes.device_class",
value: "battery"
}]
},
secondary_info: "{last_changed}",
bulk_rename: [
{ from: " Battery" },
{ from: " level" },
]
}
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BatteryStateEntity } from "./custom-elements/battery-state-entity";
import { BatteryStateCard } from "./custom-elements/battery-state-card";
import { log, printVersion } from "./utils";

declare let window: HomeAssistantWindow;

if (customElements.get("battery-state-entity") === undefined) {
printVersion();
Expand All @@ -10,4 +11,12 @@ if (customElements.get("battery-state-entity") === undefined) {
}
else {
log("Element seems to be defined already", "warn");
}
}

window.customCards = window.customCards || [];
window.customCards.push({
type: "battery-state-card",
name: "Battery state card",
preview: true,
description: "Customizable card for listing battery states/levels"
});
12 changes: 12 additions & 0 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,15 @@ interface IMap<T> {

type IObjectOrString<T> = T | string;
type ISimplifiedArray<T> = IObjectOrString<T> | IObjectOrString<T>[] | undefined;

interface HomeAssistantWindow extends Window {
customCards: ICardInfo[] | undefined;
}

interface ICardInfo {
type: string;
name: string;
description: string;
preview?: boolean;
documentationURL?: string;
}

0 comments on commit 77069d5

Please sign in to comment.