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 { Registry } from '../Registry';
|
||||||
import { LovelaceChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
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 {
|
abstract class AbstractChip {
|
||||||
/**
|
/**
|
||||||
@@ -18,8 +20,7 @@ abstract class AbstractChip {
|
|||||||
*
|
*
|
||||||
* Child classes should override this property to reflect their own card type and options.
|
* Child classes should override this property to reflect their own card type and options.
|
||||||
*/
|
*/
|
||||||
protected configuration: LovelaceChipConfig = {
|
configuration: LovelaceChipConfig = {
|
||||||
// TODO: Check if this is correct vs custom:mushroom-template-badge. Also in child classes.
|
|
||||||
type: 'template',
|
type: 'template',
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -43,6 +44,21 @@ abstract class AbstractChip {
|
|||||||
getChipConfiguration(): LovelaceChipConfig {
|
getChipConfiguration(): LovelaceChipConfig {
|
||||||
return this.configuration;
|
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;
|
export default AbstractChip;
|
||||||
|
@@ -3,7 +3,6 @@
|
|||||||
import { Registry } from '../Registry';
|
import { Registry } from '../Registry';
|
||||||
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
||||||
import AbstractChip from './AbstractChip';
|
import AbstractChip from './AbstractChip';
|
||||||
import RegistryFilter from '../utilities/RegistryFilter';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fan Chip class.
|
* Fan Chip class.
|
||||||
@@ -19,13 +18,8 @@ class FanChip extends AbstractChip {
|
|||||||
icon_color: 'green',
|
icon_color: 'green',
|
||||||
content: Registry.getCountTemplate('fan', 'eq', 'on'),
|
content: Registry.getCountTemplate('fan', 'eq', 'on'),
|
||||||
tap_action: {
|
tap_action: {
|
||||||
action: 'perform-action',
|
action: 'call-service',
|
||||||
perform_action: 'fan.turn_off',
|
perform_action: 'fan.turn_off',
|
||||||
target: {
|
|
||||||
entity_id: new RegistryFilter(Registry.entities)
|
|
||||||
.whereDomain('fan')
|
|
||||||
.getValuesByProperty('entity_id') as string[],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
hold_action: {
|
hold_action: {
|
||||||
action: 'navigate',
|
action: 'navigate',
|
||||||
|
@@ -3,7 +3,6 @@
|
|||||||
import { Registry } from '../Registry';
|
import { Registry } from '../Registry';
|
||||||
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
||||||
import AbstractChip from './AbstractChip';
|
import AbstractChip from './AbstractChip';
|
||||||
import RegistryFilter from '../utilities/RegistryFilter';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Light Chip class.
|
* Light Chip class.
|
||||||
@@ -19,13 +18,8 @@ class LightChip extends AbstractChip {
|
|||||||
icon_color: 'amber',
|
icon_color: 'amber',
|
||||||
content: Registry.getCountTemplate('light', 'eq', 'on'),
|
content: Registry.getCountTemplate('light', 'eq', 'on'),
|
||||||
tap_action: {
|
tap_action: {
|
||||||
action: 'perform-action',
|
action: 'call-service',
|
||||||
perform_action: 'light.turn_off',
|
perform_action: 'light.turn_off',
|
||||||
target: {
|
|
||||||
entity_id: new RegistryFilter(Registry.entities)
|
|
||||||
.whereDomain('light')
|
|
||||||
.getValuesByProperty('entity_id') as string[],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
hold_action: {
|
hold_action: {
|
||||||
action: 'navigate',
|
action: 'navigate',
|
||||||
|
@@ -3,7 +3,6 @@
|
|||||||
import { Registry } from '../Registry';
|
import { Registry } from '../Registry';
|
||||||
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
import { TemplateChipConfig } from '../types/lovelace-mushroom/utils/lovelace/chip/types';
|
||||||
import AbstractChip from './AbstractChip';
|
import AbstractChip from './AbstractChip';
|
||||||
import RegistryFilter from '../utilities/RegistryFilter';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Switch Chip class.
|
* Switch Chip class.
|
||||||
@@ -19,13 +18,8 @@ class SwitchChip extends AbstractChip {
|
|||||||
icon_color: 'blue',
|
icon_color: 'blue',
|
||||||
content: Registry.getCountTemplate('switch', 'eq', 'on'),
|
content: Registry.getCountTemplate('switch', 'eq', 'on'),
|
||||||
tap_action: {
|
tap_action: {
|
||||||
action: 'perform-action',
|
action: 'call-service',
|
||||||
perform_action: 'switch.turn_off',
|
perform_action: 'switch.turn_off',
|
||||||
target: {
|
|
||||||
entity_id: new RegistryFilter(Registry.entities)
|
|
||||||
.whereDomain('switch')
|
|
||||||
.getValuesByProperty('entity_id') as string[],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
hold_action: {
|
hold_action: {
|
||||||
action: 'navigate',
|
action: 'navigate',
|
||||||
|
Reference in New Issue
Block a user