Optimize Cards

- Make use of the debug module.
- Make use of the Registry module.
- Align code across the chip modules.
- Make configurations immutable.
- Make use of the refactored types.
This commit is contained in:
DigiLive
2025-04-23 07:48:55 +02:00
parent ffce32d670
commit 946550278b
20 changed files with 449 additions and 607 deletions

View File

@@ -1,59 +1,59 @@
import {Helper} from "../Helper";
import {EntityCardConfig} from "../types/lovelace-mushroom/cards/entity-card-config";
import {cards} from "../types/strategy/cards";
import {generic} from "../types/strategy/generic";
import { Registry } from '../Registry';
import { EntityCardConfig } from '../types/lovelace-mushroom/cards/entity-card-config';
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 new card, extend the new class with this one.
* 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.
*
* @class
* @abstract
* @remarks
* Before using this class, the Registry module must be initialized by calling {@link Registry.initialize}.
*/
abstract class AbstractCard {
/**
* Entity to create the card for.
*
* @type {generic.RegistryEntry}
*/
entity: generic.RegistryEntry;
/** The registry entry this card represents. */
readonly entity: RegistryEntry;
/**
* Configuration of the card.
* The card configuration for this entity.
*
* @type {EntityCardConfig}
* Child classes should override this property to reflect their own card type and options.
*/
config: EntityCardConfig = {
type: "custom:mushroom-entity-card",
icon: "mdi:help-circle",
configuration: EntityCardConfig = {
type: 'custom:mushroom-entity-card',
icon: 'mdi:help-circle',
};
/**
* Class constructor.
*
* @param {generic.RegistryEntry} entity The hass entity to create a card for.
* @throws {Error} If the Helper module isn't initialized.
* @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: generic.RegistryEntry) {
if (!Helper.isInitialized()) {
throw new Error("The Helper module must be initialized before using this one.");
protected constructor(entity: RegistryEntry) {
if (!Registry.initialized) {
logMessage(lvlFatal, 'Registry not initialized!');
}
this.entity = entity;
}
/**
* Get a card.
* Get a card configuration.
*
* @returns {cards.AbstractCardConfig} A card object.
* The configuration should be set by any of the child classes so the card correctly reflects an entity.
*/
getCard(): cards.AbstractCardConfig {
getCard(): AbstractCardConfig {
return {
...this.config,
entity: "entity_id" in this.entity ? this.entity.entity_id : undefined,
...this.configuration,
entity: 'entity_id' in this.entity ? this.entity.entity_id : undefined,
};
}
}
export {AbstractCard};
export default AbstractCard;

View File

@@ -1,68 +1,52 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {AreaRegistryEntry} from "../types/homeassistant/data/area_registry";
import {TemplateCardConfig} from "../types/lovelace-mushroom/cards/template-card-config";
import { AreaRegistryEntry } from '../types/homeassistant/data/area_registry';
import { TemplateCardConfig } from '../types/lovelace-mushroom/cards/template-card-config';
import AbstractCard from './AbstractCard';
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
/**
* Area Card Class
*
* Used to create a card for an entity of the area domain.
*
* @class
* @extends AbstractCard
* Used to create card configuration for an entry of the HASS area registry.
*/
class AreaCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {TemplateCardConfig}
* @private
*/
#defaultConfig: TemplateCardConfig = {
type: "custom:mushroom-template-card",
primary: undefined,
icon: "mdi:floor-plan",
icon_color: "blue",
tap_action: {
action: "navigate",
navigation_path: "",
},
hold_action: {
action: "none",
},
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): TemplateCardConfig {
return {
type: 'custom:mushroom-template-card',
primary: undefined,
icon: 'mdi:floor-plan',
icon_color: 'blue',
tap_action: { action: 'navigate', navigation_path: '' },
hold_action: { action: 'none' },
};
}
/**
* Class constructor.
*
* @param {AreaRegistryEntry} area The area entity to create a card for.
* @param {cards.TemplateCardOptions} [options={}] Options for the card.
*
* @throws {Error} If the Helper module isn't initialized.
* @param {AreaRegistryEntry} area The HASS area to create a card configuration for.
* @param {TemplateCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(area: AreaRegistryEntry, options: cards.TemplateCardOptions = {}) {
constructor(area: AreaRegistryEntry, customConfiguration?: TemplateCardConfig) {
super(area);
const configuration = AreaCard.getDefaultConfig();
let customConfig = customConfiguration;
configuration.primary = area.name;
configuration.icon = area.icon || configuration.icon;
if (configuration.tap_action && 'navigation_path' in configuration.tap_action) {
configuration.tap_action.navigation_path = area.area_id;
}
// Don't override the default card type if default is set in the strategy options.
if (options.type === "default") {
delete options.type;
if (customConfig && customConfig.type === 'default') {
customConfig = { ...customConfig, type: configuration.type };
}
// Initialize the default configuration.
this.#defaultConfig.primary = area.name;
if (this.#defaultConfig.tap_action && ("navigation_path" in this.#defaultConfig.tap_action)) {
this.#defaultConfig.tap_action.navigation_path = area.area_id;
}
// Overwrite the default icon to the user-defined icon in hass.
if (area.icon) {
this.#defaultConfig.icon = area.icon;
}
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...configuration, ...customConfig };
}
}
export {AreaCard};
export default AreaCard;

View File

@@ -1,42 +1,35 @@
import {SensorCard} from "./SensorCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {EntityCardConfig} from "../types/lovelace-mushroom/cards/entity-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { EntityCardConfig } from '../types/lovelace-mushroom/cards/entity-card-config';
import SensorCard from './SensorCard';
/**
* Sensor Card Class
*
* Used to create a card for controlling an entity of the binary_sensor domain.
*
* @class
* @extends SensorCard
* Used to create a card configuration to control an entity of the binary_sensor domain.
*/
class BinarySensorCard extends SensorCard {
/**
* Default configuration of the card.
*
* @type {EntityCardConfig}
* @private
*/
#defaultConfig: EntityCardConfig = {
type: "custom:mushroom-entity-card",
icon: "mdi:power-cycle",
icon_color: "green",
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): EntityCardConfig {
return {
type: 'custom:mushroom-entity-card',
icon: 'mdi:power-cycle',
icon_color: 'green',
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.EntityCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {EntityCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.EntityCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: EntityCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...BinarySensorCard.getDefaultConfig(), ...customConfiguration };
}
}
export {BinarySensorCard};
export default BinarySensorCard;

View File

@@ -1,44 +1,37 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {PictureEntityCardConfig} from "../types/homeassistant/panels/lovelace/cards/types";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { PictureEntityCardConfig } from '../types/homeassistant/panels/lovelace/cards/types';
import AbstractCard from './AbstractCard';
/**
* Camera Card Class
*
* Used to create a card for controlling an entity of the camera domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the camera domain.
*/
class CameraCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {PictureEntityCardConfig}
* @private
*/
#defaultConfig: PictureEntityCardConfig = {
entity: "",
type: "picture-entity",
show_name: false,
show_state: false,
camera_view: "live",
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): PictureEntityCardConfig {
return {
entity: '',
type: 'picture-entity',
show_name: false,
show_state: false,
camera_view: 'live',
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.PictureEntityCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {PictureEntityCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.PictureEntityCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: PictureEntityCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...CameraCard.getDefaultConfig(), ...customConfiguration };
}
}
export {CameraCard};
export default CameraCard;

