Skip to content
Merged
93 changes: 5 additions & 88 deletions src/data/lovelace.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { HomeAssistant } from "../types";

export interface LovelaceConfig {
_frontendAuto: boolean;
title?: string;
views: LovelaceViewConfig[];
}

export interface LovelaceViewConfig {
index?: number;
title?: string;
badges?: string[];
cards?: LovelaceCardConfig[];
id?: string;
path?: string;
icon?: string;
theme?: string;
}

export interface LovelaceCardConfig {
id?: string;
index?: number;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't support stacks editing, however, since it's frontend only, we can change it later.

Copy link
Copy Markdown
Member Author

@bramkragten bramkragten Dec 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should have a parent_index for stacks or something

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we just have an array with indices.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would be even better, then it could work with endless stacks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const [viewIndex, ...cardIndices] = obj_path

view_index?: number;
type: string;
[key: string]: any;
}
Expand Down Expand Up @@ -60,95 +61,11 @@ export const fetchConfig = (
force,
});

export const migrateConfig = (hass: HomeAssistant): Promise<void> =>
hass.callWS({
type: "lovelace/config/migrate",
});

export const saveConfig = (
hass: HomeAssistant,
config: LovelaceConfig | string,
format: "json" | "yaml"
config: LovelaceConfig
): Promise<void> =>
hass.callWS({
type: "lovelace/config/save",
config,
format,
});

export const getCardConfig = (
hass: HomeAssistant,
cardId: string
): Promise<string> =>
hass.callWS({
type: "lovelace/config/card/get",
card_id: cardId,
});

export const updateCardConfig = (
hass: HomeAssistant,
cardId: string,
config: LovelaceCardConfig | string,
format: "json" | "yaml"
): Promise<void> =>
hass.callWS({
type: "lovelace/config/card/update",
card_id: cardId,
card_config: config,
format,
});

export const deleteCard = (
hass: HomeAssistant,
cardId: string
): Promise<void> =>
hass.callWS({
type: "lovelace/config/card/delete",
card_id: cardId,
});

export const addCard = (
hass: HomeAssistant,
viewId: string,
config: LovelaceCardConfig | string,
format: "json" | "yaml"
): Promise<void> =>
hass.callWS({
type: "lovelace/config/card/add",
view_id: viewId,
card_config: config,
format,
});

export const updateViewConfig = (
hass: HomeAssistant,
viewId: string,
config: LovelaceViewConfig | string,
format: "json" | "yaml"
): Promise<void> =>
hass.callWS({
type: "lovelace/config/view/update",
view_id: viewId,
view_config: config,
format,
});

export const deleteView = (
hass: HomeAssistant,
viewId: string
): Promise<void> =>
hass.callWS({
type: "lovelace/config/view/delete",
view_id: viewId,
});

export const addView = (
hass: HomeAssistant,
config: LovelaceViewConfig | string,
format: "json" | "yaml"
): Promise<void> =>
hass.callWS({
type: "lovelace/config/view/add",
view_config: config,
format,
});
18 changes: 15 additions & 3 deletions src/panels/lovelace/cards/hui-light-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,14 @@ export class HuiLightCard extends hassLocalizeLitMixin(LitElement)
this._roundSliderStyle = loaded.roundSliderStyle;
this._jQuery = loaded.jQuery;

const brightness = this.hass!.states[this._config!.entity].attributes
.brightness;
const stateObj = this.hass!.states[this._config!.entity] as LightEntity;

if (!stateObj) {
return;
}

const brightness = stateObj.attributes.brightness || 0;

