forked from DigiLive/mushroom-strategy
Adds: - Strategy: - SceneCard: Support for Stateful scenes. Closes #190. - ValveCard: A card to control an entity of the valve domain. Closes #192. - LockView: A view for entities of the lock domain. Closes #171. - Modules: - RegistryFilter: Advanced filtering and sorting of Home Assistant registries. - Auxiliaries: Generic utility functions. - Debug: Centralized logging and debug level management. - Tooling: - ESLint configuration for linting. - Prettier configuration for code formatting. Removes: - Redundant types and interface annotations. - Redundant legacy and unused modules. - Localization constant from the `Helper` module in favor of a new `localize` utility. Refactors: - References regarding change of repository owner. - Auxiliary functions are moved from the `Helper` module to `Auxiliaries` module. - `Helper` module is renamed to `Registry` for centralized state and registry management. - `ControllerCard` is renamed to `HeaderCard`. - Doc-blocks and comments are updated for clarity and consistency. - The file tree is restructured for improved clarity, modularity, and scalability. - Third party type definitions are updated and cleaned up. - Issue and Pull Request templates. Optimizations: - Parallelization of dynamic imports for views and cards. - Improved error handling and logging throughout the codebase. - Sanitization of HASS registries after import. - Enforced stricter and simplified types and interfaces. - Localization now available at global scope. Fixes: - Wrong type in configuration of `HeaderCard`. - Translation files: Resolved key inconsistencies. - Removed unnecessary namespaces from types. - Typos and Grammar. - Explicit use of `any` types. - Turn off all entities of a domain with a chip tap action. - Rendering too small HASS area cards. - Count of active vacuums. Bumps: - Mushroom Strategy - home-assistant-js-websocket - superstruct - ts-loader - ts-node - typescript - webpack - webpack-cli
102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import { HassServiceTarget } from 'home-assistant-js-websocket';
|
|
import { LovelaceCardConfig } from '../types/homeassistant/data/lovelace/config/card';
|
|
import { StackCardConfig } from '../types/homeassistant/panels/lovelace/cards/types';
|
|
import { CustomHeaderCardConfig, StrategyHeaderCardConfig } from '../types/strategy/strategy-cards';
|
|
|
|
/**
|
|
* Header Card class.
|
|
*
|
|
* Used to create a card configuration for a Header Card.
|
|
* The card can be used to describe a group of cards and optionally to control multiple entities.
|
|
*/
|
|
class HeaderCard {
|
|
/** The target to control the entities of. */
|
|
private readonly target: HassServiceTarget;
|
|
/** The current configuration of the card after instantiating this class. */
|
|
private readonly configuration: StrategyHeaderCardConfig;
|
|
|
|
/** Returns the default configuration object for the card. */
|
|
static getDefaultConfig(): StrategyHeaderCardConfig {
|
|
return {
|
|
type: 'custom:mushroom-title-card',
|
|
showControls: true,
|
|
iconOn: 'mdi:power-on',
|
|
iconOff: 'mdi:power-off',
|
|
onService: 'none',
|
|
offService: 'none',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Class constructor.
|
|
*
|
|
* @param {HassServiceTarget} target The target which is optionally controlled by the card.
|
|
* @param {CustomHeaderCardConfig} [customConfiguration] Custom card configuration.
|
|
*
|
|
* @remarks
|
|
* The target object can contain one or multiple ids of different entry types.
|
|
*/
|
|
constructor(target: HassServiceTarget, customConfiguration?: CustomHeaderCardConfig) {
|
|
this.target = target;
|
|
this.configuration = { ...HeaderCard.getDefaultConfig(), ...customConfiguration };
|
|
}
|
|
|
|
/**
|
|
* Create a Header card configuration.
|
|
*
|
|
* @remarks
|
|
* The card is represented by a horizontal stack of cards.
|
|
* One title card and optionally two template cards to control entities.
|
|
*/
|
|
createCard(): StackCardConfig {
|
|
// Create a title card.
|
|
const cards: LovelaceCardConfig[] = [
|
|
{
|
|
type: 'custom:mushroom-title-card',
|
|
title: this.configuration.title,
|
|
subtitle: this.configuration.subtitle,
|
|
},
|
|
];
|
|
|
|
// Add controls to the card.
|
|
if (this.configuration.showControls) {
|
|
cards.push({
|
|
type: 'horizontal-stack',
|
|
cards: [
|
|
{
|
|
type: 'custom:mushroom-template-card',
|
|
icon: this.configuration.iconOff,
|
|
layout: 'vertical',
|
|
icon_color: 'red',
|
|
tap_action: {
|
|
action: 'call-service',
|
|
service: this.configuration.offService,
|
|
target: this.target,
|
|
data: {},
|
|
},
|
|
},
|
|
{
|
|
type: 'custom:mushroom-template-card',
|
|
icon: this.configuration.iconOn,
|
|
layout: 'vertical',
|
|
icon_color: 'amber',
|
|
tap_action: {
|
|
action: 'call-service',
|
|
service: this.configuration.onService,
|
|
target: this.target,
|
|
data: {},
|
|
},
|
|
},
|
|
],
|
|
});
|
|
}
|
|
|
|
return {
|
|
type: 'horizontal-stack',
|
|
cards: cards,
|
|
};
|
|
}
|
|
}
|
|
|
|
export default HeaderCard;
|