View File

@@ -1,48 +1,36 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {ClimateCardConfig} from "../types/lovelace-mushroom/cards/climate-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { ClimateCardConfig } from '../types/lovelace-mushroom/cards/climate-card-config';
import AbstractCard from './AbstractCard';
/**
* Climate Card Class
*
* Used to create a card for controlling an entity of the climate domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the climate domain.
*/
class ClimateCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {ClimateCardConfig}
* @private
*/
#defaultConfig: ClimateCardConfig = {
type: "custom:mushroom-climate-card",
icon: undefined,
hvac_modes: [
"off",
"cool",
"heat",
"fan_only",
],
show_temperature_control: true,
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): ClimateCardConfig {
return {
type: 'custom:mushroom-climate-card',
icon: undefined,
hvac_modes: ['off', 'cool', 'heat', 'fan_only'],
show_temperature_control: true,
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.ClimateCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {ClimateCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.ClimateCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: ClimateCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...ClimateCard.getDefaultConfig(), ...customConfiguration };
}
}
export {ClimateCard};
export default ClimateCard;

View File

@@ -1,44 +1,37 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {CoverCardConfig} from "../types/lovelace-mushroom/cards/cover-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { CoverCardConfig } from '../types/lovelace-mushroom/cards/cover-card-config';
import AbstractCard from './AbstractCard';
/**
* Cover Card Class
*
* Used to create a card for controlling an entity of the cover domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the cover domain.
*/
class CoverCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {CoverCardConfig}
* @private
*/
#defaultConfig: CoverCardConfig = {
type: "custom:mushroom-cover-card",
icon: undefined,
show_buttons_control: true,
show_position_control: true,
show_tilt_position_control: true,
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): CoverCardConfig {
return {
type: 'custom:mushroom-cover-card',
icon: undefined,
show_buttons_control: true,
show_position_control: true,
show_tilt_position_control: true,
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.CoverCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {CoverCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.CoverCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: CoverCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...CoverCard.getDefaultConfig(), ...customConfiguration };
}
}
export {CoverCard};
export default CoverCard;

