Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automation editor tweaks #6713

Merged
merged 10 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions src/components/entity/ha-entity-attribute-picker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import "@polymer/paper-input/paper-input";
import "@polymer/paper-item/paper-item";
import "@vaadin/vaadin-combo-box/theme/material/vaadin-combo-box-light";
import { HassEntity } from "home-assistant-js-websocket";
import {
css,
CSSResult,
customElement,
html,
LitElement,
property,
PropertyValues,
query,
TemplateResult,
} from "lit-element";
import { fireEvent } from "../../common/dom/fire_event";
import { PolymerChangedEvent } from "../../polymer-types";
import { HomeAssistant } from "../../types";
import "../ha-icon-button";
import "./state-badge";

export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;

const rowRenderer = (root: HTMLElement, _owner, model: { item: string }) => {
if (!root.firstElementChild) {
root.innerHTML = `
<style>
paper-item {
margin: -10px;
padding: 0;
}
</style>
<paper-item></paper-item>
`;
}
root.querySelector("paper-item")!.textContent = model.item;
};

@customElement("ha-entity-attribute-picker")
class HaEntityAttributePicker extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property() public entityId?: string;

@property({ type: Boolean }) public autofocus = false;

@property({ type: Boolean }) public disabled = false;

@property({ type: Boolean, attribute: "allow-custom-value" })
public allowCustomValue;

@property() public label?: string;

@property() public value?: string;

@property({ type: Boolean }) private _opened = false;

@query("vaadin-combo-box-light") private _comboBox!: HTMLElement;

protected shouldUpdate(changedProps: PropertyValues) {
return !(!changedProps.has("_opened") && this._opened);
}

protected updated(changedProps: PropertyValues) {
if (changedProps.has("_opened") && this._opened) {
const state = this.entityId ? this.hass.states[this.entityId] : undefined;
(this._comboBox as any).items = state
? Object.keys(state.attributes)
: [];
}
}

protected render(): TemplateResult {
if (!this.hass) {
return html``;
}

return html`
<vaadin-combo-box-light
.value=${this._value}
.allowCustomValue=${this.allowCustomValue}
.renderer=${rowRenderer}
@opened-changed=${this._openedChanged}
@value-changed=${this._valueChanged}
>
<paper-input
.autofocus=${this.autofocus}
.label=${this.label ??
Copy link
Member

Choose a reason for hiding this comment

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

Did not know ?? existed, nice one!

this.hass.localize(
"ui.components.entity.entity-attribute-picker.attribute"
)}
.value=${this._value}
.disabled=${this.disabled || !this.entityId}
class="input"
autocapitalize="none"
autocomplete="off"
autocorrect="off"
spellcheck="false"
>
${this.value
? html`
<ha-icon-button
aria-label=${this.hass.localize(
"ui.components.entity.entity-picker.clear"
)}
slot="suffix"
class="clear-button"
icon="hass:close"
@click=${this._clearValue}
no-ripple
>
Clear
</ha-icon-button>
`
: ""}

<ha-icon-button
aria-label=${this.hass.localize(
"ui.components.entity.entity-attribute-picker.show_attributes"
)}
slot="suffix"
class="toggle-button"
.icon=${this._opened ? "hass:menu-up" : "hass:menu-down"}
>
Toggle
</ha-icon-button>
</paper-input>
</vaadin-combo-box-light>
`;
}

private _clearValue(ev: Event) {
ev.stopPropagation();
this._setValue("");
}

private get _value() {
return this.value || "";
}

private _openedChanged(ev: PolymerChangedEvent<boolean>) {
this._opened = ev.detail.value;
}

private _valueChanged(ev: PolymerChangedEvent<string>) {
const newValue = ev.detail.value;
if (newValue !== this._value) {
this._setValue(newValue);
}
}

private _setValue(value: string) {
this.value = value;
setTimeout(() => {
fireEvent(this, "value-changed", { value });
fireEvent(this, "change");
}, 0);
}

static get styles(): CSSResult {
return css`
paper-input > ha-icon-button {
--mdc-icon-button-size: 24px;
padding: 0px 2px;
color: var(--secondary-text-color);
}
[hidden] {
display: none;
}
`;
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-entity-attribute-picker": HaEntityAttributePicker;
}
}
15 changes: 12 additions & 3 deletions src/components/entity/ha-entity-picker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "../ha-icon-button";
import "@polymer/paper-input/paper-input";
import "@polymer/paper-item/paper-icon-item";
import "@polymer/paper-item/paper-item-body";
Expand All @@ -20,6 +19,7 @@ import { computeDomain } from "../../common/entity/compute_domain";
import { computeStateName } from "../../common/entity/compute_state_name";
import { PolymerChangedEvent } from "../../polymer-types";
import { HomeAssistant } from "../../types";
import "../ha-icon-button";
import "./state-badge";

