Migrate automations config panel to router (#5509)

* MIgrate automations config panel to router

* Correct link to create new
This commit is contained in:
Bram Kragten 2020-04-09 19:34:45 +02:00 committed by GitHub
parent 0c06517dcc
commit baf5dcbd03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 120 deletions

View File

@ -189,7 +189,7 @@ export const showAutomationEditor = (
data?: Partial<AutomationConfig> data?: Partial<AutomationConfig>
) => { ) => {
inititialAutomationEditorData = data; inititialAutomationEditorData = data;
navigate(el, "/config/automation/new"); navigate(el, "/config/automation/edit/new");
}; };
export const getAutomationEditorInitData = () => { export const getAutomationEditorInitData = () => {

View File

@ -1,119 +0,0 @@
import "@polymer/app-route/app-route";
import { html } from "@polymer/polymer/lib/utils/html-tag";
import { PolymerElement } from "@polymer/polymer/polymer-element";
import "./ha-automation-editor";
import "./ha-automation-picker";
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
class HaConfigAutomation extends PolymerElement {
static get template() {
return html`
<style>
ha-automation-picker,
ha-automation-editor {
height: 100%;
}
</style>
<app-route
route="[[route]]"
pattern="/edit/:automation"
data="{{_routeData}}"
active="{{_edittingAutomation}}"
></app-route>
<app-route
route="[[route]]"
pattern="/new"
active="{{_creatingNew}}"
></app-route>
<template is="dom-if" if="[[!showEditor]]">
<ha-automation-picker
hass="[[hass]]"
automations="[[automations]]"
is-wide="[[isWide]]"
narrow="[[narrow]]"
route="[[route]]"
></ha-automation-picker>
</template>
<template is="dom-if" if="[[showEditor]]" restamp="">
<ha-automation-editor
hass="[[hass]]"
automation="[[automation]]"
is-wide="[[isWide]]"
narrow="[[narrow]]"
route="[[route]]"
creating-new="[[_creatingNew]]"
></ha-automation-editor>
</template>
`;
}
static get properties() {
return {
hass: Object,
route: Object,
isWide: Boolean,
narrow: Boolean,
_routeData: Object,
_routeMatches: Boolean,
_creatingNew: Boolean,
_edittingAutomation: Boolean,
automations: {
type: Array,
computed: "computeAutomations(hass)",
},
automation: {
type: Object,
computed:
"computeAutomation(automations, _edittingAutomation, _routeData)",
},
showEditor: {
type: Boolean,
computed: "computeShowEditor(_edittingAutomation, _creatingNew)",
},
};
}
disconnectedCallback() {
super.disconnectedCallback();
this.route = { path: "", prefix: "" };
}
computeAutomation(automations, edittingAddon, routeData) {
if (!automations || !edittingAddon) {
return null;
}
for (var i = 0; i < automations.length; i++) {
if (automations[i].attributes.id === routeData.automation) {
return automations[i];
}
}
return null;
}
computeAutomations(hass) {
var automations = [];
Object.keys(hass.states).forEach(function(key) {
var entity = hass.states[key];
if (computeStateDomain(entity) === "automation") {
automations.push(entity);
}
});
return automations;
}
computeShowEditor(_edittingAutomation, _creatingNew) {
return _creatingNew || _edittingAutomation;
}
}
customElements.define("ha-config-automation", HaConfigAutomation);

View File

@ -0,0 +1,82 @@
import { HassEntities } from "home-assistant-js-websocket";
import { customElement, property, PropertyValues } from "lit-element";
import memoizeOne from "memoize-one";
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
import { AutomationEntity } from "../../../data/automation";
import {
HassRouterPage,
RouterOptions,
} from "../../../layouts/hass-router-page";
import { HomeAssistant } from "../../../types";
import "./ha-automation-editor";
import "./ha-automation-picker";
@customElement("ha-config-automation")
class HaConfigAutomation extends HassRouterPage {
@property() public hass!: HomeAssistant;
@property() public narrow!: boolean;
@property() public isWide!: boolean;
@property() public showAdvanced!: boolean;
@property() public automations: AutomationEntity[] = [];
protected routerOptions: RouterOptions = {
defaultPage: "dashboard",
routes: {
dashboard: {
tag: "ha-automation-picker",
cache: true,
},
edit: {
tag: "ha-automation-editor",
},
},
};
private _computeAutomations = memoizeOne((states: HassEntities) => {
const automations: AutomationEntity[] = [];
Object.values(states).forEach((state) => {
if (
computeStateDomain(state) === "automation" &&
!state.attributes.hidden
) {
automations.push(state as AutomationEntity);
}
});
return automations;
});
protected updatePageEl(pageEl, changedProps: PropertyValues) {
pageEl.hass = this.hass;
pageEl.narrow = this.narrow;
pageEl.isWide = this.isWide;
pageEl.route = this.routeTail;
pageEl.showAdvanced = this.showAdvanced;
if (this.hass) {
pageEl.automations = this._computeAutomations(this.hass.states);
}
if (
(!changedProps || changedProps.has("route")) &&
this._currentPage === "edit"
) {
pageEl.creatingNew = undefined;
const automationId = this.routeTail.path.substr(1);
pageEl.creatingNew = automationId === "new" ? true : false;
pageEl.automation =
automationId === "new"
? undefined
: pageEl.automations.find(
(entity: AutomationEntity) =>
entity.attributes.id === automationId
);
}
}
}
declare global {
interface HTMLElementTagNameMap {
"ha-config-automation": HaConfigAutomation;
}
}