Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of various types of values #26

Merged
merged 4 commits into from
Jan 1, 2022

Conversation

treitmayr
Copy link
Contributor

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.

Without this fix FriendlyName1 cannot be cleared, for example.
The original code converted all values to strings while subsequent
code is present for differentiating between dict and other value types,
which was meaningless. Now when keeping the original value type,
a dict can be written out correctly again.
@treitmayr treitmayr changed the title Fix handling of various types of value Fix handling of various types of values Dec 11, 2021
@tobias-richter
Copy link
Owner

tobias-richter commented Dec 20, 2021

@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: 988f690

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
Copy link
Contributor Author

@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
Copy link
Owner

@treitmayr no problem, take your time!

@treitmayr
Copy link
Contributor Author

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
Copy link
Owner

@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.

@tobias-richter tobias-richter merged commit f62574d into tobias-richter:master Jan 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants