mirror of
https://github.com/DigiLive/mushroom-strategy.git
synced 2025-08-04 20:14:28 +02:00
Optimize Chips
- 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:
@@ -1,6 +1,8 @@
|
||||
import { HassServiceTarget } from 'home-assistant-js-websocket';
|
||||
import { Registry } from '../Registry';
|
||||
import { LovelaceChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
||||
import { logMessage, lvlFatal } from '../utilities/debug';
|
||||
import { isCallServiceActionConfig } from '../types/strategy/strategy-generics';
|
||||
import { logMessage, lvlFatal, lvlWarn } from '../utilities/debug';
|
||||
|
||||
abstract class AbstractChip {
|
||||
/**
|
||||
@@ -18,8 +20,7 @@ abstract class AbstractChip {
|
||||
*
|
||||
* Child classes should override this property to reflect their own card type and options.
|
||||
*/
|
||||
protected configuration: LovelaceChipConfig = {
|
||||
// TODO: Check if this is correct vs custom:mushroom-template-badge. Also in child classes.
|
||||
configuration: LovelaceChipConfig = {
|
||||
type: 'template',
|
||||
};
|
||||
|
||||
@@ -43,6 +44,21 @@ abstract class AbstractChip {
|
||||
getChipConfiguration(): LovelaceChipConfig {
|
||||
return this.configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the target for the tap action.
|
||||
*
|
||||
* @param {HassServiceTarget} target Target of the tap action.
|
||||
*/
|
||||
setTapActionTarget(target: HassServiceTarget) {
|
||||
if ('tap_action' in this.configuration && isCallServiceActionConfig(this.configuration.tap_action)) {
|
||||
this.configuration.tap_action.target = target;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
logMessage(lvlWarn, 'Target not set: Invalid target or tap action.');
|
||||
}
|
||||
}
|
||||
|
||||
export default AbstractChip;
|
||||
|
@@ -3,7 +3,6 @@
|
||||
import { Registry } from '../Registry';
|
||||
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
||||
import AbstractChip from './AbstractChip';
|
||||
import RegistryFilter from '../utilities/RegistryFilter';
|
||||
|
||||
/**
|
||||
* Fan Chip class.
|
||||
@@ -19,13 +18,8 @@ class FanChip extends AbstractChip {
|
||||
icon_color: 'green',
|
||||
content: Registry.getCountTemplate('fan', 'eq', 'on'),
|
||||
tap_action: {
|
||||
action: 'perform-action',
|
||||
action: 'call-service',
|
||||
perform_action: 'fan.turn_off',
|
||||
target: {
|
||||
entity_id: new RegistryFilter(Registry.entities)
|
||||
.whereDomain('fan')
|
||||
.getValuesByProperty('entity_id') as string[],
|
||||
},
|
||||
},
|
||||
hold_action: {
|
||||
action: 'navigate',
|
||||
|
@@ -3,7 +3,6 @@
|
||||
import { Registry } from '../Registry';
|
||||
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
||||
import AbstractChip from './AbstractChip';
|
||||
import RegistryFilter from '../utilities/RegistryFilter';
|
||||
|
||||
/**
|
||||
* Light Chip class.
|
||||
@@ -19,13 +18,8 @@ class LightChip extends AbstractChip {
|
||||
icon_color: 'amber',
|
||||
content: Registry.getCountTemplate('light', 'eq', 'on'),
|
||||
tap_action: {
|
||||
action: 'perform-action',
|
||||
action: 'call-service',
|
||||
perform_action: 'light.turn_off',
|
||||
target: {
|
||||
entity_id: new RegistryFilter(Registry.entities)
|
||||
.whereDomain('light')
|
||||
.getValuesByProperty('entity_id') as string[],
|
||||
},
|
||||
},
|
||||
hold_action: {
|
||||
action: 'navigate',
|
||||
|
@@ -3,7 +3,6 @@
|
||||
import { Registry } from '../Registry';
|
||||
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
||||
import AbstractChip from './AbstractChip';
|
||||
import RegistryFilter from '../utilities/RegistryFilter';
|
||||
|
||||
/**
|
||||
* Switch Chip class.
|
||||
@@ -19,13 +18,8 @@ class SwitchChip extends AbstractChip {
|
||||
icon_color: 'blue',
|
||||
content: Registry.getCountTemplate('switch', 'eq', 'on'),
|
||||
tap_action: {
|
||||
action: 'perform-action',
|
||||
action: 'call-service',
|
||||
perform_action: 'switch.turn_off',
|
||||
target: {
|
||||
entity_id: new RegistryFilter(Registry.entities)
|
||||
.whereDomain('switch')
|
||||
.getValuesByProperty('entity_id') as string[],
|
||||
},
|
||||
},
|
||||
hold_action: {
|
||||
action: 'navigate',
|
||||
|
Reference in New Issue
Block a user