1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-24 20:55:49 +00:00
Files
frontend/panels/dev-service/ha-panel-dev-service.html
2017-01-06 11:21:16 +01:00

257 lines
6.6 KiB
HTML

<link rel='import' href='../../bower_components/polymer/polymer.html'>
<link rel='import' href='../../bower_components/paper-button/paper-button.html'>
<link rel='import' href='../../bower_components/paper-input/paper-textarea.html'>
<link rel='import' href='../../bower_components/app-layout/app-header-layout/app-header-layout.html'>
<link rel='import' href='../../bower_components/app-layout/app-header/app-header.html'>
<link rel='import' href='../../bower_components/app-layout/app-toolbar/app-toolbar.html'>
<link rel="import" href="../../bower_components/app-storage/app-localstorage/app-localstorage-document.html">
<link rel="import" href="../../bower_components/vaadin-combo-box/vaadin-combo-box.html">
<link rel='import' href='../../src/components/ha-menu-button.html'>
<link rel='import' href='../../src/resources/ha-style.html'>
<link rel='import' href='../../src/util/hass-behavior.html'>
<dom-module id='ha-panel-dev-service'>
<template>
<style include='ha-style'>
:host {
background-color: white;
-ms-user-select: initial;
-webkit-user-select: initial;
-moz-user-select: initial;
}
.content {
padding: 16px;
}
.ha-form {
margin-right: 16px;
max-width: 500px;
}
.description {
margin-top: 24px;
white-space: pre-wrap;
}
.header {
@apply(--paper-font-title);
}
.attributes th {
text-align: left;
}
.attributes tr {
vertical-align: top;
}
.attributes tr:nth-child(even) {
background-color: #eee;
}
.attributes td:nth-child(3) {
white-space: pre-wrap;
word-break: break-word;
}
pre {
margin: 0;
}
</style>
<app-header-layout has-scrolling-region>
<app-header fixed>
<app-toolbar>
<ha-menu-button narrow='[[narrow]]' show-menu='[[showMenu]]'></ha-menu-button>
<div main-title>Services</div>
</app-toolbar>
</app-header>
<app-localstorage-document
key='panel-dev-service-state-domain'
data='{{domain}}'>
</app-localstorage-document>
<app-localstorage-document
key='panel-dev-service-state-service'
data='{{service}}'>
</app-localstorage-document>
<app-localstorage-document
key='panel-dev-service-state-servicedata'
data='{{serviceData}}'>
</app-localstorage-document>
<div class='content'>
<p>
Call a service from a component.
</p>
<div class='ha-form'>
<vaadin-combo-box label='Domain' items='[[computeDomains(serviceDomains)]]' value='{{domain}}'></vaadin-combo-box>
<vaadin-combo-box label='Service' items='[[computeServices(serviceDomains, domain)]]' value='{{service}}'></vaadin-combo-box>
<paper-textarea label='Service Data (JSON, optional)' value='{{serviceData}}'></paper-textarea>
<paper-button on-tap='callService' raised>Call Service</paper-button>
</div>
<template is='dom-if' if='[[!domain]]'>
<h1>Select a domain and service to see the description</h1>
</template>
<template is='dom-if' if='[[domain]]'>
<template is='dom-if' if='[[!service]]'>
<h1>Select a service to see the description</h1>
</template>
</template>
<template is='dom-if' if='[[domain]]'>
<template is='dom-if' if='[[service]]'>
<template is='dom-if' if='[[!_attributes.length]]'>
<h1>No description is available</h1>
</template>
<template is='dom-if' if='[[_attributes.length]]'>
<h1>Valid Parameters</h1>
<table class='attributes'>
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Example</th>
</tr>
<template is='dom-repeat' items='[[_attributes]]' as='attribute'>
<tr>
<td><pre>[[attribute.key]]</pre></td>
<td>[[attribute.description]]</td>
<td>[[attribute.example]]</td>
</tr>
</template>
</table>
</template>
</template>
</template>
</div>
</app-header-layout>
</template>
</dom-module>
<script>
Polymer({
is: 'ha-panel-dev-service',
behaviors: [window.hassBehavior],
properties: {
hass: {
type: Object,
},
narrow: {
type: Boolean,
value: false,
},
showMenu: {
type: Boolean,
value: false,
},
domain: {
type: String,
value: '',
observer: 'domainChanged',
},
service: {
type: String,
value: '',
observer: 'serviceChanged',
},
serviceData: {
type: String,
value: '',
},
_attributes: {
type: Array,
computed: 'computeAttributesArray(hass, domain, service)',
},
serviceDomains: {
type: Array,
bindNuclear: function (hass) {
return hass.serviceGetters.entityMap;
},
},
},
computeAttributesArray: function (hass, domain, service) {
return hass.reactor.evaluate([
hass.serviceGetters.entityMap,
function (map) {
if (map.has(domain) && map.get(domain).get('services').has(service)) {
return map
.get(domain)
.get('services')
.get(service)
.get('fields')
.map(function (field, key) {
var fieldCopy = field.toJS();
fieldCopy.key = key;
return fieldCopy;
})
.toArray();
}
return [];
},
]);
},
computeDomains: function (serviceDomains) {
return serviceDomains
.valueSeq()
.map(function (domain) { return domain.domain; })
.sort()
.toJS();
},
computeServices: function (serviceDomains, domain) {
if (domain) {
return serviceDomains
.get(domain)
.get('services')
.keySeq()
.toArray();
}
return '';
},
domainChanged: function () {
this.service = '';
this.serviceData = '';
},
serviceChanged: function () {
this.serviceData = '';
},
callService: function () {
var serviceData;
try {
serviceData = this.serviceData ? JSON.parse(this.serviceData) : {};
} catch (err) {
/* eslint-disable no-alert */
alert('Error parsing JSON: ' + err);
/* eslint-enable no-alert */
return;
}
this.hass.serviceActions.callService(this.domain, this.service, serviceData);
},
});
</script>