From 3aeb1244167cf577f6b44425b1cfbaa083ba0e8f Mon Sep 17 00:00:00 2001 From: louis-local Date: Tue, 3 Mar 2020 16:44:19 +0100 Subject: [PATCH 1/3] Added authentication for the web commands --- README.md | 12 ++++++++---- action_plugins/tasmota.py | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 57e3fa7..14f4141 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ This role allows you to configure tasmota devices by executing commands. :bulb: See https://tasmota.github.io/docs/#/Commands for a command list. -This role/action_plugin will do the following steps for each provided `command`,`value` pair in the `tasmota_commands`: +This role/action_plugin will send commands to a tasmota device using web requests. +It will perform the following steps for each provided `command`,`value` pair in the `tasmota_commands`: * It will retrieve the current setting of the provided `command` * It will compare the result of this with the incoming `value` * when the new value differs from the existing value the command is executed with the new value and the task will report with `changed` @@ -36,10 +37,13 @@ This may cause wrong reported "changed" states. You are welcome to create a PR f Available variables are listed below, along with their default values: - tasmota_commands: [] - -A list of tasmota commands to execute. + tasmota_user: username + tasmota_password: password + tasmota_commands: [] + +If tasmota_user and tasmota password are both defined, they will be included in the commands to authenticate access. +Tasmota commands contains list of tasmota commands to be executed. Each tasmota_command is defined as: - command: diff --git a/action_plugins/tasmota.py b/action_plugins/tasmota.py index 7add33c..00a6188 100644 --- a/action_plugins/tasmota.py +++ b/action_plugins/tasmota.py @@ -8,6 +8,7 @@ import requests import json import sys +import copy from ansible.module_utils._text import to_native from ansible.plugins.action import ActionBase @@ -66,8 +67,18 @@ def run(self, tmp=None, task_vars=None): display.v("got an exception: "+err.message) return self._fail_result(result, "error during retrieving parameter '%s'" % (err.message)) + auth_params = {} + try: + user = self._get_arg_or_var("tasmota_user") + password = self._get_arg_or_var('tasmota_password') + auth_params = { 'user' : user, 'password' : password } + display.v("authentication parameters: %s" % (auth_params)) + except: + pass + endpoint_uri = "http://%s/cm" % (tasmota_host) - status_params = {'cmnd' : command } + status_params = copy.deepcopy(auth_params) + status_params.update( {'cmnd' : command } ) # execute command status_response = requests.get(url = endpoint_uri, params = status_params) @@ -98,12 +109,11 @@ def run(self, tmp=None, task_vars=None): display.v("[%s] command: %s, existing_value: '%s', incoming_value: '%s'" % (tasmota_host, command, existing_value, incoming_value)) - - display.v("[%s] existing_uri: %s" % (tasmota_host, endpoint_uri)) if existing_value != incoming_value: changed = True - change_params = { 'cmnd' : ("%s %s" % (command, incoming_value)) } + change_params = copy.deepcopy(auth_params) + change_params.update( { 'cmnd' : ("%s %s" % (command, incoming_value)) } ) change_response = requests.get(url = endpoint_uri, params = change_params) result["changed"] = changed From 230e0cf123896f220aa5068e5265150456bfd9cb Mon Sep 17 00:00:00 2001 From: louis-local Date: Tue, 3 Mar 2020 16:48:18 +0100 Subject: [PATCH 2/3] Fix formatting of README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 14f4141..a90c3af 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,8 @@ This may cause wrong reported "changed" states. You are welcome to create a PR f Available variables are listed below, along with their default values: tasmota_user: username - tasmota_password: password - tasmota_commands: [] + tasmota_password: password + tasmota_commands: [] If tasmota_user and tasmota password are both defined, they will be included in the commands to authenticate access. From 6c3dff8aa23f3cbc1f42da90d6a5b749547c8b27 Mon Sep 17 00:00:00 2001 From: louis-local Date: Tue, 3 Mar 2020 16:50:18 +0100 Subject: [PATCH 3/3] Clarify that user/password are default empty --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a90c3af..bd090ba 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,11 @@ This may cause wrong reported "changed" states. You are welcome to create a PR f Available variables are listed below, along with their default values: - tasmota_user: username - tasmota_password: password + tasmota_user: '' + tasmota_password: '' tasmota_commands: [] -If tasmota_user and tasmota password are both defined, they will be included in the commands to authenticate access. +If tasmota_user and tasmota password are both non empty, they will be included in the commands to authenticate access. Tasmota commands contains list of tasmota commands to be executed. Each tasmota_command is defined as: