Skip to content

Commit

Permalink
fix: soc & maxSoc entities
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocjohn committed Jun 14, 2024
1 parent 7aca342 commit 7c3d95f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ const sensorDeviceFilters: {
tirePressureRearRight: { suffix: '_tirepressurerearright' },
tirePressureFrontLeft: { suffix: '_tirepressurefrontleft' },
tirePressureFrontRight: { suffix: '_tirepressurefrontright' },
maxSoc: { suffix: '_max_soc' },
soc: { suffix: '_soc' },
chargingPower: { suffix: '_chargingPower' },
maxSoc: { prefix: 'sensor.', suffix: '_max_state_of_charge' },
soc: { prefix: 'sensor.', suffix: 'soc' },
chargingPower: { suffix: '_chargingpowerkw' },
};

export const combinedFilters: { [name in keyof Partial<VehicleEntities>]: { prefix?: string; suffix: string } } = {
Expand Down
49 changes: 25 additions & 24 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ export async function getVehicleEntities(

const deviceEntities = allEntities.filter((e) => e.device_id === carEntity.device_id);

const deviceEntitIds: { [key: string]: VehicleEntity } = {};

console.groupCollapsed('Vehicle entities');
console.log('| Name | Entity ID | Unique id |');
console.log('|-----------------------|------------------------|-------------------------|');

for (const deviceEntity of deviceEntities) {
console.log(
`| ${deviceEntity.original_name.padEnd(22)} | ${deviceEntity.entity_id.padEnd(
24,
)} | ${deviceEntity.unique_id.padEnd(24)} |`,
);

deviceEntitIds[deviceEntity.translation_key] = {
entity_id: deviceEntity.entity_id,
original_name: deviceEntity.original_name,
device_id: deviceEntity.device_id,
unique_id: deviceEntity.unique_id,
translation_key: deviceEntity.translation_key,
};
}
console.groupEnd();
console.log('Device entities:', deviceEntitIds);

const entityIds: { [key: string]: VehicleEntity } = {};

for (const entityName of Object.keys(combinedFilters)) {
Expand All @@ -65,11 +41,36 @@ export async function getVehicleEntities(
};
}
} else {
if (entityName === 'soc') {
const socName = 'State of Charge';
const entity = deviceEntities.find((e) => e.original_name === socName);
if (entity) {
entityIds[entityName] = {
entity_id: entity.entity_id,
original_name: entity.original_name,
unique_id: entity.unique_id,
device_id: entity.device_id,
};
}
} else if (entityName === 'maxSoc') {
const maxSocName = 'Max State of Charge';
const entity = deviceEntities.find((e) => e.original_name === maxSocName);
if (entity) {
entityIds[entityName] = {
entity_id: entity.entity_id,
original_name: entity.original_name,
unique_id: entity.unique_id,
device_id: entity.device_id,
};
}
}

const entity = deviceEntities.find((e) => e.entity_id.startsWith(prefix) && e.entity_id.endsWith(suffix));
if (entity) {
entityIds[entityName] = {
entity_id: entity.entity_id,
original_name: entity.original_name,
unique_id: entity.unique_id,
device_id: entity.device_id,
};
}
Expand Down
17 changes: 7 additions & 10 deletions src/vehicle-info-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,12 @@ export class VehicleCard extends LitElement {
}

private _renderRangeInfo(): TemplateResult | void {
const { fuelLevel, rangeLiquid, rangeElectric } = this.vehicleEntities;
const { fuelLevel, rangeLiquid, rangeElectric, soc } = this.vehicleEntities;

const fuelInfo = this.getEntityInfo(fuelLevel?.entity_id);
const rangeLiquidInfo = this.getEntityInfo(rangeLiquid?.entity_id);
const rangeElectricInfo = this.getEntityInfo(rangeElectric?.entity_id);
const socInfo = {
state: this.getEntityAttribute(rangeElectric?.entity_id, 'soc'),
unit: '%',
};
const socInfo = this.getEntityInfo(soc?.entity_id);

const renderInfoBox = (icon: string, state: string, unit: string, rangeState: string, rangeUnit: string) => html`
<div class="info-box">
Expand Down Expand Up @@ -487,9 +484,9 @@ export class VehicleCard extends LitElement {
{ key: 'odometer' },
{ key: 'fuelLevel' },
{ key: 'rangeLiquid' },
{ key: 'rangeElectric' },
{ key: 'soc' },
{ key: 'maxSoc' },
{ key: 'rangeElectric' },
];

const tripFromResetDataKeys = [
Expand Down Expand Up @@ -705,7 +702,7 @@ export class VehicleCard extends LitElement {

if (this.vehicleEntities[key]) {
if (key === 'soc') {
const currentState = this.getEntityAttribute(this.vehicleEntities.rangeElectric?.entity_id, 'soc');
const currentState = this.getEntityState(this.vehicleEntities.soc?.entity_id);
const stateValue = currentState ? parseFloat(currentState) : 0;
const socIcon =
stateValue < 35
Expand All @@ -715,16 +712,16 @@ export class VehicleCard extends LitElement {
: 'mdi:battery-charging-high';
return {
key,
name: name ?? 'State of charger',
name: name ?? this.vehicleEntities.soc?.original_name,
icon: icon ?? socIcon ?? '',
state: state ?? currentState ?? '',
unit: unit ?? '%',
};
} else if (key === 'maxSoc') {
const maxSocState = this.getEntityAttribute(this.vehicleEntities.rangeElectric?.entity_id, 'maxSoc');
const maxSocState = this.getEntityState(this.vehicleEntities.maxSoc?.entity_id) || '0';
return {
key,
name: name ?? this.vehicleEntities.maxSoc?.original_name ?? 'Max state of charger',
name: name ?? this.vehicleEntities.maxSoc?.original_name,
icon: icon ?? `mdi:battery-charging-${maxSocState}`,
state: state ?? maxSocState,
unit: unit ?? '%',
Expand Down

0 comments on commit 7c3d95f

Please sign in to comment.