55 fireEvent ,
66 HomeAssistant ,
77 ServiceCallRequest ,
8+ stateIcon ,
89} from 'custom-card-helpers' ;
910import registerTemplates from 'ha-template' ;
1011import 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