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

Add support for automation config panel #7509

Merged
merged 3 commits into from
May 10, 2017
Merged

Add support for automation config panel #7509

merged 3 commits into from
May 10, 2017

Conversation

balloob
Copy link
Member

@balloob balloob commented May 9, 2017

Adds basics for an automation config panel.

Should only be merged with new build of frontend after home-assistant/frontend#275 has been merged.

Documentation

This is the first version. It will read/write automations that are in your <config>/automations.yaml. This file is a list of automations. You can copy/paste automations there, make sure to give each automation a unique string id.

It's not possible yet to add automations, just edit them.

Steps to get started

  • create automations.yaml in your config dir
  • make sure automations.yaml get loaded:
automation: !include automations.yaml
  • Copy/paste an existing automation to automations.yaml. Make sure to add a new key id that is a string. Example:
- id: beer
  trigger:
  - entity_id: sun.sun
    from: below_horizon
    platform: state
    to: above_horizon
  condition:
  - above: 17
    below: 25
    condition: numeric_state
    entity_id: sensor.temperature
    value_template: '{{ float(state.state) + 2 }}'
  action:
  - service: light.turn_on
  alias: Hello world

Profit 🎉

@arsaboo
Copy link
Contributor

arsaboo commented May 9, 2017

I am sure you have thought through this, but why can we not just use alias instead of the id? Is it the length of alias?

