Fix localize key types related to form schemas (Group 2) (#13342)

This commit is contained in:
Steve Repsher 2022-08-17 12:01:05 -04:00 committed by GitHub
parent eb4dbef610
commit 9a1fc02755
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 199 additions and 205 deletions

View File

@ -112,7 +112,7 @@ export interface DurationSelector {
export interface EntitySelector {
entity: {
integration?: string;
domain?: string | string[];
domain?: string | readonly string[];
device_class?: string;
multiple?: boolean;
include_entities?: string[];
@ -180,7 +180,7 @@ export interface SelectSelector {
multiple?: boolean;
custom_value?: boolean;
mode?: "list" | "dropdown";
options: string[] | SelectOption[];
options: readonly string[] | readonly SelectOption[];
};
}

View File

@ -13,7 +13,7 @@ import {
} from "superstruct";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { GaugeCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
@ -54,57 +54,66 @@ export class HuiGaugeCardEditor
this._config = config;
}
private _schema = memoizeOne((showSeverity: boolean) => {
const schema = [
{
name: "entity",
selector: {
entity: {
domain: ["counter", "input_number", "number", "sensor"],
private _schema = memoizeOne(
(showSeverity: boolean) =>
[
{
name: "entity",
selector: {
entity: {
domain: ["counter", "input_number", "number", "sensor"],
},
},
},
},
{
name: "",
type: "grid",
schema: [
{ name: "name", selector: { text: {} } },
{ name: "unit", selector: { text: {} } },
],
},
{ name: "theme", selector: { theme: {} } },
{
name: "",
type: "grid",
schema: [
{ name: "min", selector: { number: { min: 1, mode: "box" } } },
{ name: "max", selector: { number: { min: 1, mode: "box" } } },
],
},
{
name: "",
type: "grid",
schema: [
{ name: "needle", selector: { boolean: {} } },
{ name: "show_severity", selector: { boolean: {} } },
],
},
];
if (showSeverity) {
schema.push({
name: "",
type: "grid",
schema: [
{ name: "green", selector: { number: { min: 0, mode: "box" } } },
{ name: "yellow", selector: { number: { min: 0, mode: "box" } } },
{ name: "red", selector: { number: { min: 0, mode: "box" } } },
],
});
}
return schema;
});
{
name: "",
type: "grid",
schema: [
{ name: "name", selector: { text: {} } },
{ name: "unit", selector: { text: {} } },
],
},
{ name: "theme", selector: { theme: {} } },
{
name: "",
type: "grid",
schema: [
{ name: "min", selector: { number: { min: 1, mode: "box" } } },
{ name: "max", selector: { number: { min: 1, mode: "box" } } },
],
},
{
name: "",
type: "grid",
schema: [
{ name: "needle", selector: { boolean: {} } },
{ name: "show_severity", selector: { boolean: {} } },
],
},
...(showSeverity
? ([
{
name: "",
type: "grid",
schema: [
{
name: "green",
selector: { number: { min: 0, mode: "box" } },
},
{
name: "yellow",
selector: { number: { min: 0, mode: "box" } },
},
{
name: "red",
selector: { number: { min: 0, mode: "box" } },
},
],
},
] as const)
: []),
] as const
);
protected render(): TemplateResult {
if (!this.hass || !this._config) {
@ -152,7 +161,9 @@ export class HuiGaugeCardEditor
fireEvent(this, "config-changed", { config });
}
private _computeLabelCallback = (schema: HaFormSchema) => {
private _computeLabelCallback = (
schema: SchemaUnion<ReturnType<typeof this._schema>>
) => {
switch (schema.name) {
case "name":
return this.hass!.localize(
@ -186,18 +197,16 @@ export class HuiGaugeCardEditor
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
case "unit":
return this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.unit"
);
default:
// "green" | "yellow" | "red"
return this.hass!.localize(
`ui.panel.lovelace.editor.card.gauge.severity.${schema.name}`
);
}
return (
this.hass!.localize(
`ui.panel.lovelace.editor.card.gauge.${schema.name}`
) ||
this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
) ||
this.hass!.localize(
`ui.panel.lovelace.editor.card.gauge.severity.${schema.name}`
)
);
};
}

View File

@ -20,7 +20,7 @@ import type { LovelaceCardEditor } from "../../types";
import { processEditorEntities } from "../process-editor-entities";
import { entitiesConfigStruct } from "../structs/entities-struct";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
@ -36,7 +36,7 @@ const cardConfigStruct = assign(
})
);
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{ name: "title", selector: { text: {} } },
{
name: "",
@ -57,7 +57,7 @@ const SCHEMA: HaFormSchema[] = [
],
},
{ name: "state_color", selector: { boolean: {} } },
];
] as const;
@customElement("hui-glance-card-editor")
export class HuiGlanceCardEditor
@ -117,22 +117,23 @@ export class HuiGlanceCardEditor
fireEvent(this, "config-changed", { config });
}
private _computeLabelCallback = (schema: HaFormSchema) => {
if (schema.name === "theme") {
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
switch (schema.name) {
case "theme":
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
case "columns":
return this.hass!.localize(
`ui.panel.lovelace.editor.card.glance.${schema.name}`
);
default:
return this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
);
}
return (
this.hass!.localize(
`ui.panel.lovelace.editor.card.glance.${schema.name}`
) ||
this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
)
);
};
}

