Skip to content

Commit

Permalink
Merge pull request #3 from llagendijk/add_authentication
Browse files Browse the repository at this point in the history
Add authentication
  • Loading branch information
tobias-richter authored Mar 9, 2020
2 parents f2c04b4 + 6c3dff8 commit 12114bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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: ''
tasmota_password: ''
tasmota_commands: []

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:

- command: <COMMAND>
Expand Down
18 changes: 14 additions & 4 deletions action_plugins/tasmota.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 12114bd

Please sign in to comment.