Skip to content

Commit

Permalink
prioritize custom actions from config over from file, include custom …
Browse files Browse the repository at this point in the history
…actions from file in layout editor list, autofill using custom actions from file for custom actions in config
  • Loading branch information
Nerwyn committed Nov 8, 2024
1 parent 15b26d0 commit 7e7b6d6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
6 changes: 3 additions & 3 deletions dist/universal-remote-card.min.js

Large diffs are not rendered by default.

43 changes: 32 additions & 11 deletions src/universal-remote-card-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ import {
Row,
} from './models/interfaces';
import { defaultIcons } from './models/maps';
import { deepGet, deepSet, getDefaultActions, mergeDeep } from './utils';
import {
deepGet,
deepSet,
fetchCustomActionsFromFile,
getDefaultActions,
mergeDeep,
} from './utils';

export class UniversalRemoteCardEditor extends LitElement {
@property() hass!: HomeAssistant;
Expand All @@ -63,6 +69,8 @@ export class UniversalRemoteCardEditor extends LitElement {
DEFAULT_SOURCES: IElementConfig[] = [];
DEFAULT_ACTIONS: IElementConfig[] = [];

customActionsFromFile?: IElementConfig[];

static get properties() {
return { hass: {}, config: {} };
}
Expand Down Expand Up @@ -1898,8 +1906,14 @@ export class UniversalRemoteCardEditor extends LitElement {
}

buildLayoutEditor() {
const customActionNames =
this.config.custom_actions?.map((entry) => entry.name) ?? [];
const customActionNames = Array.from(
new Set([
...(this.config.custom_actions?.map((entry) => entry.name) ??
[]),
...(this.customActionsFromFile?.map((entry) => entry.name) ??
[]),
]),
);
const defaultKeys = this.DEFAULT_KEYS.filter(
(entry) => !customActionNames.includes(entry.name),
);
Expand Down Expand Up @@ -2187,6 +2201,11 @@ export class UniversalRemoteCardEditor extends LitElement {
}

this.buildPeopleList();
fetchCustomActionsFromFile(
this.hass,
this.config.custom_actions_file,
this.customActionsFromFile,
);

const context = {
config: {
Expand Down Expand Up @@ -2456,7 +2475,10 @@ export class UniversalRemoteCardEditor extends LitElement {
if (parentName && childName) {
const parentActions =
structuredClone(
this.DEFAULT_ACTIONS.filter(
[
...(this.customActionsFromFile ?? []),
...this.DEFAULT_ACTIONS,
].filter(
(defaultActions) =>
defaultActions.name == parentName,
)[0],
Expand All @@ -2469,13 +2491,12 @@ export class UniversalRemoteCardEditor extends LitElement {
};
} else {
const defaultActions =
structuredClone(
this.DEFAULT_ACTIONS.filter(
(defaultActions) =>
defaultActions.name ==
this.renderTemplate(entry.name, context),
)[0],
) ?? {};
[
...(this.customActionsFromFile ?? []),
...this.DEFAULT_ACTIONS,
].filter(
(defaultActions) => defaultActions.name == parentName,
)[0] ?? {};
entry = {
...defaultActions,
...entry,
Expand Down
10 changes: 9 additions & 1 deletion src/universal-remote-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
REPEAT_DELAY,
} from './models/constants';

import { fetchCustomActionsFromFile } from './utils';

console.info(
`%c UNIVERSAL-REMOTE-CARD v${packageInfo.version}`,
'color: white; font-weight: bold; background: green',
Expand Down Expand Up @@ -283,8 +285,8 @@ class UniversalRemoteCard extends LitElement {

getElementConfig(name: string): IElementConfig {
const customActionsList = [
...(this.customActionsFromFile ?? []),
...(this.config.custom_actions ?? []),
...(this.customActionsFromFile ?? []),
];
const customActions = customActionsList.filter(
(customActions) => customActions.name == name,
Expand Down Expand Up @@ -503,6 +505,12 @@ class UniversalRemoteCard extends LitElement {
return html``;
}

fetchCustomActionsFromFile(
this.hass,
this.config.custom_actions_file,
this.customActionsFromFile,
);

if (!this.customActionsFromFile && this.config.custom_actions_file) {
const filename = `${
this.config.custom_actions_file.startsWith('/') ? '' : '/'
Expand Down
36 changes: 36 additions & 0 deletions src/utils/fetchCustomActionsFromFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { load } from 'js-yaml';

import { HomeAssistant, IElementConfig } from '../models/interfaces';

export function fetchCustomActionsFromFile(
hass: HomeAssistant,
filename: string | undefined = undefined,
output: IElementConfig[] | undefined = undefined,
) {
if (!output && filename) {
filename = `${filename.startsWith('/') ? '' : '/'}${filename}`;
try {
const extension = filename.split('.').pop()?.toLowerCase();
switch (extension) {
case 'json':
hass.fetchWithAuth(filename)
.then((r) => r.json())
.then((json) => {
output = json;
});
break;
case 'yaml':
case 'yml':
default:
hass.fetchWithAuth(filename)
.then((r) => r.text())
.then((text) => {
output = load(text) as IElementConfig[];
});
break;
}
} catch (e) {
console.error(`File ${filename} is not a valid JSON or YAML\n${e}`);
}
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './deepKeys';
export * from './defaultActions';
export * from './fetchCustomActionsFromFile';

0 comments on commit 7e7b6d6

Please sign in to comment.