allow Python Events a bit more functionnalities

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

Post Reply
Refhi
Posts: 3
Joined: Monday 24 December 2018 20:15
Target OS: Linux
Domoticz version:
Contact:

allow Python Events a bit more functionnalities

Post by Refhi »

Hello,

I think Python Events "DE,command" could benefit from a bit more option:
- allow numeric arguments instead of just "On" or "Off"
- and/or allow http commands to be send

I believe it would increase tenfold the benefits of Python Events :)

For more insight : I'm trying to make an Event with some fine-tuning and Blocky refusing the nested ifs and Lua being an unknown language (for me of course as I'm only "fluent" in Python), I was trying to use Python. I'm almost at the point where I can make everything works, but I need to control a selector switch which DE,command won't be able to set except for On/Off.
Meanwhile I'm checking how to make a Python Plugin which might (or not) fill the gap.
Refhi
Posts: 3
Joined: Monday 24 December 2018 20:15
Target OS: Linux
Domoticz version:
Contact:

Re: allow Python Events a bit more functionnalities

Post by Refhi »

Ok, so "python plugins" would not really fit my needs.

So I've made a workaround using the Json http domoticz API with the requests python module.

I managed to install the requests module inside the Domoticz docker container with the following

Code: Select all

customstart,sh
set in the Domoticz folder :

Code: Select all

#!/bin/bash

if [ -f /opt/domoticz/FIRSTRUN ]; then
true
else
echo 'installing requests et urllib3'
pip install requests
pip install urllib3
echo 'creating FIRSTRUN file so script can check on next run'
touch /opt/domoticz/FIRSTRUN
echo 'copying modules to proper folder'
cp -r /usr/local/lib/python3,9/dist-packages/* /usr/lib/python3,9

fi
=> note that the folder where the module needs to be is not the default one.

Then made the following python event script :

Code: Select all

import DomoticzEvents as DE

# Needed for DE to be accessed even inside functions (!)
global DE

DE,Log("Python: Changed: " + str(DE,changed_device,id))

def set_asked_device_to(idx, lvl):
# used instead of DE,Command("Devicename","On/Off") if more granularity needed
# requests import in the function to make sure it's only called when needed
    import requests
    query = "http://localhost:8080/json,htm?type=command&param=switchlight&idx={idx}&switchcmd=Set%20Level&level={lvl}"
    query = query,format(idx=idx, lvl=lvl)
    DE,Log(f"url demandée = {query}")
    try:  # vérifier que domoticz répond, sinon laisser tomber
        requests,get(query)
    except Exception as e:
        DE,Log(f"c'est pas l'heure {e}")


if DE,changed_device,id == 131:
    DE,Log(f"device changed have idx {DE,changed_device,id}")
    set_asked_device_to(idx=118,lvl=30)
I guess it might not be a very clean workaround ? But it fits my needs for now.

[Edit : why won't the forum let me use dots in the code ? I've changed them all to comas... sorry if you have to copy that, you'll have to change them to "." as needed]
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest