Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cards and tile features to provide a schema to create the editor #16142

Merged
merged 8 commits into from
Jun 21, 2023

Conversation

piitaya
Copy link
Member

@piitaya piitaya commented Apr 12, 2023

Proposed change

Adding easier editor support for custom cards and custom tile features. It only need a form schema to generate the editor.

Benefits:

  • no need to rebuild a component
  • supports all selectors

Downsides :

  • no translation support for label
  • no conditional form

Example (entity card)

  public static async getConfigSchema(): Promise<HaFormSchema[]> {
    return [
      { name: "entity", required: true, selector: { entity: {} } },
      {
        type: "grid",
        name: "",
        schema: [
          { name: "name", selector: { text: {} } },
          {
            name: "icon",
            selector: {
              icon: {},
            },
            context: {
              icon_entity: "entity",
            },
          },
          {
            name: "attribute",
            selector: {
              attribute: {},
            },
            context: {
              filter_entity: "entity",
            },
          },
          { name: "unit", selector: { text: {} } },
          { name: "theme", selector: { theme: {} } },
          { name: "state_color", selector: { boolean: {} } },
        ],
      },
    ];
  }

CleanShot 2023-04-12 at 10 03 26

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

getStubConfig?: (
hass: HomeAssistant,
stateObj?: HassEntity
) => LovelaceTileFeatureConfig;
getConfigElement?: () => LovelaceTileFeatureEditor;
getConfigSchema?: () => HaFormSchema[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getConfigSchema?: () => HaFormSchema[];
getConfigSchema?: () => {schema: HaFormSchema[], computeLabel: (schema: HaFormSchema) => string};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer automatic key generation like : card.{card_name}.editor.fields.{field_name}.name and let custom cards inject translations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would injecting translations work? The card would register a function that we call with a language param?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in customCards window object.

@piitaya piitaya force-pushed the custom_card_editor_schema branch 3 times, most recently from e1dc7af to e24bacb Compare June 7, 2023 18:37
const computeLabel = (_schema: HaFormSchema, localize: LocalizeFunc) =>
localize(`ui.panel.lovelace.editor.card.entity.${_schema.name}`);

return { schema, assertConfig, computeLabel };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keep everything in the editor file?

return import(
      "../editor/config-elements/hui-entity-card-editor"
    );

Comment on lines -105 to -107
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing now

Copy link
Member Author

@piitaya piitaya Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restored this logic. But the most fields are optional. We should align that (or automatically add optional label if need). We can do that in another PR.

CleanShot 2023-06-08 at 14 50 12

@piitaya piitaya force-pushed the custom_card_editor_schema branch 2 times, most recently from 77d19c7 to 9d6f3ad Compare June 8, 2023 12:49
Comment on lines 13 to 14
@customElement("hui-generic-editor")
export class HuiGenericEditor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we name this hui-form-editor to make it more descriptive? And match getConfigForm better?

bramkragten
bramkragten previously approved these changes Jun 21, 2023
@piitaya piitaya force-pushed the custom_card_editor_schema branch from 9d6f3ad to 9ae389b Compare June 21, 2023 10:13
Copy link
Member

@bramkragten bramkragten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs dev documentation and maybe even a dev blog post?

@bramkragten bramkragten merged commit 1fe5d66 into dev Jun 21, 2023
@bramkragten bramkragten deleted the custom_card_editor_schema branch June 21, 2023 15:22
@piitaya
Copy link
Member Author

piitaya commented Jun 21, 2023

At least documentation

@Misiu
Copy link
Contributor

Misiu commented Feb 13, 2024

Any updates on the docs? Sadly I couldn't find anything in https://developers.home-assistant.io/docs/frontend/custom-ui/custom-card/#graphical-card-configuration about getConfigSchema.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants