Skip to content

Commit

Permalink
move fetchCustomActionsFromFile back to a class function and make dup…
Browse files Browse the repository at this point in the history
…licate (yuck) copy in the editor
  • Loading branch information
Nerwyn committed Nov 8, 2024
1 parent 7e7b6d6 commit 8b41ff1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 65 deletions.
6 changes: 3 additions & 3 deletions dist/universal-remote-card.min.js

Large diffs are not rendered by default.

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

export class UniversalRemoteCardEditor extends LitElement {
@property() hass!: HomeAssistant;
Expand Down Expand Up @@ -2195,17 +2189,52 @@ export class UniversalRemoteCardEditor extends LitElement {
}
}

fetchCustomActionsFromFile() {
if (!this.customActionsFromFile && this.config.custom_actions_file) {
const filename = `${
this.config.custom_actions_file.startsWith('/') ? '' : '/'
}${this.config.custom_actions_file}`;
try {
const extension = filename.split('.').pop()?.toLowerCase();
switch (extension) {
case 'json':
this.hass
.fetchWithAuth(filename)
.then((r) => r.json())
.then((json) => {
this.customActionsFromFile = json;
this.requestUpdate();
});
break;
case 'yaml':
case 'yml':
default:
this.hass
.fetchWithAuth(filename)
.then((r) => r.text())
.then((text) => {
this.customActionsFromFile = load(
text,
) as IElementConfig[];
this.requestUpdate();
});
break;
}
} catch (e) {
console.error(
`File ${this.config.custom_actions_file} is not a valid JSON or YAML\n${e}`,
);
}
}
}

render() {
if (!this.hass || !this.config) {
return html``;
}

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

const context = {
config: {
Expand Down
22 changes: 9 additions & 13 deletions src/universal-remote-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ 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 @@ -500,17 +498,7 @@ class UniversalRemoteCard extends LitElement {
return html`<keyboard-dialog .hass=${this.hass}></keyboard-dialog>`;
}

render() {
if (!this.config || !this.hass) {
return html``;
}

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

fetchCustomActionsFromFile() {
if (!this.customActionsFromFile && this.config.custom_actions_file) {
const filename = `${
this.config.custom_actions_file.startsWith('/') ? '' : '/'
Expand Down Expand Up @@ -547,6 +535,14 @@ class UniversalRemoteCard extends LitElement {
);
}
}
}

render() {
if (!this.config || !this.hass) {
return html``;
}

this.fetchCustomActionsFromFile();

this.editMode = Boolean(
document
Expand Down
36 changes: 0 additions & 36 deletions src/utils/fetchCustomActionsFromFile.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './deepKeys';
export * from './defaultActions';
export * from './fetchCustomActionsFromFile';

0 comments on commit 8b41ff1

Please sign in to comment.