Files
mushroom-strategy/src/cards/AbstractCard.ts
T
DigiLive 5dde2ada98 Fix card configuration
The type of the card configuration was set to EntityCardConfig, which is
too strict as a generic type of card.
The type is loosened to LoveLaceCardConfig.
2025-05-13 12:08:44 +02:00

60 lines
1.8 KiB
TypeScript

import { Registry } from '../Registry';
import { LovelaceCardConfig } from '../types/homeassistant/data/lovelace/config/card';
import { AbstractCardConfig } from '../types/strategy/strategy-cards';
import { RegistryEntry } from '../types/strategy/strategy-generics';
import { logMessage, lvlFatal } from '../utilities/debug';
/**
* Abstract Card Class
*
* To create a card configuration, this class should be extended by a child class.
* Child classes should override the default configuration so the card correctly reflects the entity.
*
* @remarks
* Before using this class, the Registry module must be initialized by calling {@link Registry.initialize}.
*/
abstract class AbstractCard {
/** The registry entry this card represents. */
readonly entity: RegistryEntry;
/**
* The card configuration for this entity.
*
* Child classes should override this property to reflect their own card type and options.
*/
configuration: LovelaceCardConfig = {
type: 'custom:mushroom-entity-card',
icon: 'mdi:help-circle',
};
/**
* Class constructor.
*
* @param {RegistryEntry} entity The registry entry to create a card configuration for.
*
* @remarks
* Before this class can be used, the Registry module must be initialized by calling {@link Registry.initialize}.
*/
protected constructor(entity: RegistryEntry) {
if (!Registry.initialized) {
logMessage(lvlFatal, 'Registry not initialized!');
}
this.entity = entity;
}
/**
* Get a card configuration.
*
* The configuration should be set by any of the child classes so the card correctly reflects an entity.
*/
getCard(): AbstractCardConfig {
return {
...this.configuration,
entity: 'entity_id' in this.entity ? this.entity.entity_id : undefined,
};
}
}
export default AbstractCard;