Skip to content

Commit

Permalink
try add yaml parsing for ha-code-editor (codemirror returns as a string)
Browse files Browse the repository at this point in the history
  • Loading branch information
snootched committed Jan 5, 2025
1 parent 8a30be7 commit fe96b99
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
23 changes: 21 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ha-editor-formbuilder-yaml",
"version": "2025.01.2-beta.5",
"version": "2025.01.2-beta.6",
"description": "A reimplementation of ha-editor-formbuilder - supporting YAML configuration of HA Selector controls, conditional expresssions, and more.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -38,6 +38,7 @@
"homepage": "https://github.com/snootched/ha-editor-formbuilder#readme",
"dependencies": {
"custom-card-helpers": "^1.9.0",
"js-yaml": "^4.1.0",
"lit": "^3.1.2",
"typescript": "^5.5.4"
},
Expand Down
47 changes: 22 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HomeAssistant, LovelaceCardConfig, fireEvent } from "custom-card-helpers";
import jsyaml from 'js-yaml';
import { LitElement, CSSResult, css, html } from "lit";
import { unsafeHTML } from "lit/directives/unsafe-html.js";
import { ValueChangedEvent, HAInputElement, ControlRow, Section, isSection} from "./interfaces";
Expand Down Expand Up @@ -179,36 +180,32 @@ export default class EditorForm extends LitElement {
this.requestUpdate();
}


// Helper function to extract the new value based on control type
private _getNewValue(target: HAInputElement, detail?: ValueChangedEvent['detail']): string | boolean | undefined | string[] | number | object {
if (target.tagName === "HA-SELECTOR") {
return detail.value;
/*
if (Array.isArray(detail?.value)) {
return detail?.value;
} else {
return detail?.value !== undefined ? detail.value : target.value;

try {
if (target.tagName === "HA-CODE-EDITOR") {
return jsyaml.load(detail?.value);
}
*/
} else if (target.tagName === "HA-SWITCH") {
return target.checked !== undefined ? target.checked : target.__checked; // Handle switch control
} else if (target.tagName === "HA-CHECKBOX") {
// Return the value of the checkbox, whether checked or unchecked
//console.debug("ha-checkbox target: ", target);
return target.value;
} else if (target.tagName === "HA-FORM") {
// Handle ha-form control
//console.debug("ha-form detail: ", detail);
//console.debug("Object values[0]: ",Object.values(detail.value)[0]);
const formValue = Object.values(detail.value)[0];
return formValue;
} else {
const value = detail?.value !== undefined ? detail.value : target.value;
return value;
} catch (e) {
console.error("Failed to parse YAML input:", e);
return;
}
}


switch (target.tagName) {
case "HA-SELECTOR":
return detail?.value;
case "HA-SWITCH":
return target.checked ?? target.__checked;
case "HA-CHECKBOX":
return target.value;
case "HA-FORM":
return Object.values(detail?.value ?? {})[0];
default:
return detail?.value ?? target.value;
}
}

private _updateConfig(configPath: string[], newValue: any, isArray: boolean = false) {
if (!configPath.length) {
Expand Down

0 comments on commit fe96b99

Please sign in to comment.