From 05a930d1be49aa31c0b8c8fe494b387e5454b1a9 Mon Sep 17 00:00:00 2001 From: Renato Augusto Gama dos Santos Date: Tue, 7 Jan 2025 07:52:35 -0300 Subject: [PATCH] Add battery support --- src/config.ts | 1 + src/editor.ts | 35 +++++++++++++++++++++++++++++++++-- src/translations/en.json | 1 + src/types.ts | 1 + src/vacuum-card.ts | 29 ++++++++++++++++++++++------- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/config.ts b/src/config.ts index 3c95ed5e..ae04b5a1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -21,6 +21,7 @@ export default function buildConfig( entity: config.entity, map: config.map ?? '', map_refresh: config.map_refresh ?? 5, + battery: config.battery ?? '', image: config.image ?? 'default', show_name: config.show_name ?? true, show_status: config.show_status ?? true, diff --git a/src/editor.ts b/src/editor.ts index ea10fad8..d06a31e7 100644 --- a/src/editor.ts +++ b/src/editor.ts @@ -35,11 +35,22 @@ export class VacuumCardEditor extends LitElement implements LovelaceCardEditor { } } - private getEntitiesByType(type: string): string[] { + private getEntitiesByType(type: string, deviceClass?: string): string[] { if (!this.hass) { return []; } - return Object.keys(this.hass.states).filter((id) => id.startsWith(type)); + + const entities = Object.keys(this.hass.states).filter((id) => + id.startsWith(type), + ); + + if (deviceClass) { + return entities.filter( + (id) => this.hass?.states[id]?.attributes?.device_class === deviceClass, + ); + } + + return entities; } protected render(): Template { @@ -48,6 +59,7 @@ export class VacuumCardEditor extends LitElement implements LovelaceCardEditor { } const vacuumEntities = this.getEntitiesByType('vacuum'); + const batteryEntities = this.getEntitiesByType('sensor', 'battery'); const cameraEntities = [ ...this.getEntitiesByType('camera'), ...this.getEntitiesByType('image'), @@ -76,6 +88,25 @@ export class VacuumCardEditor extends LitElement implements LovelaceCardEditor { +
+ e.stopPropagation()} + fixedMenuPosition + naturalMenuWidth + > + ${batteryEntities.map( + (entity) => + html` ${entity}`, + )} + +
+
+ + ${batteryLevel}% +
+ `; + } - return html` -
- - ${battery_level}% -
- `; + return nothing; } private renderMapOrImage(state: VacuumEntityState): Template {