mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-25 13:57:21 +00:00
Better gauge segment coloring (#11570)
This commit is contained in:
parent
cff2f856b3
commit
e927091d21
@ -174,6 +174,26 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
|
|||||||
if (this._config!.needle) {
|
if (this._config!.needle) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// new format
|
||||||
|
let segments = this._config!.segments;
|
||||||
|
if (segments) {
|
||||||
|
segments = [...segments].sort((a, b) => a?.from - b?.from);
|
||||||
|
|
||||||
|
for (let i = 0; i < segments.length; i++) {
|
||||||
|
const segment = segments[i];
|
||||||
|
if (
|
||||||
|
segment &&
|
||||||
|
numberValue >= segment.from &&
|
||||||
|
(i + 1 === segments.length || numberValue < segments[i + 1]?.from)
|
||||||
|
) {
|
||||||
|
return segment.color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return severityMap.normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
// old format
|
||||||
const sections = this._config!.severity;
|
const sections = this._config!.severity;
|
||||||
|
|
||||||
if (!sections) {
|
if (!sections) {
|
||||||
@ -206,6 +226,16 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _severityLevels() {
|
private _severityLevels() {
|
||||||
|
// new format
|
||||||
|
const segments = this._config!.segments;
|
||||||
|
if (segments) {
|
||||||
|
return segments.map((segment) => ({
|
||||||
|
level: segment?.from,
|
||||||
|
stroke: segment?.color,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// old format
|
||||||
const sections = this._config!.severity;
|
const sections = this._config!.severity;
|
||||||
|
|
||||||
if (!sections) {
|
if (!sections) {
|
||||||
|
@ -176,6 +176,11 @@ export interface SeverityConfig {
|
|||||||
red?: number;
|
red?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GaugeSegment {
|
||||||
|
from: number;
|
||||||
|
color: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface GaugeCardConfig extends LovelaceCardConfig {
|
export interface GaugeCardConfig extends LovelaceCardConfig {
|
||||||
entity: string;
|
entity: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
@ -185,6 +190,7 @@ export interface GaugeCardConfig extends LovelaceCardConfig {
|
|||||||
severity?: SeverityConfig;
|
severity?: SeverityConfig;
|
||||||
theme?: string;
|
theme?: string;
|
||||||
needle?: boolean;
|
needle?: boolean;
|
||||||
|
segments?: GaugeSegment[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ConfigEntity extends EntityConfig {
|
export interface ConfigEntity extends EntityConfig {
|
||||||
|
@ -2,6 +2,7 @@ import "../../../../components/ha-form/ha-form";
|
|||||||
import { html, LitElement, TemplateResult } from "lit";
|
import { html, LitElement, TemplateResult } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import {
|
import {
|
||||||
|
array,
|
||||||
assert,
|
assert,
|
||||||
assign,
|
assign,
|
||||||
boolean,
|
boolean,
|
||||||
@ -18,6 +19,11 @@ import type { GaugeCardConfig } from "../../cards/types";
|
|||||||
import type { LovelaceCardEditor } from "../../types";
|
import type { LovelaceCardEditor } from "../../types";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
|
|
||||||
|
const gaugeSegmentStruct = object({
|
||||||
|
from: number(),
|
||||||
|
color: string(),
|
||||||
|
});
|
||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
baseLovelaceCardConfig,
|
||||||
object({
|
object({
|
||||||
@ -29,6 +35,7 @@ const cardConfigStruct = assign(
|
|||||||
severity: optional(object()),
|
severity: optional(object()),
|
||||||
theme: optional(string()),
|
theme: optional(string()),
|
||||||
needle: optional(boolean()),
|
needle: optional(boolean()),
|
||||||
|
segments: optional(array(gaugeSegmentStruct)),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user