View File

@@ -1,44 +1,37 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {FanCardConfig} from "../types/lovelace-mushroom/cards/fan-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { FanCardConfig } from '../types/lovelace-mushroom/cards/fan-card-config';
import AbstractCard from './AbstractCard';
/**
* Fan Card Class
*
* Used to create a card for controlling an entity of the fan domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the fan domain.
*/
class FanCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {FanCardConfig}
* @private
*/
#defaultConfig: FanCardConfig = {
type: "custom:mushroom-fan-card",
icon: undefined,
show_percentage_control: true,
show_oscillate_control: true,
icon_animation: true,
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): FanCardConfig {
return {
type: 'custom:mushroom-fan-card',
icon: undefined,
show_percentage_control: true,
show_oscillate_control: true,
icon_animation: true,
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.FanCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {FanCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.FanCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: FanCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...FanCard.getDefaultConfig(), ...customConfiguration };
}
}
export {FanCard};
export default FanCard;

View File

@@ -1,49 +1,45 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {AreaRegistryEntry} from "../types/homeassistant/data/area_registry";
import {AreaCardConfig} from "../types/homeassistant/panels/lovelace/cards/types";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { AreaRegistryEntry } from '../types/homeassistant/data/area_registry';
import { AreaCardConfig } from '../types/homeassistant/panels/lovelace/cards/types';
import AbstractCard from './AbstractCard';
/**
* HA Area Card Class
*
* Used to create a card for an entity of the area domain using the built-in type 'area'.
*
* @class
* @extends AbstractCard
* Used to create card configuration for an entry of the HASS area registry.
*/
class AreaCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {AreaCardConfig}
* @private
*/
#defaultConfig: AreaCardConfig = {
type: "area",
area: "",
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): AreaCardConfig {
return {
type: 'area',
area: '',
};
}
/**
* Class constructor.
*
* @param {AreaRegistryEntry} area The area entity to create a card for.
* @param {cards.AreaCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {AreaRegistryEntry} area The HASS entity to create a card configuration for.
* @param {AreaCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(area: AreaRegistryEntry, options: cards.AreaCardOptions = {}) {
constructor(area: AreaRegistryEntry, customConfiguration?: AreaCardConfig) {
super(area);
// Initialize the default configuration.
this.#defaultConfig.area = area.area_id;
this.#defaultConfig.navigation_path = this.#defaultConfig.area;
const configuration = AreaCard.getDefaultConfig();
// Enforce the card type.
delete options.type;
configuration.area = area.area_id;
configuration.navigation_path = configuration.area;
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = {
...this.configuration,
...configuration,
...customConfiguration,
type: configuration.type, // Enforce the card type.
};
}
}
export {AreaCard};
export default AreaCard;

View File

@@ -1,41 +1,34 @@
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {SelectCardConfig} from "../types/lovelace-mushroom/cards/select-card-config";
import {SelectCard} from './SelectCard';
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { SelectCardConfig } from '../types/lovelace-mushroom/cards/select-card-config';
import SelectCard from './SelectCard';
// noinspection JSUnusedGlobalSymbols Class is dynamically imported
/**
* InputSelect Card Class
*
* Used to create a card for controlling an entity of the input_select domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the input_select domain.
*/
class InputSelectCard extends SelectCard {
/**
* Default configuration of the card.
*
* @type {SelectCardConfig}
* @private
*/
#defaultConfig: SelectCardConfig = {
type: "custom:mushroom-select-card",
icon: undefined,
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): SelectCardConfig {
return {
type: 'custom:mushroom-select-card',
icon: undefined,
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.InputSelectCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {SelectCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.InputSelectCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: SelectCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...InputSelectCard.getDefaultConfig(), ...customConfiguration };
}
}
export {InputSelectCard};
export default InputSelectCard;

