Skip to content

Commit

Permalink
Add better localize keys typings for config pages (home-assistant#17815)
Browse files Browse the repository at this point in the history
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
  • Loading branch information
piitaya and bramkragten committed Sep 5, 2023
1 parent d4872b1 commit f7f1a0c
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 877 deletions.
7 changes: 0 additions & 7 deletions src/common/translations/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ export type LocalizeKeys =
| `ui.dialogs.unhealthy.reason.${string}`
| `ui.dialogs.unsupported.reason.${string}`
| `ui.panel.config.${string}.${"caption" | "description"}`
| `ui.panel.config.automation.${string}`
| `ui.panel.config.dashboard.${string}`
| `ui.panel.config.devices.${string}`
| `ui.panel.config.energy.${string}`
| `ui.panel.config.info.${string}`
| `ui.panel.config.lovelace.${string}`
| `ui.panel.config.network.${string}`
| `ui.panel.config.scene.${string}`
| `ui.panel.config.zha.${string}`
| `ui.panel.config.zwave_js.${string}`
| `ui.panel.lovelace.card.${string}`
Expand Down
4 changes: 3 additions & 1 deletion src/data/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,13 @@ export interface ZoneCondition extends BaseCondition {
zone: string;
}

type Weekday = "sun" | "mon" | "tue" | "wed" | "thu" | "fri" | "sat";

export interface TimeCondition extends BaseCondition {
condition: "time";
after?: string;
before?: string;
weekday?: string | string[];
weekday?: Weekday | Weekday[];
}

export interface TemplateCondition extends BaseCondition {
Expand Down
10 changes: 0 additions & 10 deletions src/data/cloud.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { EntityFilter } from "../common/entity/entity_filter";
import { PlaceholderContainer } from "../panels/config/automation/thingtalk/dialog-thingtalk";
import { HomeAssistant } from "../types";
import { AutomationConfig } from "./automation";

interface CloudStatusNotLoggedIn {
logged_in: false;
Expand Down Expand Up @@ -66,11 +64,6 @@ export interface CloudWebhook {
managed?: boolean;
}

export interface ThingTalkConversion {
config: Partial<AutomationConfig>;
placeholders: PlaceholderContainer;
}

export const cloudLogin = (
hass: HomeAssistant,
email: string,
Expand Down Expand Up @@ -136,9 +129,6 @@ export const disconnectCloudRemote = (hass: HomeAssistant) =>
export const fetchCloudSubscriptionInfo = (hass: HomeAssistant) =>
hass.callWS<SubscriptionInfo>({ type: "cloud/subscription" });

export const convertThingTalk = (hass: HomeAssistant, query: string) =>
hass.callWS<ThingTalkConversion>({ type: "cloud/thingtalk/convert", query });

export const updateCloudPref = (
hass: HomeAssistant,
prefs: {
Expand Down
12 changes: 7 additions & 5 deletions src/panels/config/automation/action/ha-automation-action-row.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { consume } from "@lit-labs/context";
import { ActionDetail } from "@material/mwc-list/mwc-list-foundation";
import "@material/mwc-list/mwc-list-item";
import {
Expand Down Expand Up @@ -25,7 +26,6 @@ import {
} from "lit";
import { customElement, property, query, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map";
import { consume } from "@lit-labs/context";
import { storage } from "../../../../common/decorators/storage";
import { dynamicElement } from "../../../../common/dom/dynamic-element-directive";
import { fireEvent } from "../../../../common/dom/fire_event";
Expand All @@ -40,6 +40,7 @@ import type { HaYamlEditor } from "../../../../components/ha-yaml-editor";
import { ACTION_TYPES, YAML_ONLY_ACTION_TYPES } from "../../../../data/action";
import { AutomationClipboard } from "../../../../data/automation";
import { validateConfig } from "../../../../data/config";
import { fullEntitiesContext } from "../../../../data/context";
import { EntityRegistryEntry } from "../../../../data/entity_registry";
import {
Action,
Expand Down Expand Up @@ -70,19 +71,20 @@ import "./types/ha-automation-action-service";
import "./types/ha-automation-action-stop";
import "./types/ha-automation-action-wait_for_trigger";
import "./types/ha-automation-action-wait_template";
import { fullEntitiesContext } from "../../../../data/context";

export const getType = (action: Action | undefined) => {
if (!action) {
return undefined;
}
if ("service" in action || "scene" in action) {
return getActionType(action);
return getActionType(action) as "activate_scene" | "service" | "play_media";
}
if (["and", "or", "not"].some((key) => key in action)) {
return "condition";
return "condition" as const;
}
return Object.keys(ACTION_TYPES).find((option) => option in action);
return Object.keys(ACTION_TYPES).find(
(option) => option in action
) as keyof typeof ACTION_TYPES;
};

export interface ActionElement extends LitElement {
Expand Down
28 changes: 14 additions & 14 deletions src/panels/config/automation/action/ha-automation-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,42 @@ import type { ActionDetail } from "@material/mwc-list";
import {
mdiArrowDown,
mdiArrowUp,
mdiContentPaste,
mdiDrag,
mdiPlus,
mdiContentPaste,
} from "@mdi/js";
import deepClone from "deep-clone-simple";
import {
css,
CSSResultGroup,
html,
LitElement,
nothing,
PropertyValues,
css,
html,
nothing,
} from "lit";
import { customElement, property } from "lit/decorators";
import { repeat } from "lit/directives/repeat";
import memoizeOne from "memoize-one";
import type { SortableEvent } from "sortablejs";
import { storage } from "../../../../common/decorators/storage";
import { fireEvent } from "../../../../common/dom/fire_event";
import { stringCompare } from "../../../../common/string/compare";
import { LocalizeFunc } from "../../../../common/translations/localize";
import "../../../../components/ha-button-menu";
import "../../../../components/ha-button";
import "../../../../components/ha-button-menu";
import type { HaSelect } from "../../../../components/ha-select";
import "../../../../components/ha-svg-icon";
import { ACTION_TYPES } from "../../../../data/action";
import { Action } from "../../../../data/script";
import { AutomationClipboard } from "../../../../data/automation";
import { Action } from "../../../../data/script";
import { sortableStyles } from "../../../../resources/ha-sortable-style";
import {
loadSortable,
SortableInstance,
loadSortable,
} from "../../../../resources/sortable.ondemand";
import { HomeAssistant } from "../../../../types";
import { getType } from "./ha-automation-action-row";
import { Entries, HomeAssistant } from "../../../../types";
import type HaAutomationActionRow from "./ha-automation-action-row";
import { getType } from "./ha-automation-action-row";
import "./types/ha-automation-action-activate_scene";
import "./types/ha-automation-action-choose";
import "./types/ha-automation-action-condition";
Expand All @@ -52,7 +53,6 @@ import "./types/ha-automation-action-service";
import "./types/ha-automation-action-stop";
import "./types/ha-automation-action-wait_for_trigger";
import "./types/ha-automation-action-wait_template";
import { storage } from "../../../../common/decorators/storage";

const PASTE_VALUE = "__paste__";

Expand Down Expand Up @@ -174,9 +174,9 @@ export default class HaAutomationAction extends LitElement {
"ui.panel.config.automation.editor.actions.paste"
)}
(${this.hass.localize(
`ui.panel.config.automation.editor.actions.type.${getType(
this._clipboard.action
)}.label`
`ui.panel.config.automation.editor.actions.type.${
getType(this._clipboard.action) || "unknown"
}.label`
)})
<ha-svg-icon slot="graphic" .path=${mdiContentPaste}></ha-svg-icon
></mwc-list-item>`
Expand Down Expand Up @@ -333,7 +333,7 @@ export default class HaAutomationAction extends LitElement {

private _processedTypes = memoizeOne(
(localize: LocalizeFunc): [string, string, string][] =>
Object.entries(ACTION_TYPES)
(Object.entries(ACTION_TYPES) as Entries<typeof ACTION_TYPES>)
.map(
([action, icon]) =>
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "../../../../../components/ha-select";
import type { HaSelect } from "../../../../../components/ha-select";
import type { Condition } from "../../../../../data/automation";
import { CONDITION_TYPES } from "../../../../../data/condition";
import { HomeAssistant } from "../../../../../types";
import { Entries, HomeAssistant } from "../../../../../types";
import "../../condition/ha-automation-condition-editor";
import type { ActionElement } from "../ha-automation-action-row";

Expand Down Expand Up @@ -55,7 +55,7 @@ export class HaConditionAction extends LitElement implements ActionElement {

private _processedTypes = memoizeOne(
(localize: LocalizeFunc): [string, string, string][] =>
Object.entries(CONDITION_TYPES)
(Object.entries(CONDITION_TYPES) as Entries<typeof CONDITION_TYPES>)
.map(
([condition, icon]) =>
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import type {
AutomationClipboard,
Condition,
} from "../../../../data/automation";
import type { HomeAssistant } from "../../../../types";
import type { Entries, HomeAssistant } from "../../../../types";
import "./ha-automation-condition-row";
import type HaAutomationConditionRow from "./ha-automation-condition-row";
// Uncommenting these and this element doesn't load
// import "./types/ha-automation-condition-not";
// import "./types/ha-automation-condition-or";
import { storage } from "../../../../common/decorators/storage";
import { stringCompare } from "../../../../common/string/compare";
import type { LocalizeFunc } from "../../../../common/translations/localize";
import type { HaSelect } from "../../../../components/ha-select";
Expand All @@ -52,7 +53,6 @@ import "./types/ha-automation-condition-template";
import "./types/ha-automation-condition-time";
import "./types/ha-automation-condition-trigger";
import "./types/ha-automation-condition-zone";
import { storage } from "../../../../common/decorators/storage";

const PASTE_VALUE = "__paste__";

Expand Down Expand Up @@ -364,7 +364,7 @@ export default class HaAutomationCondition extends LitElement {

private _processedTypes = memoizeOne(
(localize: LocalizeFunc): [string, string, string][] =>
Object.entries(CONDITION_TYPES)
(Object.entries(CONDITION_TYPES) as Entries<typeof CONDITION_TYPES>)
.map(
([condition, icon]) =>
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ export class HaZoneCondition extends LitElement {
allow-custom-entity
.includeDomains=${includeDomains}
></ha-entity-picker>
<label id="eventlabel">
${this.hass.localize(
"ui.panel.config.automation.editor.conditions.type.zone.event"
)}
</label>
`;
}

Expand Down
10 changes: 6 additions & 4 deletions src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import {
showAutomationEditor,
triggerAutomationActions,
} from "../../../data/automation";
import { validateConfig } from "../../../data/config";
import { UNAVAILABLE } from "../../../data/entity";
import { fetchEntityRegistry } from "../../../data/entity_registry";
import {
showAlertDialog,
Expand All @@ -57,15 +59,13 @@ import {
import "../../../layouts/hass-subpage";
import { KeyboardShortcutMixin } from "../../../mixins/keyboard-shortcut-mixin";
import { haStyle } from "../../../resources/styles";
import { HomeAssistant, Route } from "../../../types";
import { Entries, HomeAssistant, Route } from "../../../types";
import { showToast } from "../../../util/toast";
import "../ha-config-section";
import { showAutomationModeDialog } from "./automation-mode-dialog/show-dialog-automation-mode";
import { showAutomationRenameDialog } from "./automation-rename-dialog/show-dialog-automation-rename";
import "./blueprint-automation-editor";
import "./manual-automation-editor";
import { UNAVAILABLE } from "../../../data/entity";
import { validateConfig } from "../../../data/config";

declare global {
interface HTMLElementTagNameMap {
Expand Down Expand Up @@ -489,7 +489,9 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
condition: this._config.condition,
action: this._config.action,
});
this._validationErrors = Object.entries(validation).map(([key, value]) =>
this._validationErrors = (
Object.entries(validation) as Entries<typeof validation>
).map(([key, value]) =>
value.valid
? ""
: html`${this.hass.localize(
Expand Down
Loading

0 comments on commit f7f1a0c

Please sign in to comment.