-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration to default dashboard (#18658)
Co-authored-by: Simon Lamon <[email protected]>
- Loading branch information
Showing
15 changed files
with
521 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...panels/lovelace/editor/dashboard-strategy-editor/hui-dashboard-strategy-element-editor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { customElement } from "lit/decorators"; | ||
import { LovelaceDashboardStrategyConfig } from "../../../../data/lovelace/config/types"; | ||
import { getLovelaceStrategy } from "../../strategies/get-strategy"; | ||
import { LovelaceStrategyEditor } from "../../strategies/types"; | ||
import { HuiElementEditor } from "../hui-element-editor"; | ||
|
||
@customElement("hui-dashboard-strategy-element-editor") | ||
export class HuiDashboardStrategyElementEditor extends HuiElementEditor<LovelaceDashboardStrategyConfig> { | ||
protected async getConfigElement(): Promise< | ||
LovelaceStrategyEditor | undefined | ||
> { | ||
const elClass = await getLovelaceStrategy( | ||
"dashboard", | ||
this.configElementType! | ||
); | ||
|
||
// Check if a GUI editor exists | ||
if (elClass && elClass.getConfigElement) { | ||
return elClass.getConfigElement(); | ||
} | ||
|
||
return undefined; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-dashboard-strategy-element-editor": HuiDashboardStrategyElementEditor; | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
...ovelace/editor/dashboard-strategy-editor/hui-original-states-dashboard-strategy-editor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { html, LitElement, nothing } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import memoizeOne from "memoize-one"; | ||
import { fireEvent } from "../../../../common/dom/fire_event"; | ||
import "../../../../components/ha-form/ha-form"; | ||
import type { | ||
HaFormSchema, | ||
SchemaUnion, | ||
} from "../../../../components/ha-form/types"; | ||
import type { HomeAssistant } from "../../../../types"; | ||
import { OriginalStatesDashboardStrategyConfig } from "../../strategies/original-states-dashboard-strategy"; | ||
import { LovelaceStrategyEditor } from "../../strategies/types"; | ||
|
||
const SCHEMA = [ | ||
{ | ||
name: "hidden_areas", | ||
selector: { | ||
area: { | ||
multiple: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "", | ||
type: "grid", | ||
schema: [ | ||
{ | ||
name: "hide_entities_without_area", | ||
selector: { | ||
boolean: {}, | ||
}, | ||
}, | ||
{ | ||
name: "hide_energy", | ||
selector: { | ||
boolean: {}, | ||
}, | ||
}, | ||
], | ||
}, | ||
] as const satisfies readonly HaFormSchema[]; | ||
|
||
type FormData = { | ||
hidden_areas: string[]; | ||
hide_energy?: boolean; | ||
hide_entities_without_area?: boolean; | ||
}; | ||
|
||
@customElement("hui-original-states-dashboard-strategy-editor") | ||
export class HuiOriginalStatesDashboarStrategyEditor | ||
extends LitElement | ||
implements LovelaceStrategyEditor | ||
{ | ||
@property({ attribute: false }) public hass?: HomeAssistant; | ||
|
||
@state() | ||
private _config?: OriginalStatesDashboardStrategyConfig; | ||
|
||
public setConfig(config: OriginalStatesDashboardStrategyConfig): void { | ||
this._config = config; | ||
} | ||
|
||
private _configToFormData = memoizeOne( | ||
(config: OriginalStatesDashboardStrategyConfig): FormData => { | ||
const { areas, ...rest } = config; | ||
return { | ||
...rest, | ||
hidden_areas: areas?.hidden || [], | ||
}; | ||
} | ||
); | ||
|
||
private _formDataToConfig = memoizeOne( | ||
(data: FormData): OriginalStatesDashboardStrategyConfig => { | ||
const { hidden_areas, ...rest } = data; | ||
const areas = | ||
hidden_areas.length > 0 | ||
? { | ||
hidden: hidden_areas, | ||
} | ||
: undefined; | ||
return { | ||
type: "original-states", | ||
...rest, | ||
areas, | ||
}; | ||
} | ||
); | ||
|
||
protected render() { | ||
if (!this.hass || !this._config) { | ||
return nothing; | ||
} | ||
|
||
const data = this._configToFormData(this._config); | ||
|
||
return html` | ||
<ha-form | ||
.hass=${this.hass} | ||
.data=${data} | ||
.schema=${SCHEMA} | ||
.computeLabel=${this._computeLabelCallback} | ||
@value-changed=${this._valueChanged} | ||
></ha-form> | ||
`; | ||
} | ||
|
||
private _valueChanged(ev: CustomEvent): void { | ||
const data = ev.detail.value as FormData; | ||
const config = this._formDataToConfig(data); | ||
fireEvent(this, "config-changed", { config }); | ||
} | ||
|
||
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => { | ||
switch (schema.name) { | ||
case "hidden_areas": | ||
case "hide_energy": | ||
case "hide_entities_without_area": | ||
return this.hass?.localize( | ||
`ui.panel.lovelace.editor.strategy.original-states.${schema.name}` | ||
); | ||
default: | ||
return ""; | ||
} | ||
}; | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-original-states-dashboard-strategy-editor": HuiOriginalStatesDashboarStrategyEditor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.