@@ -1 +1 @@
!(function(){"use strict";function e(e){return{type:"auth",api_password:e}}function t(){return{type:"get_states"}}function n(){return{type:"get_config"}}function r(){return{type:"get_services"}}function i(){return{type:"get_panels"}}function s(e,t,n){var r={type:"call_service",domain:e,service:t};return n&&(r.service_data=n),r}function o(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function c(e){return{type:"unsubscribe_events",subscription:e}}function u(){return{type:"ping"}}function a(e,t){return{type:"result",success:!1,error:{code:e,message:t}}}function f(t,n){function r(i,s,o){var c=new WebSocket(t),u=!1,a=function(){if(u)return void o(C);if(0===i)return void o(O);var e=i===-1?-1:i-1;setTimeout((function(){return r(e,s,o)}),1e3)},f=function(t){var r=JSON.parse(t.data);switch(r.type){case"auth_required":"authToken"in n?c.send(JSON.stringify(e(n.authToken))):(u=!0,c.close());break;case"auth_invalid":u=!0,c.close();break;case"auth_ok":c.removeEventListener("message",f),c.removeEventListener("close",a),s(c)}};c.addEventListener("message",f),c.addEventListener("close",a)}return new Promise(function(e,t){return r(n.setupRetry||0,e,t)})}function d(e){return e.result}function v(e,t){return void 0===t&&(t={}),f(e,t).then((function(n){var r=new L(e,t);return r.setSocket(n),r}))}function h(e,t){return e._subscribeConfig?e._subscribeConfig(t):new Promise(function(n,r){var i=null,s=null,o=[],c=null;t&&o.push(t);var u=function(e){i=Object.assign({},i,e);for(var t=0;t<o.length;t++)o[t](i)},a=function(e,t){return u({services:Object.assign({},i.services,(n={},n[e]=t,n))});var n},f=function(e){if(null!==i){var t=Object.assign({},i.core,{components:i.core.components.concat(e.data.component)});u({core:t})}},d=function(e){if(null!==i){var t,n=e.data,r=n.domain,s=n.service,o=Object.assign({},i.services[r]||{},(t={},t[s]={description:"",fields:{}},t));a(r,o)}},v=function(e){if(null!==i){var t=e.data,n=t.domain,r=t.service,s=i.services[n];if(s&&r in s){var o={};Object.keys(s).forEach((function(e){e!==r&&(o[e]=s[e])})),a(n,o)}}},h=function(){return Promise.all([e.getConfig(),e.getPanels(),e.getServices()]).then((function(e){var t=e[0],n=e[1],r=e[2];u({core:t,panels:n,services:r})}))},l=function(e){e&&o.splice(o.indexOf(e),1),0===o.length&&s()};e._subscribeConfig=function(e){return e&&(o.push(e),null!==i&&e(i)),c.then((function(){return function(){return l(e)}}))},c=Promise.all([e.subscribeEvents(f,"component_loaded"),e.subscribeEvents(d,"service_registered"),e.subscribeEvents(v,"service_removed"),h()]),c.then((function(r){var i=r[0],o=r[1],c=r[2];s=function(){removeEventListener("ready",h),i(),o(),c()},e.addEventListener("ready",h),n((function(){return l(t)}))}),(function(){return r()}))})}function l(e){for(var t={},n=0;n<e.length;n++){var r=e[n];t[r.entity_id]=r}return t}function p(e,t){var n=Object.assign({},e);return n[t.entity_id]=t,n}function b(e,t){var n=Object.assign({},e);return delete n[t],n}function g(e,t){return e._subscribeEntities?e._subscribeEntities(t):new Promise(function(n,r){function i(e){if(null!==c){var t=e.data,n=t.entity_id,r=t.new_state;c=r?p(c,r):b(c,n);for(var i=0;i<a.length;i++)a[i](c)}}function s(){return e.getStates().then((function(e){c=l(e);for(var t=0;t<a.length;t++)a[t](c)}))}function o(t){t&&a.splice(a.indexOf(t),1),0===a.length&&(u(),e.removeEventListener("ready",s),e._subscribeEntities=null)}var c=null,u=null,a=[],f=null;t&&a.push(t),e._subscribeEntities=function(e){return e&&(a.push(e),null!==c&&e(c)),f.then((function(){return function(){return o(e)}}))},f=Promise.all([e.subscribeEvents(i,"state_changed"),s()]),f.then((function(r){var i=r[0];u=i,e.addEventListener("ready",s),n((function(){return o(t)}))}),(function(){return r()}))})}function m(e){return e.substr(0,e.indexOf("."))}function y(e){return e.substr(e.indexOf(".")+1)}function _(e,t){var n={};return t.attributes.entity_id.forEach((function(t){var r=e[t];r&&(n[r.entity_id]=r)})),n}function E(e){var t=[],n={};return Object.keys(e).forEach((function(r){var i=e[r];"group"===m(r)?t.push(i):n[r]=i})),t.sort((function(e,t){return e.attributes.order-t.attributes.order})),t.forEach((function(e){return e.attributes.entity_id.forEach((function(e){delete n[e]}))})),{groups:t,ungrouped:n}}function w(e,t){var n={};return t.attributes.entity_id.forEach((function(t){var r=e[t];if(r&&!r.attributes.hidden&&(n[r.entity_id]=r,"group"===m(r.entity_id))){var i=_(e,r);Object.keys(i).forEach((function(e){var t=i[e];t.attributes.hidden||(n[e]=t)}))}})),n}function k(e){var t=[];return Object.keys(e).forEach((function(n){var r=e[n];r.attributes.view&&t.push(r)})),t.sort((function(e,t){return e.entity_id===P?-1:t.entity_id===P?1:e.attributes.order-t.attributes.order})),t}var O=1,C=2,j=3,L=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.eventListeners={},this.closeRequested=!1,this._handleMessage=this._handleMessage.bind(this),this._handleClose=this._handleClose.bind(this)};L.prototype.setSocket=function(e){var t=this,n=this.socket;if(this.socket=e,e.addEventListener("message",this._handleMessage),e.addEventListener("close",this._handleClose),n){var r=this.commands;this.commandId=1,this.commands={},Object.keys(r).forEach((function(e){var n=r[e];n.eventType&&t.subscribeEvents(n.eventCallback,n.eventType).then((function(e){n.unsubscribe=e}))})),this.fireEvent("ready")}},L.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},L.prototype.removeEventListener=function(e,t){var n=this.eventListeners[e];if(n){var r=n.indexOf(t);r!==-1&&n.splice(r,1)}},L.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach((function(e){return e(t)}))},L.prototype.close=function(){this.closeRequested=!0,this.socket.close()},L.prototype.getStates=function(){return this.sendMessagePromise(t()).then(d)},L.prototype.getServices=function(){return this.sendMessagePromise(r()).then(d)},L.prototype.getPanels=function(){return this.sendMessagePromise(i()).then(d)},L.prototype.getConfig=function(){return this.sendMessagePromise(n()).then(d)},L.prototype.callService=function(e,t,n){return this.sendMessagePromise(s(e,t,n))},L.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(o(t)).then((function(r){var i={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(c(r.id)).then((function(){delete n.commands[r.id]}))}};return n.commands[r.id]=i,function(){return i.unsubscribe()}}))},L.prototype.ping=function(){return this.sendMessagePromise(u())},L.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},L.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,r){t.commandId+=1;var i=t.commandId;e.id=i,t.commands[i]={resolve:n,reject:r},t.sendMessage(e)})},L.prototype._handleMessage=function(e){var t=JSON.parse(e.data);switch(t.type){case"event":this.commands[t.id].eventCallback(t.event);break;case"result":t.success?this.commands[t.id].resolve(t):this.commands[t.id].reject(t.error),delete this.commands[t.id];break;case"pong":}},L.prototype._handleClose=function(){var e=this;if(Object.keys(this.commands).forEach((function(t){var n=e.commands[t],r=n.reject;r&&r(a(j,"Connection lost"))})),!this.closeRequested){this.fireEvent("disconnected");var t=Object.assign({},this.options,{setupRetry:0}),n=function(r){setTimeout((function(){f(e.url,t).then((function(t){return e.setSocket(t)}),(function(){return n(r+1)}))}),1e3*Math.min(r,5))};n(0)}};var P="group.default_view",S=Object.freeze({ERR_CANNOT_CONNECT:O,ERR_INVALID_AUTH:C,createConnection:v,subscribeConfig:h,subscribeEntities:g,getGroupEntities:_,splitByGroups:E,getViewEntities:w,extractViews:k,extractDomain:m,extractObjectId:y});window.HAWS=S,window.HASS_DEMO=!1;var M=window.createHassConnection=function(e){var t="https:"===window.location.protocol?"wss":"ws",n=t+"://"+window.location.host+"/api/websocket",r={setupRetry:10};return void 0!==e&&(r.authToken=e),v(n,r).then((function(e){return g(e),h(e),e}))};window.noAuth?window.hassConnection=M():window.localStorage.authToken?window.hassConnection=M(window.localStorage.authToken):window.hassConnection=null,"serviceWorker"in navigator&&window.addEventListener("load",(function(){navigator.serviceWorker.register("/service_worker.js")}))})();
!function(){"use strict";function e(e){return{type:"auth",api_password:e}}function t(){return{type:"get_states"}}function n(){return{type:"get_config"}}function i(){return{type:"get_services"}}function r(){return{type:"get_panels"}}function s(e,t,n){var i={type:"call_service",domain:e,service:t};return n&&(i.service_data=n),i}function o(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function c(e){return{type:"unsubscribe_events",subscription:e}}function u(){return{type:"ping"}}function a(e,t){return{type:"result",success:!1,error:{code:e,message:t}}}function f(t,n){function i(r,s,o){var c=new WebSocket(t),u=!1,a=function(){if(u)return void o(C);if(0===r)return void o(O);var e=-1===r?-1:r-1;setTimeout(function(){return i(e,s,o)},1e3)},f=function t(i){switch(JSON.parse(i.data).type){case"auth_required":"authToken"in n?c.send(JSON.stringify(e(n.authToken))):(u=!0,c.close());break;case"auth_invalid":u=!0,c.close();break;case"auth_ok":c.removeEventListener("message",t),c.removeEventListener("close",a),s(c)}};c.addEventListener("message",f),c.addEventListener("close",a)}return new Promise(function(e,t){return i(n.setupRetry||0,e,t)})}function d(e){return e.result}function v(e,t){return void 0===t&&(t={}),f(e,t).then(function(n){var i=new j(e,t);return i.setSocket(n),i})}function h(e,t){return e._subscribeConfig?e._subscribeConfig(t):new Promise(function(n,i){var r=null,s=null,o=[],c=null;t&&o.push(t);var u=function(e){r=Object.assign({},r,e);for(var t=0;t<o.length;t++)o[t](r)},a=function(e,t){return u({services:Object.assign({},r.services,(n={},n[e]=t,n))});var n},f=function(e){if(null!==r){var t=Object.assign({},r.core,{components:r.core.components.concat(e.data.component)});u({core:t})}},d=function(e){if(null!==r){var t,n=e.data,i=n.domain,s=n.service,o=Object.assign({},r.services[i]||{},(t={},t[s]={description:"",fields:{}},t));a(i,o)}},v=function(e){if(null!==r){var t=e.data,n=t.domain,i=t.service,s=r.services[n];if(s&&i in s){var o={};Object.keys(s).forEach(function(e){e!==i&&(o[e]=s[e])}),a(n,o)}}},h=function(){return Promise.all([e.getConfig(),e.getPanels(),e.getServices()]).then(function(e){var t=e[0],n=e[1],i=e[2];u({core:t,panels:n,services:i})})},l=function(e){e&&o.splice(o.indexOf(e),1),0===o.length&&s()};e._subscribeConfig=function(e){return e&&(o.push(e),null!==r&&e(r)),c.then(function(){return function(){return l(e)}})},c=Promise.all([e.subscribeEvents(f,"component_loaded"),e.subscribeEvents(d,"service_registered"),e.subscribeEvents(v,"service_removed"),h()]),c.then(function(i){var r=i[0],o=i[1],c=i[2];s=function(){removeEventListener("ready",h),r(),o(),c()},e.addEventListener("ready",h),n(function(){return l(t)})},function(){return i()})})}function l(e){for(var t={},n=0;n<e.length;n++){var i=e[n];t[i.entity_id]=i}return t}function p(e,t){var n=Object.assign({},e);return n[t.entity_id]=t,n}function b(e,t){var n=Object.assign({},e);return delete n[t],n}function g(e,t){return e._subscribeEntities?e._subscribeEntities(t):new Promise(function(n,i){function r(e){if(null!==c){var t=e.data,n=t.entity_id,i=t.new_state;c=i?p(c,i):b(c,n);for(var r=0;r<a.length;r++)a[r](c)}}function s(){return e.getStates().then(function(e){c=l(e);for(var t=0;t<a.length;t++)a[t](c)})}function o(t){t&&a.splice(a.indexOf(t),1),0===a.length&&(u(),e.removeEventListener("ready",s),e._subscribeEntities=null)}var c=null,u=null,a=[],f=null;t&&a.push(t),e._subscribeEntities=function(e){return e&&(a.push(e),null!==c&&e(c)),f.then(function(){return function(){return o(e)}})},f=Promise.all([e.subscribeEvents(r,"state_changed"),s()]),f.then(function(i){var r=i[0];u=r,e.addEventListener("ready",s),n(function(){return o(t)})},function(){return i()})})}function m(e){return e.substr(0,e.indexOf("."))}function y(e){return e.substr(e.indexOf(".")+1)}function _(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var i=e[t];i&&(n[i.entity_id]=i)}),n}function E(e){var t=[],n={};return Object.keys(e).forEach(function(i){var r=e[i];"group"===m(i)?t.push(r):n[i]=r}),t.sort(function(e,t){return e.attributes.order-t.attributes.order}),t.forEach(function(e){return e.attributes.entity_id.forEach(function(e){delete n[e]})}),{groups:t,ungrouped:n}}function w(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var i=e[t];if(i&&!i.attributes.hidden&&(n[i.entity_id]=i,"group"===m(i.entity_id))){var r=_(e,i);Object.keys(r).forEach(function(e){var t=r[e];t.attributes.hidden||(n[e]=t)})}}),n}function k(e){var t=[];return Object.keys(e).forEach(function(n){var i=e[n];i.attributes.view&&t.push(i)}),t.sort(function(e,t){return e.entity_id===L?-1:t.entity_id===L?1:e.attributes.order-t.attributes.order}),t}var O=1,C=2,j=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.eventListeners={},this.closeRequested=!1,this._handleMessage=this._handleMessage.bind(this),this._handleClose=this._handleClose.bind(this)};j.prototype.setSocket=function(e){var t=this,n=this.socket;if(this.socket=e,e.addEventListener("message",this._handleMessage),e.addEventListener("close",this._handleClose),n){var i=this.commands;this.commandId=1,this.commands={},Object.keys(i).forEach(function(e){var n=i[e];n.eventType&&t.subscribeEvents(n.eventCallback,n.eventType).then(function(e){n.unsubscribe=e})}),this.fireEvent("ready")}},j.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},j.prototype.removeEventListener=function(e,t){var n=this.eventListeners[e];if(n){var i=n.indexOf(t);-1!==i&&n.splice(i,1)}},j.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},j.prototype.close=function(){this.closeRequested=!0,this.socket.close()},j.prototype.getStates=function(){return this.sendMessagePromise(t()).then(d)},j.prototype.getServices=function(){return this.sendMessagePromise(i()).then(d)},j.prototype.getPanels=function(){return this.sendMessagePromise(r()).then(d)},j.prototype.getConfig=function(){return this.sendMessagePromise(n()).then(d)},j.prototype.callService=function(e,t,n){return this.sendMessagePromise(s(e,t,n))},j.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(o(t)).then(function(i){var r={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(c(i.id)).then(function(){delete n.commands[i.id]})}};return n.commands[i.id]=r,function(){return r.unsubscribe()}})},j.prototype.ping=function(){return this.sendMessagePromise(u())},j.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},j.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,i){t.commandId+=1;var r=t.commandId;e.id=r,t.commands[r]={resolve:n,reject:i},t.sendMessage(e)})},j.prototype._handleMessage=function(e){var t=JSON.parse(e.data);switch(t.type){case"event":this.commands[t.id].eventCallback(t.event);break;case"result":t.success?this.commands[t.id].resolve(t):this.commands[t.id].reject(t.error),delete this.commands[t.id]}},j.prototype._handleClose=function(){var e=this;if(Object.keys(this.commands).forEach(function(t){var n=e.commands[t],i=n.reject;i&&i(a(3,"Connection lost"))}),!this.closeRequested){this.fireEvent("disconnected");var t=Object.assign({},this.options,{setupRetry:0});!function n(i){setTimeout(function(){f(e.url,t).then(function(t){return e.setSocket(t)},function(){return n(i+1)})},1e3*Math.min(i,5))}(0)}};var L="group.default_view",P=Object.freeze({ERR_CANNOT_CONNECT:O,ERR_INVALID_AUTH:C,createConnection:v,subscribeConfig:h,subscribeEntities:g,getGroupEntities:_,splitByGroups:E,getViewEntities:w,extractViews:k,extractDomain:m,extractObjectId:y});window.HAWS=P,window.HASS_DEMO=!1;var S=window.createHassConnection=function(e){var t="https:"===window.location.protocol?"wss":"ws",n=t+"://"+window.location.host+"/api/websocket",i={setupRetry:10};return void 0!==e&&(i.authToken=e),v(n,i).then(function(e){return g(e),h(e),e})};window.noAuth?window.hassConnection=S():window.localStorage.authToken?window.hassConnection=S(window.localStorage.authToken):window.hassConnection=null,"serviceWorker"in navigator&&window.addEventListener("load",function(){navigator.serviceWorker.register("/service_worker.js")})}();