View File

@@ -1,67 +1,58 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {LightCardConfig} from "../types/lovelace-mushroom/cards/light-card-config";
import {generic} from "../types/strategy/generic";
import isCallServiceActionConfig = generic.isCallServiceActionConfig;
import isCallServiceActionTarget = generic.isCallServiceActionTarget;
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { LightCardConfig } from '../types/lovelace-mushroom/cards/light-card-config';
import { isCallServiceActionConfig, isCallServiceActionTarget } from '../types/strategy/strategy-generics';
import AbstractCard from './AbstractCard';
/**
* Light Card Class
*
* Used to create a card for controlling an entity of the light domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the light domain.
*/
class LightCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {LightCardConfig}
* @private
*/
#defaultConfig: LightCardConfig = {
type: "custom:mushroom-light-card",
icon: undefined,
show_brightness_control: true,
show_color_control: true,
show_color_temp_control: true,
use_light_color: true,
double_tap_action: {
action: "call-service",
service: "light.turn_on",
target: {
entity_id: undefined,
/** Returns the default configuration object for the card. */
static getDefaultConfig(): LightCardConfig {
return {
type: 'custom:mushroom-light-card',
icon: undefined,
show_brightness_control: true,
show_color_control: true,
show_color_temp_control: true,
use_light_color: true,
double_tap_action: {
action: 'call-service',
perform_action: 'light.turn_on',
target: {
entity_id: undefined,
},
data: {
rgb_color: [255, 255, 255],
},
},
data: {
rgb_color: [255, 255, 255],
},
},
};
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.LightCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {LightCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.LightCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: LightCardConfig) {
super(entity);
// Set the target for double-tap action.
const configuration = LightCard.getDefaultConfig();
if (
isCallServiceActionConfig(this.#defaultConfig.double_tap_action)
&& isCallServiceActionTarget(this.#defaultConfig.double_tap_action.target)
isCallServiceActionConfig(configuration.double_tap_action) &&
isCallServiceActionTarget(configuration.double_tap_action.target)
) {
this.#defaultConfig.double_tap_action.target.entity_id = entity.entity_id;
configuration.double_tap_action.target.entity_id = entity.entity_id;
}
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...configuration, ...customConfiguration };
}
}
export {LightCard};
export default LightCard;

View File

@@ -1,41 +1,34 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {LockCardConfig} from "../types/lovelace-mushroom/cards/lock-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { LockCardConfig } from '../types/lovelace-mushroom/cards/lock-card-config';
import AbstractCard from './AbstractCard';
/**
* Lock Card Class
*
* Used to create a card for controlling an entity of the lock domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the lock domain.
*/
class LockCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {LockCardConfig}
* @private
*/
#defaultConfig: LockCardConfig = {
type: "custom:mushroom-lock-card",
icon: undefined,
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): LockCardConfig {
return {
type: 'custom:mushroom-lock-card',
icon: undefined,
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.LockCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {LockCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.LockCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: LockCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...LockCard.getDefaultConfig(), ...customConfiguration };
}
}
export {LockCard};
export default LockCard;

View File

@@ -1,51 +1,37 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {MediaPlayerCardConfig} from "../types/lovelace-mushroom/cards/media-player-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { MediaPlayerCardConfig } from '../types/lovelace-mushroom/cards/media-player-card-config';
import AbstractCard from './AbstractCard';
/**
* Mediaplayer Card Class
*
* Used to create a card for controlling an entity of the media_player domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the media_player domain.
*/
class MediaPlayerCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {MediaPlayerCardConfig}
* @private
*/
#defaultConfig: MediaPlayerCardConfig = {
type: "custom:mushroom-media-player-card",
use_media_info: true,
media_controls: [
"on_off",
"play_pause_stop",
],
show_volume_level: true,
volume_controls: [
"volume_mute",
"volume_set",
"volume_buttons",
],
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): MediaPlayerCardConfig {
return {
type: 'custom:mushroom-media-player-card',
use_media_info: true,
media_controls: ['on_off', 'play_pause_stop'],
show_volume_level: true,
volume_controls: ['volume_mute', 'volume_set', 'volume_buttons'],
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.MediaPlayerCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {MediaPlayerCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.MediaPlayerCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: MediaPlayerCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...MediaPlayerCard.getDefaultConfig(), ...customConfiguration };
}
}
export {MediaPlayerCard};
export default MediaPlayerCard;

