Proper editor enum validation of timestamp formats (#9965)

This commit is contained in:
Philip Allgaier 2021-09-06 10:42:42 +02:00 committed by GitHub
parent d6dbbcb0de
commit b2a87c90a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 14 deletions

View File

@ -6,7 +6,7 @@ import { formatTime } from "../../../common/datetime/format_time";
import relativeTime from "../../../common/datetime/relative_time"; import relativeTime from "../../../common/datetime/relative_time";
import { FrontendLocaleData } from "../../../data/translation"; import { FrontendLocaleData } from "../../../data/translation";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { TimestampRenderingFormats } from "./types"; import { TimestampRenderingFormat } from "./types";
const FORMATS: { const FORMATS: {
[key: string]: (ts: Date, lang: FrontendLocaleData) => string; [key: string]: (ts: Date, lang: FrontendLocaleData) => string;
@ -23,7 +23,7 @@ class HuiTimestampDisplay extends LitElement {
@property() public ts?: Date; @property() public ts?: Date;
@property() public format?: TimestampRenderingFormats; @property() public format?: TimestampRenderingFormat;
@state() private _relative?: string; @state() private _relative?: string;

View File

@ -7,9 +7,13 @@ export interface ConditionalBaseConfig extends LovelaceCardConfig {
conditions: Condition[]; conditions: Condition[];
} }
export type TimestampRenderingFormats = export const TIMESTAMP_RENDERING_FORMATS = [
| "relative" "relative",
| "total" "total",
| "date" "date",
| "time" "time",
| "datetime"; "datetime",
] as const;
export type TimestampRenderingFormat =
typeof TIMESTAMP_RENDERING_FORMATS[number];

View File

@ -11,6 +11,7 @@ import {
assign, assign,
boolean, boolean,
dynamic, dynamic,
enums,
literal, literal,
number, number,
object, object,
@ -34,6 +35,7 @@ import "../../../../components/ha-switch";
import type { HomeAssistant } from "../../../../types"; import type { HomeAssistant } from "../../../../types";
import type { EntitiesCardConfig } from "../../cards/types"; import type { EntitiesCardConfig } from "../../cards/types";
import "../../components/hui-theme-select-editor"; import "../../components/hui-theme-select-editor";
import { TIMESTAMP_RENDERING_FORMATS } from "../../components/types";
import type { LovelaceRowConfig } from "../../entity-rows/types"; import type { LovelaceRowConfig } from "../../entity-rows/types";
import { headerFooterConfigStructs } from "../../header-footer/structs"; import { headerFooterConfigStructs } from "../../header-footer/structs";
import type { LovelaceCardEditor } from "../../types"; import type { LovelaceCardEditor } from "../../types";
@ -137,6 +139,7 @@ const attributeEntitiesRowConfigStruct = object({
suffix: optional(string()), suffix: optional(string()),
name: optional(string()), name: optional(string()),
icon: optional(string()), icon: optional(string()),
format: optional(enums(TIMESTAMP_RENDERING_FORMATS)),
}); });
const textEntitiesRowConfigStruct = object({ const textEntitiesRowConfigStruct = object({

View File

@ -1,4 +1,5 @@
import { union, object, string, optional, boolean } from "superstruct"; import { union, object, string, optional, boolean, enums } from "superstruct";
import { TIMESTAMP_RENDERING_FORMATS } from "../../components/types";
import { actionConfigStruct } from "./action-struct"; import { actionConfigStruct } from "./action-struct";
export const entitiesConfigStruct = union([ export const entitiesConfigStruct = union([
@ -8,7 +9,7 @@ export const entitiesConfigStruct = union([
icon: optional(string()), icon: optional(string()),
image: optional(string()), image: optional(string()),
secondary_info: optional(string()), secondary_info: optional(string()),
format: optional(string()), format: optional(enums(TIMESTAMP_RENDERING_FORMATS)),
state_color: optional(boolean()), state_color: optional(boolean()),
tap_action: optional(actionConfigStruct), tap_action: optional(actionConfigStruct),
hold_action: optional(actionConfigStruct), hold_action: optional(actionConfigStruct),

View File

@ -20,11 +20,11 @@ import { hasConfigOrEntityChanged } from "../common/has-changed";
import "../components/hui-generic-entity-row"; import "../components/hui-generic-entity-row";
import "../components/hui-timestamp-display"; import "../components/hui-timestamp-display";
import { createEntityNotFoundWarning } from "../components/hui-warning"; import { createEntityNotFoundWarning } from "../components/hui-warning";
import { TimestampRenderingFormats } from "../components/types"; import { TimestampRenderingFormat } from "../components/types";
import { LovelaceRow } from "./types"; import { LovelaceRow } from "./types";
interface SensorEntityConfig extends EntitiesCardEntityConfig { interface SensorEntityConfig extends EntitiesCardEntityConfig {
format?: TimestampRenderingFormats; format?: TimestampRenderingFormat;
} }
@customElement("hui-sensor-entity-row") @customElement("hui-sensor-entity-row")

View File

@ -1,7 +1,7 @@
import { ActionConfig } from "../../../data/lovelace"; import { ActionConfig } from "../../../data/lovelace";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { Condition } from "../common/validate-condition"; import { Condition } from "../common/validate-condition";
import { TimestampRenderingFormats } from "../components/types"; import { TimestampRenderingFormat } from "../components/types";
export interface EntityConfig { export interface EntityConfig {
entity: string; entity: string;
@ -92,5 +92,5 @@ export interface AttributeRowConfig extends EntityConfig {
attribute: string; attribute: string;
prefix?: string; prefix?: string;
suffix?: string; suffix?: string;
format?: TimestampRenderingFormats; format?: TimestampRenderingFormat;
} }