Choose a reason for hiding this comment

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

Missing semicolon.
Expected an assignment or function call and instead saw an expression.
Unreachable 'var' after 'return'.
Too many errors. (50% scanned).

@@ -1 +1 @@
!(function(){"use strict";function e(e,r){var t=arguments;if(void 0===e||null===e)throw new TypeError("Cannot convert first argument to object");for(var n=Object(e),o=1;o<arguments.length;o++){var i=t[o];if(void 0!==i&&null!==i)for(var l=Object.keys(Object(i)),a=0,c=l.length;a<c;a++){var b=l[a],f=Object.getOwnPropertyDescriptor(i,b);void 0!==f&&f.enumerable&&(n[b]=i[b])}}return n}function r(){Object.assign||Object.defineProperty(Object,"assign",{enumerable:!1,configurable:!0,writable:!0,value:e})}var t={assign:e,polyfill:r};t.polyfill()})();
!function(){"use strict";function e(e,t){if(void 0===e||null===e)throw new TypeError("Cannot convert first argument to object");for(var r=Object(e),n=1;n<arguments.length;n++){var o=arguments[n];if(void 0!==o&&null!==o)for(var i=Object.keys(Object(o)),l=0,c=i.length;l<c;l++){var a=i[l],b=Object.getOwnPropertyDescriptor(o,a);void 0!==b&&b.enumerable&&(r[a]=o[a])}}return r}function t(){Object.assign||Object.defineProperty(Object,"assign",{enumerable:!1,configurable:!0,writable:!0,value:e})}({assign:e,polyfill:t}).polyfill()}();

Choose a reason for hiding this comment

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

Expected an assignment or function call and instead saw an expression.
Missing semicolon.
Function declarations are not invocable. Wrap the whole function invocation in parens.

@balloob
Copy link
Member Author

balloob commented May 9, 2017

Alias doesn't have to be unique.

@balloob balloob merged commit 5d820ec into dev May 10, 2017
@balloob balloob deleted the automation-panel branch May 10, 2017 01:45
@home-assistant home-assistant locked and limited conversation to collaborators Aug 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants