Add default placeholders for various number fields in cards (#15903)

This commit is contained in:
karwosts 2023-03-23 07:15:38 -07:00 committed by GitHub
parent ca6d1544d1
commit 4e6ed61e2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 70 additions and 19 deletions

View File

@ -24,6 +24,9 @@ import { createEntityNotFoundWarning } from "../components/hui-warning";
import type { LovelaceCard, LovelaceCardEditor } from "../types";
import type { GaugeCardConfig } from "./types";
export const DEFAULT_MIN = 0;
export const DEFAULT_MAX = 100;
export const severityMap = {
red: "var(--error-color)",
green: "var(--success-color)",
@ -76,7 +79,7 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
throw new Error("Invalid entity");
}
this._config = { min: 0, max: 100, ...config };
this._config = { min: DEFAULT_MIN, max: DEFAULT_MAX, ...config };
}
protected render() {

View File

@ -4,7 +4,7 @@ import { LovelaceCardEditor } from "../types";
import { HuiStackCard } from "./hui-stack-card";
import { GridCardConfig } from "./types";
const DEFAULT_COLUMNS = 3;
export const DEFAULT_COLUMNS = 3;
const SQUARE_ROW_HEIGHTS_BY_COLUMNS = {
1: 5,
2: 3,

View File

@ -22,6 +22,8 @@ import { processConfigEntities } from "../common/process-config-entities";
import { LovelaceCard } from "../types";
import { HistoryGraphCardConfig } from "./types";
export const DEFAULT_HOURS_TO_SHOW = 24;
@customElement("hui-history-graph-card")
export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
public static async getConfigElement() {
@ -46,7 +48,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
private _entityIds: string[] = [];
private _hoursToShow = 24;
private _hoursToShow = DEFAULT_HOURS_TO_SHOW;
private _interval?: number;
@ -77,7 +79,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
}
});
this._hoursToShow = config.hours_to_show || 24;
this._hoursToShow = config.hours_to_show || DEFAULT_HOURS_TO_SHOW;
this._config = config;
}

View File

@ -21,6 +21,8 @@ import type { EntityConfig } from "../entity-rows/types";
import type { LovelaceCard, LovelaceCardEditor } from "../types";
import type { LogbookCardConfig } from "./types";
export const DEFAULT_HOURS_TO_SHOW = 24;
@customElement("hui-logbook-card")
export class HuiLogbookCard extends LitElement implements LovelaceCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
@ -66,7 +68,7 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard {
}
this._config = {
hours_to_show: 24,
hours_to_show: DEFAULT_HOURS_TO_SHOW,
...config,
};
this._time = {

View File

@ -41,7 +41,9 @@ import { EntityConfig } from "../entity-rows/types";
import { LovelaceCard } from "../types";
import { MapCardConfig } from "./types";
const DEFAULT_HOURS_TO_SHOW = 0;
export const DEFAULT_HOURS_TO_SHOW = 0;
export const DEFAULT_ZOOM = 14;
@customElement("hui-map-card")
class HuiMapCard extends LitElement implements LovelaceCard {
@property({ attribute: false }) public hass!: HomeAssistant;
@ -149,7 +151,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
this._config,
this._configEntities
)}
.zoom=${this._config.default_zoom ?? 14}
.zoom=${this._config.default_zoom ?? DEFAULT_ZOOM}
.paths=${this._getHistoryPaths(this._config, this._stateHistory)}
.autoFit=${this._config.auto_fit}
.darkMode=${this._config.dark_mode}

View File

@ -10,6 +10,8 @@ import { EntityCardConfig, SensorCardConfig } from "./types";
const includeDomains = ["counter", "input_number", "number", "sensor"];
export const DEFAULT_HOURS_TO_SHOW = 24;
@customElement("hui-sensor-card")
class HuiSensorCard extends HuiEntityCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
@ -59,7 +61,7 @@ class HuiSensorCard extends HuiEntityCard {
type: "graph",
entity: config.entity,
detail: detail || 1,
hours_to_show: hours_to_show || 24,
hours_to_show: hours_to_show || DEFAULT_HOURS_TO_SHOW,
limits: config.limits!,
};

View File