View File

@@ -1,40 +1,32 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {EntityCardConfig} from "../types/lovelace-mushroom/cards/entity-card-config";
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { EntityCardConfig } from '../types/lovelace-mushroom/cards/entity-card-config';
import AbstractCard from './AbstractCard';
/**
* Miscellaneous Card Class
*
* Used to create a card an entity of any domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of any domain.
*/
class MiscellaneousCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {EntityCardConfig}
* @private
*/
#defaultConfig: EntityCardConfig = {
type: "custom:mushroom-entity-card",
icon_color: "blue-grey",
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): EntityCardConfig {
return {
type: 'custom:mushroom-entity-card',
icon_color: 'blue-grey',
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.EntityCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {EntityCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.EntityCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: EntityCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...MiscellaneousCard.getDefaultConfig(), ...customConfiguration };
}
}
export {MiscellaneousCard};
export default MiscellaneousCard;

View File

@@ -1,41 +1,34 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {NumberCardConfig} from "../types/lovelace-mushroom/cards/number-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { NumberCardConfig } from '../types/lovelace-mushroom/cards/number-card-config';
import AbstractCard from './AbstractCard';
// noinspection JSUnusedGlobalSymbols Class is dynamically imported
/**
* Number Card Class
*
* Used to create a card for controlling an entity of the number domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the number domain.
*/
class NumberCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {NumberCardConfig}
* @private
*/
#defaultConfig: NumberCardConfig = {
type: "custom:mushroom-number-card",
icon: undefined,
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): NumberCardConfig {
return {
type: 'custom:mushroom-number-card',
icon: undefined,
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.NumberCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {NumberCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.NumberCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: NumberCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...NumberCard.getDefaultConfig(), ...customConfiguration };
}
}
export {NumberCard};
export default NumberCard;

View File

