Programmation python

Posez ici vos questions d'utilisation, de configuration de DomoticZ, de bugs, de conseils sur le logiciel lui même dans son utilisation et son paramétrage.
Des forums spécifiques sont ouverts ci-dessous pour regrouper les différents sujets.
Benito13
Messages : 6
Inscription : 26 févr. 2022, 09:58

Programmation python

Message par Benito13 »

Bonjour,

Problème lors de la programmation en python via domoticz même, le script en python donné par domoticz ne fonctionne pas et donne ces messages d'erreurs :

Code : Tout sélectionner

2022-03-08 15:18:17.522 Status: EventSystem: reset all events...
2022-03-08 15:18:17.538 Error: Pyhton Event System: Failed to parse parameters: string expected.
2022-03-08 15:18:17.538 Error: EventSystem: Failed to execute python event script "Original"
2022-03-08 15:18:17.538 Error: EventSystem: Traceback (most recent call last):
2022-03-08 15:18:17.538 Error: EventSystem: File "C:\Program Files (x86)\Domoticz\scripts\python\domoticz.py", line 41, in <module>
2022-03-08 15:18:17.538 Error: EventSystem: Devices
2022-03-08 15:18:17.538 Error: EventSystem: NameError: name 'Devices' is not defined
2022-03-08 15:18:17.538 Error: EventSystem:
2022-03-08 15:18:17.538 Error: EventSystem: During handling of the above exception, another exception occurred:
2022-03-08 15:18:17.538 Error: EventSystem:
2022-03-08 15:18:17.538 Error: EventSystem: TypeError: function takes exactly 1 argument (2 given)
2022-03-08 15:18:17.538 Error: EventSystem:
2022-03-08 15:18:17.538 Error: EventSystem: The above exception was the direct cause of the following exception:
2022-03-08 15:18:17.538 Error: EventSystem:
2022-03-08 15:18:17.538 Error: EventSystem: Traceback (most recent call last):
2022-03-08 15:18:17.538 Error: EventSystem: File "<string>", line 63, in <module>
2022-03-08 15:18:17.538 Error: EventSystem: File "C:\Program Files (x86)\Domoticz\scripts\python\domoticz.py", line 44, in <module>
2022-03-08 15:18:17.538 Error: EventSystem: domoticz_.Log(1, "Devices was not created by Domoticz C++ code")
2022-03-08 15:18:17.538 Error: EventSystem: SystemError: <built-in function Log> returned a result with an error set 
Programme en question :

Code : Tout sélectionner

import domoticz
import random

print ("This will only show up in the shell where you start domoticz");
domoticz.log("Hi Domoticz!")

domoticz.log("The device that got changed is: ", changed_device_name)
# changed_device.name is the same string

for name, device in domoticz.devices.items():
  domoticz.log("device", name, "is", "on" if device.is_on() else "off")
  if device.is_off():
      domoticz.log("could turn it on in 3 seconds")
      #device.on(after=3)
for name, value in user_variables.items():
  domoticz.log("var", name, "has value", value)
et le fichier et lignes en question :

Code : Tout sélectionner

"""
Helper module for python+domoticz
Every script_device_*.py script has the following variables
 * changed_device: the current device that changed (object of Device)
 * changed_device_name: name of current device (same as changed_device.name)
 * is_daytime: boolean, true when it is is daytime
 * is_nighttime: same for the night
 * sunrise_in_minutes: integer
 * sunset_in_minutes: integer
 * user_variables: dictionary from string to value

For more advanced project, you may want to use modules.
say you have a file heating.py
You can do
import heating
heating.check_heating()

But heating has a different scope, and won't have access to these variables.
By importing this module, you can have access to them in a standard way
import domoticz
domoticz.changed_device

And you also have access to more:
 * same as in the device scripts (changed_device ... user_variables)
 * devices: dictionary from string to Device


"""
#try:
import DomoticzEvents as domoticz_ #
#except:
#    pass

import reloader
import datetime
import re

# Enable plugins to do:
# from Domoticz import Devices, Images, Parameters, Settings
try:
    Devices #(Ligne 41) 
except NameError:
    Devices = {}
    domoticz_.Log(1, "Devices was not created by Domoticz C++ code") #(Ligne 44)