@ -28,6 +28,8 @@ import { processConfigEntities } from "../common/process-config-entities";
import { LovelaceCard } from "../types";
import { StatisticsGraphCardConfig } from "./types";
export const DEFAULT_DAYS_TO_SHOW = 30;
@customElement("hui-statistics-graph-card")
export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
public static async getConfigElement() {
@ -225,7 +227,10 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
const startDate = new Date();
startDate.setTime(
startDate.getTime() -
1000 * 60 * 60 * (24 * (this._config!.days_to_show || 30) + 1)
1000 *
60 *
60 *
(24 * (this._config!.days_to_show || DEFAULT_DAYS_TO_SHOW) + 1)
);
try {
let unitClass;

View File

@ -19,6 +19,7 @@ import type { HomeAssistant } from "../../../../types";
import type { GaugeCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { DEFAULT_MIN, DEFAULT_MAX } from "../../cards/hui-gauge-card";
const gaugeSegmentStruct = object({
from: number(),
@ -79,8 +80,16 @@ export class HuiGaugeCardEditor
name: "",
type: "grid",
schema: [
{ name: "min", selector: { number: { mode: "box" } } },
{ name: "max", selector: { number: { mode: "box" } } },
{
name: "min",
default: DEFAULT_MIN,
selector: { number: { mode: "box" } },
},
{
name: "max",
default: DEFAULT_MAX,
selector: { number: { mode: "box" } },
},
],
},
{

View File

@ -16,6 +16,7 @@ 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";
import { DEFAULT_COLUMNS } from "../../cards/hui-grid-card";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
@ -32,7 +33,11 @@ const SCHEMA = [
type: "grid",
name: "",
schema: [
{ name: "columns", selector: { number: { min: 1, mode: "box" } } },
{
name: "columns",
default: DEFAULT_COLUMNS,
selector: { number: { min: 1, mode: "box" } },
},
{ name: "square", selector: { boolean: {} } },
],
},
@ -50,7 +55,7 @@ export class HuiGridCardEditor extends HuiStackCardEditor {
return nothing;
}
const data = { square: true, columns: 3, ...this._config };
const data = { square: true, ...this._config };
return html`
<ha-form

View File

@ -21,6 +21,7 @@ import type { LovelaceCardEditor } from "../../types";
import { processEditorEntities } from "../process-editor-entities";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { entitiesConfigStruct } from "../structs/entities-struct";
import { DEFAULT_HOURS_TO_SHOW } from "../../cards/hui-history-graph-card";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
@ -39,7 +40,11 @@ const SCHEMA = [
name: "",
type: "grid",
schema: [
{ name: "hours_to_show", selector: { number: { min: 1, mode: "box" } } },
{
name: "hours_to_show",
default: DEFAULT_HOURS_TO_SHOW,
selector: { number: { min: 1, mode: "box" } },
},
],
},
] as const;

View File

@ -18,6 +18,7 @@ import type { HomeAssistant } from "../../../../types";
import type { LogbookCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { DEFAULT_HOURS_TO_SHOW } from "../../cards/hui-logbook-card";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
@ -36,7 +37,11 @@ const SCHEMA = [
type: "grid",
schema: [
{ name: "theme", selector: { theme: {} } },
{ name: "hours_to_show", selector: { number: { mode: "box", min: 1 } } },
{
name: "hours_to_show",
default: DEFAULT_HOURS_TO_SHOW,
selector: { number: { mode: "box", min: 1 } },
},
],
},
] as const;

View File

@ -28,6 +28,7 @@ import { entitiesConfigStruct } from "../structs/entities-struct";
import { EntitiesEditorEvent } from "../types";
import { configElementStyle } from "./config-elements-style";
import { hasLocation } from "../../../../common/entity/has_location";
import { DEFAULT_HOURS_TO_SHOW, DEFAULT_ZOOM } from "../../cards/hui-map-card";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
@ -50,9 +51,17 @@ const SCHEMA = [
type: "grid",
schema: [
{ name: "aspect_ratio", selector: { text: {} } },
{ name: "default_zoom", selector: { number: { mode: "box", min: 0 } } },
{
name: "default_zoom",
default: DEFAULT_ZOOM,
selector: { number: { mode: "box", min: 0 } },
},
{ name: "dark_mode", selector: { boolean: {} } },
{ name: "hours_to_show", selector: { number: { mode: "box", min: 1 } } },
{
name: "hours_to_show",
default: DEFAULT_HOURS_TO_SHOW,
selector: { number: { mode: "box", min: 0 } },
},
],
},
] as const;

View File

@ -19,6 +19,7 @@ import type { SensorCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { configElementStyle } from "./config-elements-style";
import { DEFAULT_HOURS_TO_SHOW } from "../../cards/hui-sensor-card";
const cardConfigStruct = assign(
baseLovelaceCardConfig,
@ -77,6 +78,7 @@ const SCHEMA = [
{ name: "theme", selector: { theme: {} } },
{
name: "hours_to_show",
default: DEFAULT_HOURS_TO_SHOW,
selector: { number: { min: 1, mode: "box" } },
},
],
@ -103,7 +105,6 @@ export class HuiSensorCardEditor
}
const data = {
hours_to_show: 24,
graph: "none",
...this._config,
detail: this._config!.detail === 2,

View File

@ -45,6 +45,7 @@ import { processConfigEntities } from "../../common/process-config-entities";
import type { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { entitiesConfigStruct } from "../structs/entities-struct";
import { DEFAULT_DAYS_TO_SHOW } from "../../cards/hui-statistics-graph-card";
const statTypeStruct = union([
literal("state"),
@ -172,7 +173,7 @@ export class HuiStatisticsGraphCardEditor
},
{
name: "days_to_show",
default: 30,
default: DEFAULT_DAYS_TO_SHOW,
selector: { number: { min: 1, mode: "box" } },
},
{