jpmens / homie-ota

OTA "server" in Python for Homie
127 stars 50 forks source link

reset option in device page #82

Closed migueltercero closed 4 years ago

migueltercero commented 4 years ago

Hi, can you add reset option in device.tpl?

Homie implements reset option to delete all configuration for a device:

https://homieiot.github.io/homie-esp8266/docs/develop-v3/others/homie-implementation-specifics/

$implementation/reset: You can publish a true to this topic to reset the device

Thank you!!!

PD: I have implemented it, here is the code:

homie-ota.py:

# Handle deleting a device from the mqtt broker, and the local db.
@route('/device/<device_id>', method='PATCH')
@conditional_decorator('HTTP_USER' in globals() and 'HTTP_PASSWORD' in globals(), auth_basic(check))
def reset_device(device_id):
    topic = "%s/%s/$implementation/reset" % (MQTT_SENSOR_PREFIX, device_id)
    mqttc.publish(topic, payload='true', qos=1, retain=False)
    logging.debug("Reset device: {}".format(device_id))

    info = "reset configuration for %s" % (device_id)
    logging.info(info)

    del db[device_id]
    del sensors[device_id]

    return info

device.tpl

... [<a class="reset" data-reset-url="{{base_url}}/device/{{device}}" href="#">Reset</a>] ..
$('.reset').bind('click', function (e){
  e.preventDefault();
  if (confirm("Are you sure to reset this device?")) {
    $.ajax({
      url: $(this).data('reset-url'),
      type: 'PATCH',
      async: true
    })
    .done(function() {
      alert('Reseted device');
      window.location.href = '{{base_url}}/';
    })
    .fail(function(e) {
      alert('Error: ' + e.statusText);
    })
  }
  return false;
})`