export type HaEntityPickerEntityFilterFunc = (entityId: HassEntity) => boolean;
Expand Down Expand Up @@ -95,6 +95,8 @@ class HaEntityPicker extends LitElement {

@query("vaadin-combo-box-light") private _comboBox!: HTMLElement;

private _initedStates = false;

private _getStates = memoizeOne(
(
_opened: boolean,
Expand Down Expand Up @@ -148,11 +150,18 @@ class HaEntityPicker extends LitElement {
);

protected shouldUpdate(changedProps: PropertyValues) {
if (
changedProps.has("value") ||
changedProps.has("label") ||
changedProps.has("disabled")
) {
return true;
}
return !(!changedProps.has("_opened") && this._opened);
}

protected updated(changedProps: PropertyValues) {
if (changedProps.has("_opened") && this._opened) {
if (!this._initedStates || (changedProps.has("_opened") && this._opened)) {
const states = this._getStates(
this._opened,
this.hass,
Expand All @@ -162,14 +171,14 @@ class HaEntityPicker extends LitElement {
this.includeDeviceClasses
);
(this._comboBox as any).items = states;
this._initedStates = true;
}
}

protected render(): TemplateResult {
if (!this.hass) {
return html``;
}

return html`
<vaadin-combo-box-light
item-value-path="entity_id"
Expand Down
9 changes: 7 additions & 2 deletions src/data/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
HassEntityBase,
} from "home-assistant-js-websocket";
import { navigate } from "../common/navigate";
import { HomeAssistant, Context } from "../types";
import { Context, HomeAssistant } from "../types";
import { DeviceCondition, DeviceTrigger } from "./device_automation";
import { Action } from "./script";

Expand All @@ -15,6 +15,7 @@ export interface AutomationEntity extends HassEntityBase {
}

export interface AutomationConfig {
id?: string;
alias: string;
description: string;
trigger: Trigger[];
Expand All @@ -32,7 +33,8 @@ export interface ForDict {

export interface StateTrigger {
platform: "state";
entity_id?: string;
entity_id: string;
attribute?: string;
from?: string | number;
to?: string | number;
for?: string | number | ForDict;
Expand All @@ -59,6 +61,7 @@ export interface HassTrigger {
export interface NumericStateTrigger {
platform: "numeric_state";
entity_id: string;
attribute?: string;
above?: number;
below?: number;
value_template?: string;
Expand Down Expand Up @@ -136,12 +139,14 @@ export interface LogicalCondition {
export interface StateCondition {
condition: "state";
entity_id: string;
attribute?: string;
state: string | number;
}

export interface NumericStateCondition {
condition: "numeric_state";
entity_id: string;
attribute?: string;
above?: number;
below?: number;
value_template?: string;
Expand Down
10 changes: 9 additions & 1 deletion src/data/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { computeObjectId } from "../common/entity/compute_object_id";
import { navigate } from "../common/navigate";
import { HomeAssistant } from "../types";
import { Condition } from "./automation";
import { Condition, Trigger } from "./automation";

export const MODES = ["single", "restart", "queued", "parallel"];
export const MODES_MAX = ["queued", "parallel"];
Expand Down Expand Up @@ -56,6 +56,13 @@ export interface SceneAction {
export interface WaitAction {
wait_template: string;
timeout?: number;
continue_on_timeout?: boolean;
}

export interface WaitForTriggerAction {
wait_for_trigger: Trigger[];
timeout?: number;
continue_on_timeout?: boolean;
}

export interface RepeatAction {
Expand Down Expand Up @@ -91,6 +98,7 @@ export type Action =
| DelayAction
| SceneAction
| WaitAction
| WaitForTriggerAction
| RepeatAction
| ChooseAction;

Expand Down
8 changes: 4 additions & 4 deletions src/dialogs/generic/dialog-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import {
CSSResult,
customElement,
html,
internalProperty,
LitElement,
property,
internalProperty,
TemplateResult,
} from "lit-element";
import { classMap } from "lit-html/directives/class-map";
import { fireEvent } from "../../common/dom/fire_event";
import "../../components/ha-dialog";
import "../../components/ha-switch";
import { PolymerChangedEvent } from "../../polymer-types";
import { haStyleDialog } from "../../resources/styles";
import { HomeAssistant } from "../../types";
import { DialogParams } from "./show-dialog-box";
import { fireEvent } from "../../common/dom/fire_event";

@customElement("dialog-box")
class DialogBox extends LitElement {
Expand Down Expand Up @@ -114,8 +114,8 @@ class DialogBox extends LitElement {
}

private _dismiss(): void {
if (this._params!.cancel) {
this._params!.cancel();
if (this._params?.cancel) {
this._params.cancel();
}
this._close();
}
Expand Down
Loading