Source: control-onoff.js

/**
  * Construct a new On/Off control.
  *
  * @class
  * @param {Home} home the home that this control belongs to.
  * @param {String} id the id of this control. Updates will be POSTed to
  *                    <code>home.control_endpoint/id</code>
  * @param {Object} options options for this control:
  * <ul>
  *   <li>
  *     <code>name</code> - String, required - friendly name for this control
  *   </li>
  *   <li>
  *     <code>on</code> - boolean, default: false - whether the control is
  *     already "on" or not
  *   </li>
  *   <li>
  *     <code>on-label</code> - String, default: "ON" - label to display when
  *     the control is on.
  *   </li>
  *   <li>
  *     <code>off-label</code> - String, default: "OFF" - label to display when
  *     the control is off.
  *   </li>
  * </ul>
  */
function OnOffControl(home, id, options) {
  "use strict";
  this.home = home;
  this.id = id;
  this.options = options;
  var template_source = $("#control-onoff-template").html();
  this.template = Handlebars.compile(template_source);
}

OnOffControl.prototype.render = function() {
  "use strict";
  this.html = $(this.template(this.options));
  this.switch = this.html.find(".make-switch").bootstrapSwitch();
  if (this.options.on) {
    this.switch.bootstrapSwitch("setState", true);
  }
  if (this.options["on-label"]) {
    this.switch.bootstrapSwitch("setOnLabel", this.options["on-label"]);
  }
  if (this.options["off-label"]) {
    this.switch.bootstrapSwitch("setOffLabel", this.options["off-label"]);
  }
  var thiz = this;
  this.switch.on("switch-change", function(e, data) {
    var on = data.value;
    $.post(thiz.home.options.control_endpoint + thiz.id, {"on": on});
  });
  return this.html;
};

Home.register_control("on-off", OnOffControl);