View File

@ -12,7 +12,7 @@ import {
string,
} from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { GridCardConfig } from "../../cards/types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { HuiStackCardEditor } from "./hui-stack-card-editor";
@ -27,7 +27,7 @@ const cardConfigStruct = assign(
})
);
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{
type: "grid",
name: "",
@ -36,7 +36,7 @@ const SCHEMA: HaFormSchema[] = [
{ name: "square", selector: { boolean: {} } },
],
},
];
] as const;
@customElement("hui-grid-card-editor")
export class HuiGridCardEditor extends HuiStackCardEditor {
@ -68,7 +68,7 @@ export class HuiGridCardEditor extends HuiStackCardEditor {
fireEvent(this, "config-changed", { config: ev.detail.value });
}
private _computeLabelCallback = (schema: HaFormSchema) =>
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) =>
this.hass!.localize(`ui.panel.lovelace.editor.card.grid.${schema.name}`);
}

View File

@ -19,7 +19,7 @@ import type { LovelaceCardEditor } from "../../types";
import { processEditorEntities } from "../process-editor-entities";
import { entitiesConfigStruct } from "../structs/entities-struct";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
@ -31,7 +31,7 @@ const cardConfigStruct = assign(
})
);
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{ name: "title", selector: { text: {} } },
{
name: "",
@ -44,7 +44,7 @@ const SCHEMA: HaFormSchema[] = [
},
],
},
];
] as const;
@customElement("hui-history-graph-card-editor")
export class HuiHistoryGraphCardEditor
@ -97,7 +97,7 @@ export class HuiHistoryGraphCardEditor
fireEvent(this, "config-changed", { config });
}
private _computeLabelCallback = (schema: HaFormSchema) =>
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) =>
this.hass!.localize(`ui.panel.lovelace.editor.card.generic.${schema.name}`);
static styles: CSSResultGroup = css`

View File

@ -3,7 +3,7 @@ import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { HumidifierCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
@ -18,7 +18,7 @@ const cardConfigStruct = assign(
})
);
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{
name: "entity",
required: true,
@ -32,7 +32,7 @@ const SCHEMA: HaFormSchema[] = [
{ name: "theme", selector: { theme: {} } },
],
},
];
] as const;
@customElement("hui-humidifier-card-editor")
export class HuiHumidifierCardEditor
@ -68,7 +68,7 @@ export class HuiHumidifierCardEditor
fireEvent(this, "config-changed", { config: ev.detail.value });
}
private _computeLabelCallback = (schema: HaFormSchema) => {
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
if (schema.name === "entity") {
return this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.entity"

View File

@ -3,7 +3,7 @@ import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { IframeCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
@ -18,7 +18,7 @@ const cardConfigStruct = assign(
})
);
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{ name: "title", selector: { text: {} } },
{
name: "",
@ -28,7 +28,7 @@ const SCHEMA: HaFormSchema[] = [
{ name: "aspect_ratio", selector: { text: {} } },
],
},
];
] as const;
@customElement("hui-iframe-card-editor")
export class HuiIframeCardEditor
@ -64,7 +64,7 @@ export class HuiIframeCardEditor
fireEvent(this, "config-changed", { config: ev.detail.value });
}
private _computeLabelCallback = (schema: HaFormSchema) =>
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) =>
this.hass!.localize(`ui.panel.lovelace.editor.card.generic.${schema.name}`);
}

View File

@ -15,7 +15,7 @@ import { configElementStyle } from "./config-elements-style";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { domainIcon } from "../../../../common/entity/domain_icon";
import { computeDomain } from "../../../../common/entity/compute_domain";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
@ -44,37 +44,34 @@ export class HuiLightCardEditor
}
private _schema = memoizeOne(
(
entity: string,
icon: string | undefined,
entityState: HassEntity
): HaFormSchema[] => [
{
name: "entity",
required: true,
selector: { entity: { domain: "light" } },
},
{
type: "grid",
name: "",
schema: [
{ name: "name", selector: { text: {} } },
{
name: "icon",
selector: {
icon: {
placeholder: icon || entityState?.attributes.icon,
fallbackPath:
!icon && !entityState?.attributes.icon && entityState
? domainIcon(computeDomain(entity), entityState)
: undefined,
(entity: string, icon: string | undefined, entityState: HassEntity) =>
[
{
name: "entity",
required: true,
selector: { entity: { domain: "light" } },
},
{
type: "grid",
name: "",
schema: [
{ name: "name", selector: { text: {} } },
{
name: "icon",
selector: {
icon: {
placeholder: icon || entityState?.attributes.icon,
fallbackPath:
!icon && !entityState?.attributes.icon && entityState
? domainIcon(computeDomain(entity), entityState)
: undefined,
},
},
},
},
],
},
{ name: "theme", selector: { theme: {} } },
]
],
},
{ name: "theme", selector: { theme: {} } },
] as const
);
get _hold_action(): ActionConfig {
@ -172,7 +169,9 @@ export class HuiLightCardEditor
fireEvent(this, "config-changed", { config: ev.detail.value });
}
private _computeLabelCallback = (schema: HaFormSchema) => {
private _computeLabelCallback = (
schema: SchemaUnion<ReturnType<typeof this._schema>>
) => {
if (schema.name === "entity") {
return this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.entity"

View File

@ -3,7 +3,7 @@ import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { MarkdownCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
@ -18,11 +18,11 @@ const cardConfigStruct = assign(
})
);
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{ name: "title", selector: { text: {} } },
{ name: "content", required: true, selector: { template: {} } },
{ name: "theme", selector: { theme: {} } },
];
] as const;
@customElement("hui-markdown-card-editor")
export class HuiMarkdownCardEditor
@ -58,22 +58,23 @@ export class HuiMarkdownCardEditor
fireEvent(this, "config-changed", { config: ev.detail.value });
}
private _computeLabelCallback = (schema: HaFormSchema) => {
if (schema.name === "theme") {
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
switch (schema.name) {
case "theme":
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
case "content":
return this.hass!.localize(
`ui.panel.lovelace.editor.card.markdown.${schema.name}`
);
default:
return this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
);
}
return (
this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
) ||
this.hass!.localize(
`ui.panel.lovelace.editor.card.markdown.${schema.name}`
)
);
};
}

View File

@ -2,7 +2,7 @@ import { CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, boolean, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { ActionConfig } from "../../../../data/lovelace";
import type { HomeAssistant } from "../../../../types";
import type { PictureEntityCardConfig } from "../../cards/types";
@ -30,7 +30,7 @@ const cardConfigStruct = assign(
})
);
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{ name: "entity", required: true, selector: { entity: {} } },
{ name: "name", selector: { text: {} } },
{ name: "image", selector: { text: {} } },
@ -61,7 +61,7 @@ const SCHEMA: HaFormSchema[] = [
],
},
{ name: "theme", selector: { theme: {} } },
];
] as const;
@customElement("hui-picture-entity-card-editor")
export class HuiPictureEntityCardEditor
@ -163,29 +163,19 @@ export class HuiPictureEntityCardEditor
fireEvent(this, "config-changed", { config: this._config });
}
private _computeLabelCallback = (schema: HaFormSchema) => {
if (schema.name === "entity") {
return this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.entity"
);
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
switch (schema.name) {
case "theme":
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
default:
return this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
);
}
if (schema.name === "theme") {
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
}
return (
this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
) ||
this.hass!.localize(
`ui.panel.lovelace.editor.card.picture-entity.${schema.name}`
)
);
};
static styles: CSSResultGroup = configElementStyle;

View File

@ -3,7 +3,7 @@ import { customElement, property, state } from "lit/decorators";
import { array, assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/ha-form/ha-form";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { ActionConfig } from "../../../../data/lovelace";
import type { HomeAssistant } from "../../../../types";
import type { PictureGlanceCardConfig } from "../../cards/types";
@ -36,7 +36,7 @@ const cardConfigStruct = assign(
const actions = ["more-info", "toggle", "navigate", "call-service", "none"];
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{ name: "title", selector: { text: {} } },
{ name: "image", selector: { text: {} } },
{ name: "camera_image", selector: { entity: { domain: "camera" } } },
@ -53,7 +53,7 @@ const SCHEMA: HaFormSchema[] = [
},
{ name: "entity", selector: { entity: {} } },
{ name: "theme", selector: { theme: {} } },
];
] as const;
@customElement("hui-picture-glance-card-editor")
export class HuiPictureGlanceCardEditor
@ -158,29 +158,23 @@ export class HuiPictureGlanceCardEditor
fireEvent(this, "config-changed", { config: this._config });
}
private _computeLabelCallback = (schema: HaFormSchema) => {
if (schema.name === "entity") {
return this.hass!.localize(
"ui.panel.lovelace.editor.card.picture-glance.state_entity"
);
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
switch (schema.name) {
case "theme":
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
case "entity":
return this.hass!.localize(
"ui.panel.lovelace.editor.card.picture-glance.state_entity"
);
default:
return this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
);
}
if (schema.name === "theme") {
return `${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.theme"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
}
return (
this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
) ||
this.hass!.localize(
`ui.panel.lovelace.editor.card.picture-glance.${schema.name}`
)
);
};
static styles: CSSResultGroup = configElementStyle;

