From 88d8ad655d94fe57514850f6ee03a7c429b82121 Mon Sep 17 00:00:00 2001 From: Viet Ngoc Date: Tue, 11 Jun 2024 15:54:04 +0200 Subject: [PATCH] add charging indicator with toggle method --- src/css/styles.css | 29 ++++++++++++- src/vehicle-info-card.ts | 88 +++++++++++++++++++++++++++++++++------- 2 files changed, 100 insertions(+), 17 deletions(-) diff --git a/src/css/styles.css b/src/css/styles.css index 332c5aa..a006dc2 100644 --- a/src/css/styles.css +++ b/src/css/styles.css @@ -67,7 +67,8 @@ header h1 { position: relative; width: 100%; height: fit-content; - gap: 1rem; + overflow: hidden; + gap: 0.3rem; flex-wrap: wrap; } .info-box { @@ -86,7 +87,7 @@ header h1 { .info-box .item { display: flex; align-items: center; - gap: 0.2rem; + gap: 0.4rem; span { font-size: 1rem; } @@ -94,6 +95,30 @@ header h1 { margin-bottom: 3px; } } +.item.charge { + cursor: pointer; +} + +.info-box.charge { + overflow: hidden; + padding-top: 0 !important; + justify-content: space-around; + max-height: 0; + opacity: 0; + transition: max-height 0.5s ease, opacity 0.5s ease; +} + +.info-box.charge.active { + max-height: 100px; /* Adjust this to be more than the expected height of the content */ + opacity: 1; +} + +.info-box .item-secondary { + display: flex; + align-items: flex-start; + flex-wrap: nowrap; + flex-direction: column; +} .fuel-wrapper { display: inline-block; diff --git a/src/vehicle-info-card.ts b/src/vehicle-info-card.ts index 42386a2..6b9268e 100644 --- a/src/vehicle-info-card.ts +++ b/src/vehicle-info-card.ts @@ -122,7 +122,7 @@ export class VehicleCard extends LitElement { @state() private activeCardType: string | null = null; private lockAttributesVisible = false; - private windowAttributesVisible = false; + private chargingInfoVisible = false; protected firstUpdated(changedProperties: PropertyValues) { super.firstUpdated(changedProperties); @@ -262,31 +262,89 @@ export class VehicleCard extends LitElement { private _renderMainCard(): TemplateResult { return html`
-
${this._renderWarnings()} ${this._renderRangeInfo()}
+
+ ${this._renderWarnings()} ${this._renderChargingInfo()} ${this._renderRangeInfo()} +
${this._renderHeaderSlides()} ${this._renderMap()} ${this._renderButtons()}
`; } + private isCharging = true; private _renderWarnings(): TemplateResult { // Get the current state of the lock and park brake - const lockState = this.getEntityState(this.sensorDevices.lock?.entity_id); - const parkBrakeState = this.getBooleanState(this.binaryDevices.parkBrake?.entity_id); + const lockState = + lockStateMapping[this.getEntityState(this.sensorDevices.lock?.entity_id)] || lockStateMapping['4']; + const lockSensorState = this.getEntityState(this.sensorDevices.lock?.entity_id); + const lockIconDisplay = lockSensorState === '2' || lockSensorState === '1' ? 'mdi:lock' : 'mdi:lock-open'; - // Determine the display text for the lock state - // Default to "Unknown" if the lock state is not in the formatting object - const lockDisplayText = lockStateMapping[lockState] || lockStateMapping['4']; + const parkBrakeState = this.getBooleanState(this.binaryDevices.parkBrake?.entity_id) ? 'Parked' : 'Released'; - return html` -
-
- -
${lockDisplayText}
-
+ const itemsData = [ + { key: 'lock', state: lockState, icon: lockIconDisplay }, + { key: 'parkBrake', state: parkBrakeState, icon: 'mdi:car-brake-parking' }, + ]; + + const chargingIcon = 'mdi:ev-station'; + + const defaultIdicator = itemsData.map(({ state, icon }) => { + return html`
- -
${parkBrakeState ? 'Parked' : 'Released'}
+ +
${state}
+ `; + }); + + const addedChargingInfo = this.isCharging + ? html`
this.toggleChargingInfo()}> + +
+ Charging + +
+
` + : html``; + + return html`
${defaultIdicator} ${addedChargingInfo}
`; + } + + toggleChargingInfo(): void { + this.chargingInfoVisible = !this.chargingInfoVisible; + console.log('charging toggle: ', this.chargingInfoVisible); + this.requestUpdate(); + } + + private _renderChargingInfo(): TemplateResult | void { + if (!this.isCharging) return; + const chargingClass = this.chargingInfoVisible ? 'info-box charge active' : 'info-box charge'; + const currentChargingState = 80; + const stateUnit = '%'; + const maxSoc = 100; + const mode = 'Standard'; + const power = 3.7; + const powerUnit = 'kW'; + + const chargingItems = [ + { name: 'Power', value: `${power} ${powerUnit}`, icon: 'mdi:flash' }, + { name: 'Current state', value: `${currentChargingState} ${stateUnit}`, icon: 'mdi:battery-charging-medium' }, + { name: 'Maximum', value: `${maxSoc} ${stateUnit}`, icon: 'mdi:battery-charging-100' }, + { name: 'Program', value: mode, icon: 'mdi:ev-station' }, + ]; + + return html` +
+ ${chargingItems.map(({ name, value, icon }) => { + return html` +
+ +
+ ${name} + ${value} +
+
+ `; + })}
`; }