@@ -1,43 +1,35 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {PersonCardConfig} from "../types/lovelace-mushroom/cards/person-card-config";
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { PersonCardConfig } from '../types/lovelace-mushroom/cards/person-card-config';
import AbstractCard from './AbstractCard';
/**
* Person Card Class
*
* Used to create a card for an entity of the Person domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the person domain.
*/
class PersonCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {PersonCardConfig}
* @private
*/
#defaultConfig: PersonCardConfig = {
type: "custom:mushroom-person-card",
layout: "vertical",
primary_info: "none",
secondary_info: "none",
icon_type: "entity-picture",
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): PersonCardConfig {
return {
type: 'custom:mushroom-person-card',
layout: 'vertical',
primary_info: 'none',
secondary_info: 'none',
icon_type: 'entity-picture',
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.PersonCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {PersonCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.PersonCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: PersonCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...PersonCard.getDefaultConfig(), ...customConfiguration };
}
}
export {PersonCard};
export default PersonCard;

View File

@@ -1,63 +1,54 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {generic} from "../types/strategy/generic";
import {EntityCardConfig} from "../types/lovelace-mushroom/cards/entity-card-config";
import {Helper} from "../Helper";
import isCallServiceActionConfig = generic.isCallServiceActionConfig;
import isCallServiceActionTarget = generic.isCallServiceActionTarget;
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { Registry } from '../Registry';
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { EntityCardConfig } from '../types/lovelace-mushroom/cards/entity-card-config';
import { isCallServiceActionConfig, isCallServiceActionTarget } from '../types/strategy/strategy-generics';
import AbstractCard from './AbstractCard';
/**
* Scene Card Class
*
* Used to create a card for an entity of the scene domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the scene domain.
*/
class SceneCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {EntityCardConfig}
* @private
*/
#defaultConfig: EntityCardConfig = {
type: "custom:mushroom-entity-card",
icon: "mdi:palette",
icon_color: "blue",
tap_action: {
action: "call-service",
service: "scene.turn_on",
target: {
entity_id: undefined,
/** Returns the default configuration object for the card. */
static getDefaultConfig(): EntityCardConfig {
return {
type: 'custom:mushroom-entity-card',
icon: 'mdi:palette',
icon_color: 'blue',
tap_action: {
action: 'call-service',
perform_action: 'scene.turn_on',
target: {
entity_id: undefined,
},
},
},
};
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.EntityCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {EntityCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.EntityCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: EntityCardConfig) {
super(entity);
// Set the target for tap action.
const configuration = SceneCard.getDefaultConfig();
if (
isCallServiceActionConfig(this.#defaultConfig.tap_action)
&& isCallServiceActionTarget(this.#defaultConfig.tap_action.target)
isCallServiceActionConfig(configuration.tap_action) &&
isCallServiceActionTarget(configuration.tap_action.target)
) {
this.#defaultConfig.tap_action.target.entity_id = entity.entity_id;
configuration.tap_action.target.entity_id = entity.entity_id;
}
configuration.icon = Registry.hassStates[entity.entity_id]?.attributes.icon ?? configuration.icon;
this.#defaultConfig.icon = Helper.getEntityState(entity)?.attributes.icon ?? this.#defaultConfig.icon;
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...configuration, ...customConfiguration };
}
}
export {SceneCard};
export default SceneCard;

View File

@@ -1,41 +1,34 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {SelectCardConfig} from "../types/lovelace-mushroom/cards/select-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { SelectCardConfig } from '../types/lovelace-mushroom/cards/select-card-config';
import AbstractCard from './AbstractCard';
// noinspection JSUnusedGlobalSymbols Class is dynamically imported
/**
* Select Card Class
*
* Used to create a card for controlling an entity of the select domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the select domain.
*/
class SelectCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {SelectCardConfig}
* @private
*/
#defaultConfig: SelectCardConfig = {
type: "custom:mushroom-select-card",
icon: undefined,
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): SelectCardConfig {
return {
type: 'custom:mushroom-select-card',
icon: undefined,
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.SelectCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {SelectCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.SelectCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: SelectCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...SelectCard.getDefaultConfig(), ...customConfiguration };
}
}
export {SelectCard};
export default SelectCard;

View File

@@ -1,42 +1,34 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {EntityCardConfig} from "../types/lovelace-mushroom/cards/entity-card-config";
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { EntityCardConfig } from '../types/lovelace-mushroom/cards/entity-card-config';
import AbstractCard from './AbstractCard';
/**
* Sensor Card Class
*
* Used to create a card for controlling an entity of the sensor domain.
*
* @class
* @extends AbstractCard
*/
class SensorCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {EntityCardConfig}
* @private
*/
#defaultConfig: EntityCardConfig = {
type: "custom:mushroom-entity-card",
icon: "mdi:information",
animate: true,
line_color: "green",
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): EntityCardConfig {
return {
type: 'custom:mushroom-entity-card',
icon: 'mdi:information',
animate: true,
line_color: 'green',
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.EntityCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {EntityCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.EntityCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: EntityCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...SensorCard.getDefaultConfig(), ...customConfiguration };
}
}
export {SensorCard};
export default SensorCard;