this._jQuery("#light", this.shadowRoot).roundSlider({
...lightConfig,
change: (value) => this._setBrightness(value),
Expand All @@ -152,7 +158,13 @@ export class HuiLightCard extends hassLocalizeLitMixin(LitElement)
return;
}

const attrs = this.hass!.states[this._config!.entity].attributes;
const stateObj = this.hass!.states[this._config!.entity];

if (!stateObj) {
return;
}

const attrs = stateObj.attributes;

this._jQuery("#light", this.shadowRoot).roundSlider({
value: Math.round((attrs.brightness / 254) * 100) || 0,
Expand Down
5 changes: 2 additions & 3 deletions src/panels/lovelace/common/generate-lovelace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const computeDefaultViewStates = (hass: HomeAssistant): HassEntities => {

const generateViewConfig = (
localize: LocalizeFunc,
id: string,
path: string,
title: string | undefined,
icon: string | undefined,
entities: HassEntities,
Expand Down Expand Up @@ -158,7 +158,7 @@ const generateViewConfig = (
});

return {
id,
path,
title,
icon,
badges,
Expand Down Expand Up @@ -228,7 +228,6 @@ export const generateLovelaceConfig = (
}

return {
_frontendAuto: true,
title,
views,
};
Expand Down
39 changes: 8 additions & 31 deletions src/panels/lovelace/components/hui-card-options.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
import "@polymer/paper-button/paper-button";
import "@polymer/paper-icon-button/paper-icon-button";
import { fireEvent } from "../../../common/dom/fire_event";
import { showEditCardDialog } from "../editor/show-edit-card-dialog";
import { showEditCardDialog } from "../editor/card-editor/show-edit-card-dialog";

import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
import { confDeleteCard } from "../editor/delete-card";
import { HomeAssistant } from "../../../types";
import { LovelaceCardConfig } from "../../../data/lovelace";

declare global {
// for fire event
interface HASSDomEvents {
"show-edit-card": {
cardConfig?: LovelaceCardConfig;
viewId?: string | number;
add: boolean;
reloadLovelace: () => void;
};
}
}
import { Lovelace } from "../types";

export class HuiCardOptions extends hassLocalizeLitMixin(LitElement) {
public cardConfig?: LovelaceCardConfig;
protected hass?: HomeAssistant;
protected lovelace?: Lovelace;
protected path?: [number, number];

static get properties(): PropertyDeclarations {
return { hass: {} };
return { hass: {}, lovelace: {}, path: {} };
}

protected render() {
Expand Down Expand Up @@ -66,26 +56,13 @@ export class HuiCardOptions extends hassLocalizeLitMixin(LitElement) {
`;
}
private _editCard(): void {
if (!this.cardConfig) {
return;
}
showEditCardDialog(this, {
cardConfig: this.cardConfig,
add: false,
reloadLovelace: () => fireEvent(this, "config-refresh"),
lovelace: this.lovelace!,
path: this.path!,
});
}
private _deleteCard(): void {
if (!this.cardConfig) {
return;
}
if (!this.cardConfig.id) {
this._editCard();
return;
}
confDeleteCard(this.hass!, this.cardConfig.id, () =>
fireEvent(this, "config-refresh")
);
confDeleteCard(this.lovelace!, this.path!);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@ import { html, LitElement } from "@polymer/lit-element";
import { TemplateResult } from "lit-html";
import "@polymer/paper-button/paper-button";

import { HomeAssistant } from "../../../types";
import { fireEvent } from "../../../common/dom/fire_event";
import { LovelaceCardConfig } from "../../../data/lovelace";
import { getCardElementTag } from "../common/get-card-element-tag";
import { CardPickTarget } from "./types";
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";

import { uid } from "../../../common/util/uid";

declare global {
interface HASSDomEvents {
"card-picked": {
config: LovelaceCardConfig;
};
}
}
import { HomeAssistant } from "../../../../types";
import { LovelaceCardConfig } from "../../../../data/lovelace";
import { getCardElementTag } from "../../common/get-card-element-tag";
import { CardPickTarget } from "../types";
import { hassLocalizeLitMixin } from "../../../../mixins/lit-localize-mixin";

const cards = [
{ name: "Alarm panel", type: "alarm-panel" },
Expand Down Expand Up @@ -47,7 +36,8 @@ const cards = [
];

export class HuiCardPicker extends hassLocalizeLitMixin(LitElement) {
protected hass?: HomeAssistant;
public hass?: HomeAssistant;
public cardPicked?: (cardConf: LovelaceCardConfig) => void;

protected render(): TemplateResult {
return html`
Expand Down Expand Up @@ -90,16 +80,14 @@ export class HuiCardPicker extends hassLocalizeLitMixin(LitElement) {
const tag = getCardElementTag(type);

const elClass = customElements.get(tag);
let config: LovelaceCardConfig = { type, id: uid() };
let config: LovelaceCardConfig = { type };

if (elClass && elClass.getStubConfig) {
const cardConfig = elClass.getStubConfig(this.hass);
config = { ...config, ...cardConfig };
}

fireEvent(this, "card-picked", {
config,
});
this.cardPicked!(config);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "@polymer/paper-input/paper-textarea";

import createCardElement from "../common/create-card-element";
import createErrorCardConfig from "../common/create-error-card-config";
import { HomeAssistant } from "../../../types";
import { LovelaceCardConfig } from "../../../data/lovelace";
import { LovelaceCard } from "../types";
import { ConfigError } from "./types";
import { getCardElementTag } from "../common/get-card-element-tag";
import createCardElement from "../../common/create-card-element";
import createErrorCardConfig from "../../common/create-error-card-config";
import { HomeAssistant } from "../../../../types";
import { LovelaceCardConfig } from "../../../../data/lovelace";
import { LovelaceCard } from "../../types";
import { ConfigError } from "../types";
import { getCardElementTag } from "../../common/get-card-element-tag";

export class HuiCardPreview extends HTMLElement {
private _hass?: HomeAssistant;
Expand Down
Loading