Skip to content

Commit

Permalink
Merge pull request #22 from tobias-richter/feature/fix_json_handling
Browse files Browse the repository at this point in the history
Feature/fix json handling
  • Loading branch information
tobias-richter authored Mar 3, 2021
2 parents 61be036 + 4ee5503 commit e59770f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ e.g.
# make sure that TuyaMCU fnId is disabled or missing
- command: TuyaMCU
value: 11,0

# Configure Timer16 to trigger a Rule once a day at 06:00 (+-0:05) to restart the device
- command: Timer16
value: '{"Enable":1,"Time":"06:00","Window":5,"Days":"1111111","Repeat":1,"Output":1,"Action":3, "Mode":0}'
- command: Rule3
value: "on Clock#Timer=16 do Restart 1 endon"
- command: Rule3
value: 1
- command: Timers
value: 1

# Example for no_log
- command: MqttPassword
Expand Down
15 changes: 13 additions & 2 deletions action_plugins/tasmota.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
__metaclass__ = type

import requests
import re
import json
import sys
import copy
Expand Down Expand Up @@ -150,6 +151,10 @@ def run(self, tmp=None, task_vars=None):
existing_value = next(iter(gpios))
elif (command == 'Template'):
existing_value = data
elif (command.startswith('Timers')):
existing_value = self._translateResultStr(data.get('Timers'))
elif (re.findall('Timer\d', command)):
existing_value = data.get(command)
elif (command == 'TimeStd' or command == 'TimeDst' ):
display.vv("TimeStd/TimeDst found!")
existing_data = data.get(command)
Expand Down Expand Up @@ -193,15 +198,21 @@ def our_entry(x):
except Exception as e:
raise AnsibleRuntimeError("Invalid response payload: %s, error: %s" % (data, e))

display.v("[%s] command: %s, existing_value: '%s', incoming_value: '%s'" % (tasmota_host, command, existing_value, incoming_value if not no_log else ""))
display.v("[%s] command: %s,\n\t existing_value: '%s',\n\t incoming_value: '%s'" % (tasmota_host, command, existing_value, incoming_value if not no_log else ""))

display.v("[%s] existing_uri: %s" % (tasmota_host, endpoint_uri))

if existing_value != incoming_value:
changed = True

if not check_mode:
change_params = copy.deepcopy(auth_params)
change_params.update( { 'cmnd' : ("%s %s" % (command, incoming_value)) } )
# encode json if required
if isinstance(incoming_value, dict):
change_params.update( { 'cmnd' : ("%s %s" % (command, json.dumps(incoming_value))) } )
else:
change_params.update( { 'cmnd' : ("%s %s" % (command, incoming_value)) } )

change_response = requests.get(url = endpoint_uri, params = change_params)
if status_response.status_code != 200:
raise AnsibleRuntimeError("Unexpected response code: %s" % (status_response.status_code))
Expand Down

0 comments on commit e59770f

Please sign in to comment.