View File

@@ -1,44 +1,37 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {EntityCardConfig} from "../types/lovelace-mushroom/cards/entity-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { EntityCardConfig } from '../types/lovelace-mushroom/cards/entity-card-config';
import AbstractCard from './AbstractCard';
/**
* Switch Card Class
*
* Used to create a card for controlling an entity of the switch domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the switch domain.
*/
class SwitchCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {EntityCardConfig}
* @private
*/
#defaultConfig: EntityCardConfig = {
type: "custom:mushroom-entity-card",
icon: undefined,
tap_action: {
action: "toggle",
},
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): EntityCardConfig {
return {
type: 'custom:mushroom-entity-card',
icon: undefined,
tap_action: {
action: 'toggle',
},
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.EntityCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {EntityCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.EntityCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: EntityCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...SwitchCard.getDefaultConfig(), ...customConfiguration };
}
}
export {SwitchCard};
export default SwitchCard;

View File

@@ -1,46 +1,39 @@
import {AbstractCard} from "./AbstractCard";
import {cards} from "../types/strategy/cards";
import {EntityRegistryEntry} from "../types/homeassistant/data/entity_registry";
import {VACUUM_COMMANDS, VacuumCardConfig} from "../types/lovelace-mushroom/cards/vacuum-card-config";
// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
import { EntityRegistryEntry } from '../types/homeassistant/data/entity_registry';
import { VACUUM_COMMANDS, VacuumCardConfig } from '../types/lovelace-mushroom/cards/vacuum-card-config';
import AbstractCard from './AbstractCard';
/**
* Vacuum Card Class
*
* Used to create a card for controlling an entity of the vacuum domain.
*
* @class
* @extends AbstractCard
* Used to create a card configuration to control an entity of the vacuum domain.
*/
class VacuumCard extends AbstractCard {
/**
* Default configuration of the card.
*
* @type {VacuumCardConfig}
* @private
*/
#defaultConfig: VacuumCardConfig = {
type: "custom:mushroom-vacuum-card",
icon: undefined,
icon_animation: true,
commands: [...VACUUM_COMMANDS],
tap_action: {
action: "more-info",
}
};
/** Returns the default configuration object for the card. */
static getDefaultConfig(): VacuumCardConfig {
return {
type: 'custom:mushroom-vacuum-card',
icon: undefined,
icon_animation: true,
commands: [...VACUUM_COMMANDS],
tap_action: {
action: 'more-info',
},
};
}
/**
* Class constructor.
*
* @param {EntityRegistryEntry} entity The hass entity to create a card for.
* @param {cards.VacuumCardOptions} [options={}] Options for the card.
* @throws {Error} If the Helper module isn't initialized.
* @param {EntityRegistryEntry} entity The HASS entity to create a card configuration for.
* @param {VacuumCardConfig} [customConfiguration] Custom card configuration.
*/
constructor(entity: EntityRegistryEntry, options: cards.VacuumCardOptions = {}) {
constructor(entity: EntityRegistryEntry, customConfiguration?: VacuumCardConfig) {
super(entity);
this.config = Object.assign(this.config, this.#defaultConfig, options);
this.configuration = { ...this.configuration, ...VacuumCard.getDefaultConfig(), ...customConfiguration };
}
}
export {VacuumCard};
export default VacuumCard;