try:
    Images
except NameError:
    Images = {}
    domoticz_.Log(1, "Images was not created by Domoticz C++ code")

try:
    Parameters
except NameError:
    Parameters = {}
    domoticz_.Log(1, "Parameters was not created by Domoticz C++ code")

try:
    Settings
except NameError:
    Settings = {}
    domoticz_.Log(1, "Settings was not created by Domoticz C++ code")

#def _log(type, text): # 'virtual' function, will be modified to call
#	pass

def log(*args):
    domoticz_.Log(0, " ".join([str(k) for k in args]))

def error(*args):
    domoticz_.Log(1, " ".join([str(k) for k in args]))

reloader.auto_reload(__name__)

# this will be filled in by the c++ part
devices = {}
device = None


testing = False
commands = []

event_system = None # will be filled in by domoticz
def command(name, action, file):
    if testing:
        commands.append((name, action))
    else:
        event_system.command(name, action, file)

def process_commands():
    for name, action in commands:
        device = devices[name]
        if action == "On":
            device.n_value = 1
            print ("\tsetting %s On" % name)
        elif action == "Off":
            device.n_value = 0
            print ("\tsetting %s On" % name)
        else:
            print ("unknown command", name, action)
    del commands[:]

class Device(object):
    def __init__(self, id, name, type, sub_type, switch_type, n_value, n_value_string, s_value, last_update):
        devices[name] = self
        self.id = id
        self.name = name
        self.type = type
        self.sub_type = sub_type
        self.switch_type = switch_type
        self.n_value = n_value
        self.n_value_string = n_value_string
        self.s_value = s_value
        self.last_update_string = last_update
        # from http://stackoverflow.com/questions/127803/how-to-parse-iso-formatted-date-in-python
        if self.last_update_string:
            # from http://stackoverflow.com/questions/127803/how-to-parse-iso-formatted-date-in-python
            try:
                self.last_update = datetime.datetime(*map(int, re.split('[^\d]', self.last_update_string)[:]))
                if str(self.last_update_string) != str(self.last_update):
                    log("Error parsing date:", self.last_update_string, " parsed as",  str(self.last_update))
            except:
                log("Exception while parsing date:", self.last_update_string)
        else:
            self.last_update = None

        def __str__(self):
            return "Device(%s, %s, %s, %s)" % (self.id, self.name, self.n_value, self.s_value)
        def __repr__(self):
            return "Device(%s, %s, %s, %s)" % (self.id, self.name, self.n_value, self.s_value)

    def last_update_was_ago(self, **kwargs):
        """Arguments can be: days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]"""
        return self.last_update + datetime.deltatime(**kwargs) < datetime.datetime.now()

    def is_on(self):
        return self.n_value == 1

    def is_off(self):
        return self.n_value == 0

    def on(self, after=None, reflect=False):
        if self.is_off() or after is not None:
            self._command("On" , after=after)
        if reflect:
            self.n_value = 1

    def off(self, after=None, reflect=False):
        if self.is_on() or after is not None:
            self._command("Off" , after=after)
        if reflect:
            self.n_value = 0

    def _command(self, action, after=None):
        if after is not None:
            command(self.name, "%s AFTER %d" % (action, after), __file__)
        else:
            command(self.name, action, __file__)
Keros
Messages : 6638
Inscription : 23 juil. 2019, 20:57

Re: Programmation python

Message par Keros »

C'est la suite de viewtopic.php?f=8&t=12175 où c'est un nouveau sujet ?
Comment bien utiliser le forum : Poser une question, Mettre un script, un fichier, une image ou des logs
Mes petits guides : Débuter en programmation, Le débogage, Le choix de matériel, Les sauvegardes
Ma présentation - Mes Tutos
denis_brasseur
Messages : 898
Inscription : 24 déc. 2018, 17:05
Localisation : (26)

Re: Programmation python

Message par denis_brasseur »

Si c'est du python, il ne manquerait pas la balise ci dessous ?

Code : Tout sélectionner

#!/usr/bin/env python
Pi3 + DD PiDrive - RFXtrx433 - AEON Labs ZW090
Répondre