View File

@ -3,7 +3,7 @@ import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { PlantStatusCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
@ -18,11 +18,11 @@ const cardConfigStruct = assign(
})
);
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{ name: "entity", required: true, selector: { entity: { domain: "plant" } } },
{ name: "name", selector: { text: {} } },
{ name: "theme", selector: { theme: {} } },
];
] as const;
@customElement("hui-plant-status-card-editor")
export class HuiPlantStatusCardEditor
@ -58,7 +58,7 @@ export class HuiPlantStatusCardEditor
fireEvent(this, "config-changed", { config: ev.detail.value });
}
private _computeLabelCallback = (schema: HaFormSchema) => {
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
if (schema.name === "entity") {
return this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.entity"

View File

@ -3,7 +3,7 @@ import { html, LitElement, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators";
import { assert, assign, object, optional, string } from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import type { HaFormSchema } from "../../../../components/ha-form/types";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types";
import type { ThermostatCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
@ -18,7 +18,7 @@ const cardConfigStruct = assign(
})
);
const SCHEMA: HaFormSchema[] = [
const SCHEMA = [
{ name: "entity", selector: { entity: { domain: "climate" } } },
{
type: "grid",
@ -28,7 +28,7 @@ const SCHEMA: HaFormSchema[] = [
{ name: "theme", required: false, selector: { theme: {} } },
],
},
];
] as const;
@customElement("hui-thermostat-card-editor")
export class HuiThermostatCardEditor
@ -64,7 +64,7 @@ export class HuiThermostatCardEditor
fireEvent(this, "config-changed", { config: ev.detail.value });
}
private _computeLabelCallback = (schema: HaFormSchema) => {
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
if (schema.name === "entity") {
return this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.entity"