Calling a service can now block till execution is done
This commit is contained in:
1 parent
f8223053bd
commit
78d5625ace
6 files changed
+85
-29
No files matched your search
@@ -18,7 +18,8 @@ import functools as ft
|
||||
from homeassistant.const import (
|
||||
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
|
||||
SERVICE_HOMEASSISTANT_STOP, EVENT_TIME_CHANGED, EVENT_STATE_CHANGED,
|
||||
EVENT_CALL_SERVICE, ATTR_NOW, ATTR_DOMAIN, ATTR_SERVICE, MATCH_ALL)
|
||||
EVENT_CALL_SERVICE, ATTR_NOW, ATTR_DOMAIN, ATTR_SERVICE, MATCH_ALL,
|
||||
EVENT_SERVICE_EXECUTED, ATTR_SERVICE_CALL_ID)
|
||||
import homeassistant.util as util
|
||||
|
||||
DOMAIN = "homeassistant"
|
||||
@@ -26,6 +27,9 @@ DOMAIN = "homeassistant"
|
||||
# How often time_changed event should fire
|
||||
TIMER_INTERVAL = 10 # seconds
|
||||
|
||||
# How long we wait for the result of a service call
|
||||
SERVICE_CALL_LIMIT = 10 # seconds
|
||||
|
||||
# Number of worker threads
|
||||
POOL_NUM_THREAD = 4
|
||||
|
||||
@@ -227,6 +231,7 @@ class JobPriority(util.OrderedEnum):
|
||||
""" Provides priorities for bus events. """
|
||||
# pylint: disable=no-init,too-few-public-methods
|
||||
|
||||
EVENT_CALLBACK = 0
|
||||
EVENT_SERVICE = 1
|
||||
EVENT_STATE = 2
|
||||
EVENT_TIME = 3
|
||||
@@ -241,6 +246,8 @@ class JobPriority(util.OrderedEnum):
|
||||
return JobPriority.EVENT_STATE
|
||||
elif event_type == EVENT_CALL_SERVICE:
|
||||
return JobPriority.EVENT_SERVICE
|
||||
elif event_type == EVENT_SERVICE_EXECUTED:
|
||||
return JobPriority.EVENT_CALLBACK
|
||||
else:
|
||||
return JobPriority.EVENT_DEFAULT
|
||||
|
||||
@@ -594,6 +601,7 @@ class ServiceRegistry(object):
|
||||
self._lock = threading.Lock()
|
||||
self._pool = pool or create_worker_pool()
|
||||
self._bus = bus
|
||||
self._cur_id = 0
|
||||
bus.listen(EVENT_CALL_SERVICE, self._event_to_service_call)
|
||||
|
||||
@property
|
||||
@@ -615,9 +623,14 @@ class ServiceRegistry(object):
|
||||
else:
|
||||
self._services[domain] = {service: service_func}
|
||||
|
||||
def call(self, domain, service, service_data=None):
|
||||
def call(self, domain, service, service_data=None, blocking=False):
|
||||
"""
|
||||
Fires event to call specified service.
|
||||
Calls specified service.
|
||||
Specify blocking=True to wait till service is executed.
|
||||
Waits a maximum of SERVICE_CALL_LIMIT.
|
||||
|
||||
If blocking = True, will return boolean if service executed
|
||||
succesfully within SERVICE_CALL_LIMIT.
|
||||
|
||||
This method will fire an event to call the service.
|
||||
This event will be picked up by this ServiceRegistry and any
|
||||
@@ -626,12 +639,41 @@ class ServiceRegistry(object):
|
||||
Because the service is sent as an event you are not allowed to use
|
||||
the keys ATTR_DOMAIN and ATTR_SERVICE in your service_data.
|
||||
"""
|
||||
call_id = self._generate_unique_id()
|
||||
event_data = service_data or {}
|
||||
event_data[ATTR_DOMAIN] = domain
|
||||
event_data[ATTR_SERVICE] = service
|
||||
event_data[ATTR_SERVICE_CALL_ID] = call_id
|
||||
|
||||
if blocking:
|
||||
executed_event = threading.Event()
|
||||
|
||||
def service_executed(call):
|
||||
"""
|
||||
Called when a service is executed.
|
||||
Will set the event if matches our service call.
|
||||
"""
|
||||
if call.data[ATTR_SERVICE_CALL_ID] == call_id:
|
||||
executed_event.set()
|
||||
|
||||
self._bus.remove_listener(
|
||||
EVENT_SERVICE_EXECUTED, service_executed)
|
||||
|
||||
self._bus.listen(EVENT_SERVICE_EXECUTED, service_executed)
|
||||
|
||||
self._bus.fire(EVENT_CALL_SERVICE, event_data)
|
||||
|
||||
if blocking:
|
||||
# wait will return False if event not set after our limit has
|
||||
# passed. If not set, clean up the listener
|
||||
if not executed_event.wait(SERVICE_CALL_LIMIT):
|
||||
self._bus.remove_listener(
|
||||
EVENT_SERVICE_EXECUTED, service_executed)
|
||||
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _event_to_service_call(self, event):
|
||||
""" Calls a service from an event. """
|
||||
service_data = dict(event.data)
|
||||
@@ -642,9 +684,27 @@ class ServiceRegistry(object):
|
||||
if domain in self._services and service in self._services[domain]:
|
||||
service_call = ServiceCall(domain, service, service_data)
|
||||
|
||||
# Add a job to the pool that calls _execute_service
|
||||
self._pool.add_job(JobPriority.EVENT_SERVICE,
|
||||
(self._services[domain][service],
|
||||
service_call))
|
||||
(self._execute_service,
|
||||
(self._services[domain][service],
|
||||
service_call)))
|
||||
|
||||
def _execute_service(self, service_and_call):
|
||||
""" Executes a service and fires a SERVICE_EXECUTED event. """
|
||||
service, call = service_and_call
|
||||
|
||||
service(call)
|
||||
|
||||
self._bus.fire(
|
||||
EVENT_SERVICE_EXECUTED, {
|
||||
ATTR_SERVICE_CALL_ID: call.data[ATTR_SERVICE_CALL_ID]
|
||||
})
|
||||
|
||||
def _generate_unique_id(self):
|
||||
""" Generates a unique service call id. """
|
||||
self._cur_id += 1
|
||||
return "{}-{}".format(id(self), self._cur_id)
|
||||
|
||||
|
||||
class Timer(threading.Thread):
|
||||
|
||||
@@ -484,7 +484,7 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||
domain = path_match.group('domain')
|
||||
service = path_match.group('service')
|
||||
|
||||
self.server.hass.services.call(domain, service, data)
|
||||
self.server.hass.services.call(domain, service, data, True)
|
||||
|
||||
self._json_message("Service {}/{} called.".format(domain, service))
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
""" DO NOT MODIFY. Auto-generated by build_frontend script """
|
||||
VERSION = "b32f2de4e3336946fe68cd1e5cd0fe6f"
|
||||
VERSION = "5c3b2dd8a63197e380e798da8b057b0a"
|
||||
@@ -66,6 +66,6 @@ if(this.removeAttribute(a),d)return j(this,a,c);var e=c,f=m(this,a,e);return j(t
|
||||
</paper-tab></template></paper-tabs></div></core-toolbar><states-cards api="{{api}}" filter="{{selectedTab}}" class="content"></states-cards></core-header-panel></template><script>Polymer("home-assistant-main",{selectedTab:null,computed:{customGroups:"getCustomGroups(api.states)",hasCustomGroups:"customGroups.length > 0"},tabClicked:function(ev){if(ev.detail.isSelected){this.selectedTab=ev.detail.item.getAttribute("data-entity")}},handleRefreshClick:function(){this.api.fetchAll()},handleEventClick:function(){this.api.showFireEventDialog()},handleServiceClick:function(){this.api.showCallServiceDialog()},handleAddStateClick:function(){this.api.showSetStateDialog()},handleLogOutClick:function(){this.api.logOut()},getCustomGroups:function(states){return states?states.filter(function(state){return state.isCustomGroup}):[]}});</script></polymer-element></div>
|
||||
<div hidden><polymer-element name="core-media-query" attributes="query queryMatches" assetpath="polymer/bower_components/core-media-query/"><template><style>:host{display:none}</style></template><script>Polymer("core-media-query",{queryMatches:false,query:"",ready:function(){this._mqHandler=this.queryHandler.bind(this);this._mq=null},queryChanged:function(){if(this._mq){this._mq.removeListener(this._mqHandler)}var query=this.query;if(query[0]!=="("){query="("+this.query+")"}this._mq=window.matchMedia(query);this._mq.addListener(this._mqHandler);this.queryHandler(this._mq)},queryHandler:function(mq){this.queryMatches=mq.matches;this.asyncFire("core-media-change",mq)}});</script></polymer-element><polymer-element name="paper-toast" attributes="text duration opened responsiveWidth swipeDisabled autoCloseDisabled" role="status" assetpath="polymer/bower_components/paper-toast/"><template><style>:host{display:inline-block;background:#323232;color:#f1f1f1;min-height:48px;min-width:288px;padding:16px 24px 12px;box-sizing:border-box;-moz-box-sizing:border-box;box-shadow:0 2px 5px 0 rgba(0,0,0,.26);border-radius:2px;bottom:12px;left:12px;font-size:14px;cursor:default}:host(.capsule){border-radius:24px}:host(.fit-bottom){bottom:0;left:0;width:100%;min-width:0;border-radius:0}:host(.core-transition.dragging){transition:none}:host(.core-transition.fade-out-down),:host(.core-transition.fade-out-up),:host(.core-transition.fade-out-right),:host(.core-transition.fade-out-left){opacity:0;transition:-webkit-transform .08s ease-in-out,opacity .08s ease-in-out;transition:transform .08s ease-in-out,opacity .08s ease-in-out}:host(.core-transition.fade-out-down){-webkit-transform:translate(0,100%);transform:translate(0,100%)}:host(.core-transition.fade-out-up){-webkit-transform:translate(0,-100%);transform:translate(0,-100%)}:host(.core-transition.fade-out-right){-webkit-transform:translate(100%,0);transform:translate(100%,0)}:host(.core-transition.fade-out-left){-webkit-transform:translate(-100%,0);transform:translate(-100%,0)}.toast-container{overflow:hidden}.toast-action{padding-left:24px;cursor:pointer;text-transform:uppercase}</style><core-overlay id="overlay" autofocusdisabled="" autoclosedisabled="{{autoCloseDisabled}}" opened="{{opened}}" target="{{}}" transition="core-transition-bottom"></core-overlay><div class="toast-container" horizontal="" layout=""><div class="toast-text" flex="">{{text}}</div><div class="toast-text toast-action" on-tap="{{dismiss}}"><content></content></div></div><core-media-query query="max-width: {{responsiveWidth}}" querymatches="{{narrowMode}}"></core-media-query></template><script>(function(){var currentToast;Polymer("paper-toast",{text:"",duration:3e3,opened:false,responsiveWidth:"480px",swipeDisabled:false,autoCloseDisabled:false,narrowMode:false,eventDelegates:{trackstart:"trackStart",track:"track",trackend:"trackEnd",transitionend:"transitionEnd"},narrowModeChanged:function(){this.classList.toggle("fit-bottom",this.narrowMode);if(this.opened){this.$.overlay.resizeHandler()}},openedChanged:function(){if(this.opened){this.dismissJob=this.job(this.dismissJob,this.dismiss,this.duration)}else{this.dismissJob&&this.dismissJob.stop();this.dismiss()}},toggle:function(){this.opened=!this.opened},show:function(){if(currentToast){currentToast.dismiss()}currentToast=this;this.opened=true},dismiss:function(){if(this.dragging){this.shouldDismiss=true}else{this.opened=false;if(currentToast===this){currentToast=null}}},trackStart:function(e){if(!this.swipeDisabled){e.preventTap();this.vertical=e.yDirection;this.w=this.offsetWidth;this.h=this.offsetHeight;this.dragging=true;this.classList.add("dragging")}},track:function(e){if(this.dragging){var s=this.style;if(this.vertical){var y=e.dy;s.opacity=(this.h-Math.abs(y))/this.h;s.transform=s.webkitTransform="translate3d(0, "+y+"px, 0)"}else{var x=e.dx;s.opacity=(this.w-Math.abs(x))/this.w;s.transform=s.webkitTransform="translate3d("+x+"px, 0, 0)"}}},trackEnd:function(e){if(this.dragging){this.classList.remove("dragging");this.style.opacity="";this.style.transform=this.style.webkitTransform="";var cl=this.classList;if(this.vertical){cl.toggle("fade-out-down",e.yDirection===1&&e.dy>0);cl.toggle("fade-out-up",e.yDirection===-1&&e.dy<0)}else{cl.toggle("fade-out-right",e.xDirection===1&&e.dx>0);cl.toggle("fade-out-left",e.xDirection===-1&&e.dx<0)}this.dragging=false}},transitionEnd:function(){var cl=this.classList;if(cl.contains("fade-out-right")||cl.contains("fade-out-left")||cl.contains("fade-out-down")||cl.contains("fade-out-up")){this.dismiss();cl.remove("fade-out-right","fade-out-left","fade-out-down","fade-out-up")}else if(this.shouldDismiss){this.dismiss()}this.shouldDismiss=false}})})();</script></polymer-element><polymer-element name="paper-dialog-base" extends="core-overlay" role="dialog" on-core-overlay-open="{{openAction}}" assetpath="polymer/bower_components/paper-dialog/"><script>Polymer("paper-dialog-base",{publish:{heading:"",transition:"",layered:tLine truncated
|
||||
({{event.listener_count}} listeners)
|
||||
</div></template></div></template><script>Polymer("events-list",{cbEventClicked:null,events:[],domReady:function(){this.events=this.api.events;this.api.addEventListener("events-updated",this.eventsUpdated.bind(this))},eventsUpdated:function(){this.events=this.api.events},handleClick:function(ev){if(this.cbEventClicked){this.cbEventClicked(ev.path[0].innerHTML)}}});</script></polymer-element><polymer-element name="event-fire-dialog" attributes="api" assetpath="polymer/"><template><paper-action-dialog id="dialog" heading="Fire Event" transition="core-transition-bottom" backdrop=""><style>:host{font-family:RobotoDraft,'Helvetica Neue',Helvetica,Arial}paper-input{display:block}paper-input:first-child{padding-top:0}.eventContainer{margin-left:30px}@media all and (max-width:620px){paper-action-dialog{margin:0;width:100%;height:calc(100% - 64px);top:64px}.eventContainer{display:none}}</style><div layout="" horizontal=""><div><paper-input id="inputType" label="Event Type" floatinglabel="true" autofocus required></paper-input><paper-input-decorator label="Event Data (JSON, optional)" floatinglabel="true"><textarea id="inputData" rows="5"></textarea></paper-input-decorator></div><div class="eventContainer"><b>Available events:</b><events-list api="{{api}}" cbeventclicked="{{eventSelected}}"></events-list></div></div><paper-button dismissive="">Cancel</paper-button><paper-button affirmative="" on-click="{{clickFireEvent}}">Fire Event</paper-button></paper-action-dialog></template><script>Polymer("event-fire-dialog",{ready:function(){this.eventSelected=this.eventSelected.bind(this)},show:function(eventType,eventData){this.setEventType(eventType);this.setEventData(eventData);this.$.dialog.toggle()},setEventType:function(eventType){this.$.inputType.value=eventType},setEventData:function(eventData){this.$.inputData.value=eventData},eventSelected:function(eventType){this.setEventType(eventType)},clickFireEvent:function(){var data;if(this.$.inputData.value!=""){data=JSON.parse(this.$.inputData.value)}else{data={}}this.api.fire_event(this.$.inputType.value,data)}});</script></polymer-element><polymer-element name="paper-dialog-transition" extends="core-transition-css" assetpath="polymer/bower_components/paper-dialog/"><template><style no-shim="">:host(.paper-dialog-transition){outline:0;opacity:0;transition:transform .2s cubic-bezier(0.4,0,.2,1),opacity .2s cubic-bezier(0.4,0,.2,1);-webkit-transition:-webkit-transform .2s cubic-bezier(0.4,0,.2,1),opacity .2s cubic-bezier(0.4,0,.2,1)}:host(.paper-dialog-transition.core-opened){opacity:1;transform:none;-webkit-transform:none}:host(.paper-dialog-transition-bottom){transform:scale(0.9) translateY(200%);-webkit-transform:scale(0.9) translateY(200%)}:host(.paper-dialog-transition-center.core-opened){animation:paper-dialog-transition-center-keyframes .2s cubic-bezier(0.4,0,.2,1);-webkit-animation:paper-dialog-transition-center-keyframes .2s cubic-bezier(0.4,0,.2,1)}@keyframes paper-dialog-transition-center-keyframes{0%{transform:scale(0.5) translateY(0);-webkit-transform:scale(0.5) translateY(0)}90%{transform:scale(1) translateY(-10px);-webkit-transform:scale(1) translateY(-10px)}100%{transform:scale(1) translateY(0);-webkit-transform:scale(1) translateY(0)}}@-webkit-keyframes paper-dialog-transition-center-keyframes{0%{transform:scale(0.5) translateY(0);-webkit-transform:scale(0.5) translateY(0)}90%{transform:scale(1) translateY(-10px);-webkit-transform:scale(1) translateY(-10px)}100%{transform:scale(1) translateY(0);-webkit-transform:scale(1) translateY(0)}}</style></template><script>Polymer("paper-dialog-transition",{baseClass:"paper-dialog-transition"});</script></polymer-element><paper-dialog-transition id="paper-dialog-transition-bottom" transitiontype="bottom"></paper-dialog-transition><paper-dialog-transition id="paper-dialog-transition-center" transitiontype="center"></paper-dialog-transition><polymer-element name="core-item" attributes="label icon src" horizontal="" center="" layout="" assetpath="polymer/bower_components/core-item/"><template><style>:host{display:block;position:relative;min-height:40px;white-space:nowrap}:host(.font-scalable){min-height:2.5em}:host(.core-selected){font-weight:700}#icon{margin:0 16px 0 4px}:host(.font-scalable) #icon{margin:0 1em 0 .25em;height:1.5em;width:1.5em}polyfill-next-selector{content:':host > a'}::content>a{position:absolute;top:0;right:0;bottom:0;left:0;background-color:rgba(0,0,0,.000001)}</style><template if="{{icon || src}}"><core-icon src="{{src}}" id="icon" icon="{{icon}}" hidden?="{{!src && !icon}}"></core-icon></template><div id="label">{{label}}</div><content></content></template><script>Polymer("core-item",{});</script></polymer-element><style shim-shadowdom="">html /deep/ core-collapse{display:block}html /deep/ .core-collapse-closed{display:none}</style><polymer-element name="core-collapse" attributes="target horizontal opened duration fixedSize allowOverflow" assetpath="polymer/bower_components/core-collapse/"Line truncated
|
||||
</div></template></div></template><script>Polymer("events-list",{cbEventClicked:null,events:[],domReady:function(){this.events=this.api.events;this.api.addEventListener("events-updated",this.eventsUpdated.bind(this))},eventsUpdated:function(){this.events=this.api.events},handleClick:function(ev){if(this.cbEventClicked){this.cbEventClicked(ev.path[0].innerHTML)}}});</script></polymer-element><polymer-element name="event-fire-dialog" attributes="api" assetpath="polymer/"><template><paper-action-dialog id="dialog" heading="Fire Event" transition="core-transition-bottom" backdrop=""><style>:host{font-family:RobotoDraft,'Helvetica Neue',Helvetica,Arial}paper-input{display:block}paper-input:first-child{padding-top:0}.eventContainer{margin-left:30px}@media all and (max-width:620px){paper-action-dialog{margin:0;width:100%;height:calc(100% - 64px);top:64px}.eventContainer{display:none}}</style><div layout="" horizontal=""><div><paper-input id="inputType" label="Event Type" floatinglabel="true" autofocus required></paper-input><paper-input-decorator label="Event Data (JSON, optional)" floatinglabel="true"><textarea id="inputData" rows="5"></textarea></paper-input-decorator></div><div class="eventContainer"><b>Available events:</b><events-list api="{{api}}" cbeventclicked="{{eventSelected}}"></events-list></div></div><paper-button dismissive="">Cancel</paper-button><paper-button affirmative="" on-click="{{clickFireEvent}}">Fire Event</paper-button></paper-action-dialog></template><script>Polymer("event-fire-dialog",{ready:function(){this.eventSelected=this.eventSelected.bind(this)},show:function(eventType,eventData){this.setEventType(eventType);this.setEventData(eventData);this.$.dialog.toggle()},setEventType:function(eventType){this.$.inputType.value=eventType},setEventData:function(eventData){this.$.inputData.value=eventData},eventSelected:function(eventType){this.setEventType(eventType)},clickFireEvent:function(){var data;if(this.$.inputData.value!=""){data=JSON.parse(this.$.inputData.value)}else{data={}}this.api.fire_event(this.$.inputType.value,data)}});</script></polymer-element><polymer-element name="paper-dialog-transition" extends="core-transition-css" assetpath="polymer/bower_components/paper-dialog/"><template><style no-shim="">:host(.paper-dialog-transition){outline:0;opacity:0;transition:transform .2s cubic-bezier(0.4,0,.2,1),opacity .2s cubic-bezier(0.4,0,.2,1);-webkit-transition:-webkit-transform .2s cubic-bezier(0.4,0,.2,1),opacity .2s cubic-bezier(0.4,0,.2,1)}:host(.paper-dialog-transition.core-opened){opacity:1;transform:none;-webkit-transform:none}:host(.paper-dialog-transition-bottom){transform:scale(0.9) translateY(200%);-webkit-transform:scale(0.9) translateY(200%)}:host(.paper-dialog-transition-center.core-opened){animation:paper-dialog-transition-center-keyframes .2s cubic-bezier(0.4,0,.2,1);-webkit-animation:paper-dialog-transition-center-keyframes .2s cubic-bezier(0.4,0,.2,1)}@keyframes paper-dialog-transition-center-keyframes{0%{transform:scale(0.5) translateY(0);-webkit-transform:scale(0.5) translateY(0)}90%{transform:scale(1) translateY(-10px);-webkit-transform:scale(1) translateY(-10px)}100%{transform:scale(1) translateY(0);-webkit-transform:scale(1) translateY(0)}}@-webkit-keyframes paper-dialog-transition-center-keyframes{0%{transform:scale(0.5) translateY(0);-webkit-transform:scale(0.5) translateY(0)}90%{transform:scale(1) translateY(-10px);-webkit-transform:scale(1) translateY(-10px)}100%{transform:scale(1) translateY(0);-webkit-transform:scale(1) translateY(0)}}</style></template><script>Polymer("paper-dialog-transition",{baseClass:"paper-dialog-transition"});</script></polymer-element><paper-dialog-transition id="paper-dialog-transition-bottom" transitiontype="bottom"></paper-dialog-transition><paper-dialog-transition id="paper-dialog-transition-center" transitiontype="center"></paper-dialog-transition><polymer-element name="core-item" attributes="label icon src" horizontal="" center="" layout="" assetpath="polymer/bower_components/core-item/"><template><style>:host{display:block;position:relative;min-height:40px;white-space:nowrap}:host(.font-scalable){min-height:2.5em}:host(.core-selected){font-weight:700}#icon{margin:0 16px 0 4px}:host(.font-scalable) #icon{margin:0 1em 0 .25em;height:1.5em;width:1.5em}polyfill-next-selector{content:':host > a'}::content>a{position:absolute;top:0;right:0;bottom:0;left:0;background-color:rgba(0,0,0,.000001)}</style><template if="{{icon || src}}"><core-icon src="{{src}}" id="icon" icon="{{icon}}" hidden?="{{!src && !icon}}"></core-icon></template><div id="label">{{label}}</div><content></content></template><script>Polymer("core-item",{});</script></polymer-element><style shim-shadowdom="">html /deep/ core-collapse{display:block}html /deep/ .core-collapse-closed{display:none}</style><polymer-element name="core-collapse" attributes="target horizontal opened duration fixedSize allowOverflow" assetpath="polymer/bower_components/core-collapse/"Line truncated
|
||||
|
||||
<polymer-element name="splash-login" attributes="auth" assetpath="polymer/"><template><style>:host{font-family:RobotoDraft,'Helvetica Neue',Helvetica,Arial}paper-input{display:block}.login paper-button{margin-left:242px}.login .interact{height:125px}#validatebox{text-align:center}#validatemessage{margin-top:10px}</style><home-assistant-api auth="{{auth}}" id="api"></home-assistant-api><div layout="" horizontal="" center="" fit="" class="login" id="splash"><div layout="" vertical="" center="" flex=""><img src="/static/favicon-192x192.png"><h1>Home Assistant</h1><a href="polymer/#" id="hideKeyboardOnFocus"></a><div class="interact" layout="" vertical=""><div id="loginform"><paper-input-decorator label="Password" id="passwordDecorator"><input is="core-input" type="password" id="passwordInput" value="{{auth}}" on-keyup="{{passwordKeyup}}" autofocus></paper-input-decorator><paper-button on-click="{{validatePassword}}">Log In</paper-button></div><div id="validatebox" hidden><paper-spinner active="true"></paper-spinner><br><div id="validatemessage">Validating password...</div></div></div></div></div><home-assistant-main api="{{api}}" hidden id="main"></home-assistant-main></template><script>Polymer("splash-login",{state:"no_auth",auth:"",ready:function(){this.api=this.$.api},domReady:function(){document.getElementById("init").remove();if(this.auth){this.validatePassword()}},authChanged:function(oldVal,newVal){if(newVal===""&&this.state==="valid_auth"){this.state="no_auth"}},stateChanged:function(oldVal,newVal){if(newVal==="no_auth"){this.$.loginform.removeAttribute("hidden");this.$.validatebox.setAttribute("hidden",null);this.$.validatemessage.innerHTML="Validating password...";this.$.splash.removeAttribute("hidden");this.$.main.setAttribute("hidden",null)}else{this.$.splash.setAttribute("hidden",null);this.$.main.removeAttribute("hidden")}},passwordKeyup:function(ev){if(ev.keyCode===13){this.validatePassword()}else if(this.$.passwordDecorator.isInvalid){this.$.passwordDecorator.isInvalid=false}},validatePassword:function(){this.$.loginform.setAttribute("hidden",null);this.$.validatebox.removeAttribute("hidden");this.$.hideKeyboardOnFocus.focus();var passwordValid=function(result){this.$.validatemessage.innerHTML="Loading data...";this.api.fetchEvents();this.api.fetchStates(function(){this.state="valid_auth"}.bind(this))};var passwordInvalid=function(result){if(result&&result.message){this.$.passwordDecorator.error=result.message}else{this.$.passwordDecorator.error="Unexpected result from API"}this.auth=null;this.$.passwordDecorator.isInvalid=true;this.$.loginform.removeAttribute("hidden");this.$.validatebox.setAttribute("hidden",null);this.$.passwordInput.focus()};this.api.fetchServices(passwordValid.bind(this),passwordInvalid.bind(this))}});</script></polymer-element>
|
||||
@@ -206,11 +206,18 @@
|
||||
},
|
||||
|
||||
turn_on: function(entity_id) {
|
||||
this.call_service("homeassistant", "turn_on", {entity_id: entity_id});
|
||||
// we call the turn_on method on the domain of the entity_id
|
||||
// because the call to homeassistant.turn_on does not wait
|
||||
// till the call is done.
|
||||
var parts = entity_id.split(".");
|
||||
|
||||
this.call_service(parts[0], "turn_on", {entity_id: entity_id});
|
||||
},
|
||||
|
||||
turn_off: function(entity_id) {
|
||||
this.call_service("homeassistant", "turn_off", {entity_id: entity_id});
|
||||
var parts = entity_id.split(".");
|
||||
|
||||
this.call_service(parts[0], "turn_off", {entity_id: entity_id});
|
||||
},
|
||||
|
||||
set_state: function(entity_id, state, attributes) {
|
||||
@@ -243,23 +250,7 @@
|
||||
|
||||
// if we call a service on an entity_id, update the state
|
||||
if(parameters && parameters.entity_id) {
|
||||
var update_func;
|
||||
|
||||
// if entity_id is a string, update 1 state, else all.
|
||||
if(typeof(parameters.entity_id === "string")) {
|
||||
// if it is a group, fetch all
|
||||
if(parameters.entity_id.slice(0,6) == "group.") {
|
||||
update_func = this.fetchStates;
|
||||
} else {
|
||||
update_func = function() {
|
||||
this.fetchState(parameters.entity_id);
|
||||
};
|
||||
}
|
||||
} else {
|
||||
update_func = this.fetchStates;
|
||||
}
|
||||
|
||||
setTimeout(update_func.bind(this), 1000);
|
||||
this.fetchStates();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -20,7 +20,8 @@ EVENT_HOMEASSISTANT_START = "homeassistant_start"
|
||||
EVENT_HOMEASSISTANT_STOP = "homeassistant_stop"
|
||||
EVENT_STATE_CHANGED = "state_changed"
|
||||
EVENT_TIME_CHANGED = "time_changed"
|
||||
EVENT_CALL_SERVICE = "services.call"
|
||||
EVENT_CALL_SERVICE = "call_service"
|
||||
EVENT_SERVICE_EXECUTED = "service_executed"
|
||||
|
||||
# #### STATES ####
|
||||
STATE_ON = 'on'
|
||||
@@ -28,7 +29,7 @@ STATE_OFF = 'off'
|
||||
STATE_HOME = 'home'
|
||||
STATE_NOT_HOME = 'not_home'
|
||||
|
||||
# #### STATE ATTRIBUTES ####
|
||||
# #### STATE AND EVENT ATTRIBUTES ####
|
||||
# Contains current time for a TIME_CHANGED event
|
||||
ATTR_NOW = "now"
|
||||
|
||||
@@ -36,6 +37,10 @@ ATTR_NOW = "now"
|
||||
ATTR_DOMAIN = "domain"
|
||||
ATTR_SERVICE = "service"
|
||||
|
||||
# Data for a SERVICE_EXECUTED event
|
||||
ATTR_SERVICE_CALL_ID = "service_call_id"
|
||||
ATTR_RESULT = "result"
|
||||
|
||||
# Contains one string or a list of strings, each being an entity id
|
||||
ATTR_ENTITY_ID = 'entity_id'
|
||||
|
||||
|
||||
Reference in new issue
Block a user