tobias-richter / ansible-tasmota

Ansible Role for managing tasmota devices with tasmota commands
Apache License 2.0
31 stars 13 forks source link

Fix handling of various types of values #26

Closed treitmayr closed 2 years ago

treitmayr commented 2 years ago

I wanted to configure my new set of rebranded Gosund SP111 and stumbled across two issues which this PR shall fix.

  1. Empty strings could not be written. Example: Clearing FriendlyNameX (X e [1;8]) The issue here was that just the command name and a single space separating the command from its value were sent. The empty value however needs to be specified as an empty pair of quotes. -> Added special handling for empty string values.
  2. Dict values were not handled as intended by the code. While there is special handling for Dicts already in place, this was never triggered because _get_arg_or_var() always converted its return value to a string. The symptom was that this string was no proper JSON line (mainly single quotes instead of double quotes) therefore I was not able to configure the tasmota Template.
  3. Fixed the parsing of the existing values of rules. There seem to have been some variables mixed up, so that the resulting existing values were always None. That seems to work now reliably.

With the changes my own playbooks work but I did not test some of the very specific commands not applicable to my devices (or at least to my setup). So please check if the changes may break things in different areas.

tobias-richter commented 2 years ago

@treitmayr thank you very much for your contribution. I have tested your change with my setup and I have problems with the changes you added for the rules:

See: https://github.com/tobias-richter/ansible-tasmota/pull/26/commits/988f690a49351bb302c6be1eb93617f0846ac731

Rule config

    - command: Rule1
      value: "on Power1#state=0 do TelePeriod {{ tasmota_teleperiod_off }} endon on Power1#state=1 do TelePeriod {{ tasmota_teleperiod_default }} endon on Energy#Power>10 do TelePeriod 1 endon on Energy#Power<10 do TelePeriod {{ tasmota_teleperiod_default }} endon"
    # enable Rule1
    - command: Rule1
      value: 1
      # Enable one-shot detection for Rule1
    - command: Rule1
      value: 5

I am running your change against a "Tasmota 9.1.0" device and this is the incoming existing data:

data: {'Rule1': 'ON', 'Once': 'ON', 'StopOnError': 'OFF', 'Length': 166, 'Free': 345, 'Rules': 'on Power1#state=0 do TelePeriod 1800 endon on Power1#state=1 do TelePeriod 60 endon on Energy#Power>10 do TelePeriod 1 endon on Energy#Power<10 do TelePeriod 60 endon'}

I am getting the following error:

The full traceback is:
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/ansible/executor/task_executor.py", line 113, in run
    item_results = self._run_loop(items)
  File "/usr/local/lib/python3.8/dist-packages/ansible/executor/task_executor.py", line 367, in _run_loop
    res = self._execute(variables=task_vars)
  File "/usr/local/lib/python3.8/dist-packages/ansible/executor/task_executor.py", line 665, in _execute
    result = self._handler.run(task_vars=variables)
  File "/home/vagrant/ansible/roles/tobias_richter.tasmota/action_plugins/tasmota.py", line 130, in run
    existing_state = existing_rule.get("State")
AttributeError: 'str' object has no attribute 'get'
fatal: [socket025.sangrail.net]: FAILED! => {
    "msg": "Unexpected failure during module execution.",
    "stdout": ""
}

I assume that you are running the role aginst a different tasmota version and the response for the existing data has changed.

treitmayr commented 2 years ago

@tobias-richter you are right, I tested against 10.0.0, and there the output on the console looks like

21:21:44.947 CMD: rule1
21:21:44.953 RSL: RESULT = {"Rule1":{"State":"OFF","Once":"OFF","StopOnError":"OFF","Length":0,"Free":511,"Rules":""}}

I will downgrade one of my nodes (hopefully that can be done) and fix the commit by auto-detecting if the original format or the new one is being used. Not sure if I find the time before xmas though.

tobias-richter commented 2 years ago

@treitmayr no problem, take your time!

treitmayr commented 2 years ago

The current version should now work for both rule responses (tasmota versions < 10.0.0 vs. >= 10.0.0), I tested it with 9.1.0 and 10.1.0. Please give it a try and check if the changes are fine for you.

tobias-richter commented 2 years ago

@treitmayr happy new year and thank you for the Update. I have successfully tested the new version with 10.1.0 and 9.1.0 devices. I will release a new version soon.