Skip to content

Commit

Permalink
combined device entities
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocjohn committed Jun 13, 2024
1 parent 0b8c513 commit f88a56c
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 216 deletions.
1 change: 0 additions & 1 deletion src/components/map-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ export class VehicleMap extends LitElement {
throw new Error('Failed to fetch address');
}
} catch (error) {
console.error('Error fetching address:', error);
return null;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,10 @@ export const lockStateMapping = {
'3': 'Partly unlocked',
'4': 'Unknown',
};

export const selectedProgramMapping = {
'0': 'Standard',
'1': 'Unknown',
'2': 'Home',
'3': 'Work',
};
51 changes: 30 additions & 21 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ActionConfig, LovelaceCard, LovelaceCardConfig, LovelaceCardEditor, Themes } from 'custom-card-helpers';

import { VehicleCard } from './vehicle-info-card';
declare global {
interface HTMLElementTagNameMap {
'vehicle-info-card-editor': LovelaceCardEditor;
'hui-error-card': LovelaceCard;
}
interface Window {
BenzCard: VehicleCard | undefined;
}
}
/**
* ExtendedThemes extends the existing Themes interface with additional properties.
Expand Down Expand Up @@ -55,31 +59,43 @@ export const defaultConfig: Partial<VehicleCardConfig> = {
tyre_card: [],
};

export interface SensorDevice {
export interface VehicleEntity {
entity_id: string;
original_name: string;
device_id: string;
}

export interface BinarySensorDevice {
export interface VehicleEntities {
[key: string]: VehicleEntity;
}

export interface VehicleEntity {
entity_id: string;
original_name: string;
device_id: string;
}

export interface SensorDevices {
[key: string]: SensorDevice;
}
export type EntityAttr = {
key: string;
name: string;
icon: string;
unit: string;
state: string;
};

export interface BinarySensors {
[key: string]: BinarySensorDevice;
export interface EntityConfig {
key: string;
name?: string;
icon?: string;
unit?: string;
state?: string;
}

/**
* Filters for binary sensors.
*/
export const binarySensorsFilters: {
[name in keyof Partial<BinarySensors>]: { prefix?: string; suffix: string };
[key: string]: { prefix?: string; suffix: string };
} = {
lock: { prefix: 'lock', suffix: '_lock' },
parkBrake: { suffix: '_parkbrakestatus' },
Expand All @@ -99,9 +115,9 @@ export const binarySensorsFilters: {
* Filters for sensor devices.
*/
export const sensorDeviceFilters: {
[name in keyof Partial<SensorDevices>]: { prefix?: string; suffix: string };
[key: string]: { prefix?: string; suffix: string };
} = {
lock: { prefix: 'sensor.', suffix: '_lock' },
lockSensor: { prefix: 'sensor.', suffix: '_lock' },
averageSpeedReset: { suffix: '_averagespeedreset' },
averageSpeedStart: { suffix: '_averagespeedstart' },
distanceReset: { suffix: '_distancereset' },
Expand Down Expand Up @@ -131,14 +147,7 @@ export const sensorDeviceFilters: {
chargingPower: { suffix: '_chargingPower' },
};

/**
* Interface entityConfig
*/

export interface EntityConfig {
key: string;
name?: string;
icon?: string;
unit?: string;
state?: string;
}
export const combinedFilters: { [name in keyof Partial<VehicleEntities>]: { prefix?: string; suffix: string } } = {
...binarySensorsFilters,
...sensorDeviceFilters,
};
3 changes: 3 additions & 0 deletions src/utils/compute_object_id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** Compute the object ID of a state. */
export const computeObjectId = (entityId: string): string =>
entityId.substr(entityId.indexOf(".") + 1);
13 changes: 13 additions & 0 deletions src/utils/compute_state_name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { HassEntity } from "home-assistant-js-websocket";
import { computeObjectId } from "./compute_object_id";

export const computeStateNameFromEntityAttributes = (
entityId: string,
attributes: { [key: string]: any }
): string =>
attributes.friendly_name === undefined
? computeObjectId(entityId).replace(/_/g, " ")
: (attributes.friendly_name ?? "").toString();

export const computeStateName = (stateObj: HassEntity): string =>
computeStateNameFromEntityAttributes(stateObj.entity_id, stateObj.attributes);
21 changes: 9 additions & 12 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { HomeAssistant } from 'custom-card-helpers';
import { SensorDevice, BinarySensorDevice } from '../types';
import { VehicleEntity } from '../types';
import { combinedFilters } from '../types';

/**
*
* @param hass
* @param config
* @param filters
* @param car
* @returns
*/
export async function getDeviceEntities(

export async function getVehicleEntities(
hass: HomeAssistant,
config: { entity?: string },
filters: {
[key: string]: { prefix?: string; suffix: string };
},
): Promise<{ [key: string]: SensorDevice | BinarySensorDevice }> {
): Promise<{ [key: string]: VehicleEntity }> {
const allEntities = await hass.callWS<
{ entity_id: string; device_id: string; original_name: string; unique_id: string }[]
>({
Expand All @@ -27,10 +24,10 @@ export async function getDeviceEntities(
}

const deviceEntities = allEntities.filter((e) => e.device_id === carEntity.device_id);
const entityIds: { [key: string]: SensorDevice | BinarySensorDevice } = {};
const entityIds: { [key: string]: VehicleEntity } = {};

for (const entityName of Object.keys(filters)) {
const { prefix, suffix } = filters[entityName];
for (const entityName of Object.keys(combinedFilters)) {
const { prefix, suffix } = combinedFilters[entityName];
if (!prefix) {
const entity = deviceEntities.find((e) => e.unique_id.endsWith(suffix));
if (entity) {
Expand Down
Loading

0 comments on commit f88a56c

Please sign in to comment.