Skip to content

Commit 05a930d

Browse files
committed
Add battery support
1 parent 54c6660 commit 05a930d

5 files changed

Lines changed: 58 additions & 9 deletions

File tree

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default function buildConfig(
2121
entity: config.entity,
2222
map: config.map ?? '',
2323
map_refresh: config.map_refresh ?? 5,
24+
battery: config.battery ?? '',
2425
image: config.image ?? 'default',
2526
show_name: config.show_name ?? true,
2627
show_status: config.show_status ?? true,

src/editor.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,22 @@ export class VacuumCardEditor extends LitElement implements LovelaceCardEditor {
3535
}
3636
}
3737

38-
private getEntitiesByType(type: string): string[] {
38+
private getEntitiesByType(type: string, deviceClass?: string): string[] {
3939
if (!this.hass) {
4040
return [];
4141
}
42-
return Object.keys(this.hass.states).filter((id) => id.startsWith(type));
42+
43+
const entities = Object.keys(this.hass.states).filter((id) =>
44+
id.startsWith(type),
45+
);
46+
47+
if (deviceClass) {
48+
return entities.filter(
49+
(id) => this.hass?.states[id]?.attributes?.device_class === deviceClass,
50+
);
51+
}
52+
53+
return entities;
4354
}
4455

4556
protected render(): Template {
@@ -48,6 +59,7 @@ export class VacuumCardEditor extends LitElement implements LovelaceCardEditor {
4859
}
4960

5061
const vacuumEntities = this.getEntitiesByType('vacuum');
62+
const batteryEntities = this.getEntitiesByType('sensor', 'battery');
5163
const cameraEntities = [
5264
...this.getEntitiesByType('camera'),
5365
...this.getEntitiesByType('image'),
@@ -76,6 +88,25 @@ export class VacuumCardEditor extends LitElement implements LovelaceCardEditor {
7688
</ha-select>
7789
</div>
7890
91+
<div class="option">
92+
<ha-select
93+
.label=${localize('editor.battery')}
94+
@selected=${this.valueChanged}
95+
.configValue=${'battery'}
96+
.value=${this.config.battery}
97+
@closed=${(e: Event) => e.stopPropagation()}
98+
fixedMenuPosition
99+
naturalMenuWidth
100+
>
101+
${batteryEntities.map(
102+
(entity) =>
103+
html` <mwc-list-item .value=${entity}
104+
>${entity}</mwc-list-item
105+
>`,
106+
)}
107+
</ha-select>
108+
</div>
109+
79110
<div class="option">
80111
<ha-select
81112
.label=${localize('editor.map')}

src/translations/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
},
6262
"editor": {
6363
"entity": "Entity (Required)",
64+
"battery": "Battery Sensor (Optional)",
6465
"map": "Map Camera (Optional)",
6566
"image": "Image (Optional)",
6667
"compact_view": "Compact View",

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface VacuumCardShortcut {
5858

5959
export interface VacuumCardConfig {
6060
entity: string;
61+
battery: string;
6162
map: string;
6263
map_refresh: number;
6364
image: string;

src/vacuum-card.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
fireEvent,
66
HomeAssistant,
77
ServiceCallRequest,
8+
stateIcon,
89
} from 'custom-card-helpers';
910
import registerTemplates from 'ha-template';
1011
import get from 'lodash/get';
@@ -67,7 +68,8 @@ export class VacuumCard extends LitElement {
6768
}
6869

6970
get entity(): VacuumEntity {
70-
return this.hass.states[this.config.entity] as VacuumEntity;
71+
const vacuum = this.hass.states[this.config.entity] as VacuumEntity;
72+
return vacuum;
7173
}
7274

7375
get map(): HassEntity | null {
@@ -77,6 +79,13 @@ export class VacuumCard extends LitElement {
7779
return this.hass.states[this.config.map];
7880
}
7981

82+
get battery(): HassEntity | null {
83+
if (!this.hass || !this.config.battery) {
84+
return null;
85+
}
86+
return this.hass.states[this.config.battery];
87+
}
88+
8089
public setConfig(config: VacuumCardConfig): void {
8190
this.config = buildConfig(config);
8291
}
@@ -218,13 +227,19 @@ export class VacuumCard extends LitElement {
218227

219228
private renderBattery(): Template {
220229
const { battery_level, battery_icon } = this.getAttributes(this.entity);
230+
const batteryLevel = this.battery?.state || battery_level;
231+
const batteryIcon = this?.battery ? stateIcon(this.battery) : battery_icon;
232+
233+
if (batteryLevel && batteryIcon) {
234+
return html`
235+
<div class="tip" @click="${() => this.handleMore()}">
236+
<ha-icon icon="${batteryIcon}"></ha-icon>
237+
<span class="icon-title">${batteryLevel}%</span>
238+
</div>
239+
`;
240+
}
221241

222-
return html`
223-
<div class="tip" @click="${() => this.handleMore()}">
224-
<ha-icon icon="${battery_icon}"></ha-icon>
225-
<span class="icon-title">${battery_level}%</span>
226-
</div>
227-
`;
242+
return nothing;
228243
}
229244

230245
private renderMapOrImage(state: VacuumEntityState): Template {

0 commit comments

Comments
 (0)