FHEM Forum

FHEM => Anfängerfragen => Thema gestartet von: Wolfgang Hochweller am 08 Oktober 2017, 18:00:58

Titel: Python Script
Beitrag von: Wolfgang Hochweller am 08 Oktober 2017, 18:00:58
Ich dachte, ich haette es verstanden, sieht aber nicht so aus.
Ich moechte ein Pythonscript ausfuehren und das Ergebnis weiter verwenden.
Aus einem Terminal :
python /opt/fhem/python/verisure_getAlarm.py

gibt mir das Ergebnis : DISARMED
Sehr schoen !

Aus FHEM heraus :
VerisureAlarm:.* {
my $alarm = qx (python /opt/fhem/python/verisure_getAlarm.py);;
fhem ( "setstate VerisureAlarm $alarm" );;
}

Das qx Kommando weigert sich.
Titel: Antw:Python Script
Beitrag von: amenomade am 08 Oktober 2017, 18:09:21
Was sagt die Log?
Titel: Antw:Python Script
Beitrag von: Wolfgang Hochweller am 08 Oktober 2017, 18:26:11
Vielleicht noch wichtig :
Mein FHEM laeuft jetzt auf Ubuntu.
Vorher hatte ich alles unter Windows und ich meine, da hat das funktioniert.

Das Log zeigt mir folgendes :

  File "/opt/fhem/python/verisure_getAlarm.py", line 1, in <module>
    import verisure
  File "/usr/local/lib/python2.7/dist-packages/verisure/__init__.py", line 13, in <module>
    from .session import ( # NOQA
  File "/usr/local/lib/python2.7/dist-packages/verisure/session.py", line 7, in <module>
    import requests
  File "/usr/local/lib/python2.7/dist-packages/requests/__init__.py", line 97, in <module>
    from . import utils
  File "/usr/local/lib/python2.7/dist-packages/requests/utils.py", line 42, in <module>
    if platform.system() == 'Windows':
  File "/usr/lib/python2.7/platform.py", line 1286, in system
    return uname()[0]
  File "/usr/lib/python2.7/platform.py", line 1253, in uname
    processor = _syscmd_uname('-p','')
  File "/usr/lib/python2.7/platform.py", line 988, in _syscmd_uname
    rc = f.close()
IOError: [Errno 10] No child processes

Titel: Antw:Python Script
Beitrag von: amenomade am 08 Oktober 2017, 18:56:06
Ich meine die Logfile von Fhem.
Titel: Antw:Python Script
Beitrag von: Otto123 am 08 Oktober 2017, 19:12:52
Hi,

gib mal bitte ein komplettes list vom notify in code tags -> die # Taste über dem  :-X Smily

Sieht ansonsten eigentlich gut aus -> https://forum.fhem.de/index.php?topic=59291.0

Gruß Otto
Titel: Antw:Python Script
Beitrag von: Wolfgang Hochweller am 08 Oktober 2017, 20:46:25
Bin etwas unsicher , was ihr meint :
Der Ausschnitt oben ist der Teil des Logfiles, der entsteht, wenn ich das Notify triggere.

Das Notify :


VerisureAlarm:.* {
my $alarm = qx (python /opt/fhem/python/verisure_getAlarm.py);;
fhem ( "setstate VerisureAlarm $alarm" );;
}


Das Pythonscript :



import verisure

session = verisure.Session('user', 'pass')
session.login()
armstate = session.get_arm_state()
session.logout()
print(armstate["statusType"])

Titel: Antw:Python Script
Beitrag von: andies am 08 Oktober 2017, 20:50:01
Führe mal python innerhalb von FHEM als sudo aus (ich hoffe, das geht), wer weiß, wie da die Rechte gesetzt sind. Also statt
python DeinProgramm.py
besser
sudo python DeinProgramm.py
Titel: Antw:Python Script
Beitrag von: Otto123 am 08 Oktober 2017, 20:51:52
ich meine das Ergebnis von list <name des notify> komplett. Das was Du schreibst ist nur die DEF.

Gruß Otto
Titel: Antw:Python Script
Beitrag von: amenomade am 08 Oktober 2017, 20:58:30
Und die Logfile von FHEM sieht so aus:
2017.10.01 19:16:45 1: PERL WARNING: Terminating on signal SIGINT(2)
2017.10.02 23:40:08 1: starting in console mode
2017.10.02 23:40:08 1: Including fhem.cfg
2017.10.02 23:40:08 3: telnetPort: port 7072 opened
2017.10.02 23:40:09 3: WEB: port 8083 opened
2017.10.02 23:40:09 3: WEBphone: port 8084 opened
2017.10.02 23:40:09 3: WEBtablet: port 8085 opened
2017.10.02 23:40:09 2: eventTypes: loaded 240 events from ./log/eventTypes.txt

Mit Datum, Uhrzeit, Loglevel, usw.
Titel: Antw:Python Script
Beitrag von: Wolfgang Hochweller am 08 Oktober 2017, 22:17:41
Hhmmm, Rechte ?
Ja, kann natuerlich sein, habe ich mich nach der Installation nie drum gekuemmert.
Der FHEM-Server laeuft unter dem USER fhem.
Alles, was ich tue, tue ich unter meinem User.
Also, z. B
. die Verisure Routinen sind unter meinem User angelegt, wie auch alles andere, was  sonst so erledigt werden muss.

Titel: Antw:Python Script
Beitrag von: amenomade am 08 Oktober 2017, 22:30:42
Wegen Rechte, kannst Du einfach { qx(python -c print) } im Kommandofeld von FHEM eingeben. Wenn nix in der Logfile von Fhem (wenn Du die gefunden hast) kommt, dann kannst Du python von fhem aus ausführen. Auch mit dem echten script kannst Du probieren:
{ qx (python /opt/fhem/python/verisure_getAlarm.py);; }

sollte was in der Log oder in der Weboberfläsche ausgeben. Entweder eine Fehlermeldung oder den Wert, den Du willst.
Titel: Antw:Python Script
Beitrag von: Wolfgang Hochweller am 08 Oktober 2017, 22:51:14
Das habe ich schon probiert , Ergebnis :
Logfile :

2017.10.08 22:45:55 1: Including fhem.cfg
2017.10.08 22:45:55 3: telnetPort: port 7072 opened
2017.10.08 22:45:55 3: WEB: port 8083 opened
2017.10.08 22:45:55 3: WEBphone: port 8084 opened
2017.10.08 22:45:55 3: WEBtablet: port 8085 opened
2017.10.08 22:45:55 2: eventTypes: loaded 199 events from ./log/eventTypes.txt
2017.10.08 22:45:55 3: Opening RFXTRX device /dev/ttyUSB0
2017.10.08 22:45:55 3: Setting RFXTRX serial parameters to 38400,8,N,1
2017.10.08 22:45:58 1: TRX: Init OK
2017.10.08 22:45:58 1: TRX: Init status: '433.92MHz transceiver, firmware=1020, protocols enabled: Lighting4 FineOffset/Viking Rubicson LaCrosse Hideki OREGON HOMEEASY AC ARC HomeComfort '
2017.10.08 22:45:58 3: RFXTRX device opened
2017.10.08 22:45:58 3: Opening ZWAVE2 device /dev/ttyACM0
2017.10.08 22:45:58 3: Setting ZWAVE2 serial parameters to 38400,8,N,1
2017.10.08 22:45:59 3: ZWAVE2 device opened
2017.10.08 22:45:59 3: FHEM2FHEM opening RemoteServer at 10.0.0.40:7072
2017.10.08 22:46:00 3: TABLETUI: new ext defined infix:ftui/: dir:./www/tablet/:
2017.10.08 22:46:00 3: Registering HTTPSRV TABLETUI for URL /ftui   and assigned link ftui/ ...
2017.10.08 22:46:00 2: SIRD Init module
2017.10.08 22:46:00 1: Including ./log/fhem.save
2017.10.08 22:46:00 2: SecurityCheck:  WEB has no associated allowed device with basicAuth. telnetPort has no associated allowed device with password/globalpassword.  Restart FHEM for a new check if the problem is fixed, or set the global attribute motd to none to supress this message.
2017.10.08 22:46:00 0: Featurelevel: 5.8
2017.10.08 22:46:00 0: Server started with 120 defined entities (fhem.pl:15112/2017-09-21 perl:5.022001 os:linux user:fhem pid:29577)
2017.10.08 22:46:00 3: FHEM2FHEM device opened (RemoteServer)
Traceback (most recent call last):
  File "/opt/fhem/python/verisure_getAlarm.py", line 1, in <module>
    import verisure
  File "/usr/local/lib/python2.7/dist-packages/verisure/__init__.py", line 13, in <module>
    from .session import ( # NOQA
  File "/usr/local/lib/python2.7/dist-packages/verisure/session.py", line 7, in <module>
    import requests
  File "/usr/local/lib/python2.7/dist-packages/requests/__init__.py", line 97, in <module>
    from . import utils
  File "/usr/local/lib/python2.7/dist-packages/requests/utils.py", line 42, in <module>
    if platform.system() == 'Windows':
  File "/usr/lib/python2.7/platform.py", line 1286, in system
    return uname()[0]
  File "/usr/lib/python2.7/platform.py", line 1253, in uname
    processor = _syscmd_uname('-p','')
  File "/usr/lib/python2.7/platform.py", line 988, in _syscmd_uname
    rc = f.close()
IOError: [Errno 10] No child processes
2017.10.08 22:46:17 3: setstate VerisureAlarm  : Usage: setstate <name> <state>
where <name> is a single device name, a list separated by komma (,) or a regexp. See the devspec section in the commandref.html for details.

2017.10.08 22:46:17 3: VAlarm_check return value: Usage: setstate <name> <state>
where <name> is a single device name, a list separated by komma (,) or a regexp. See the devspec section in the commandref.html for details.

Logfile Ende.

Ein Unterschied gibt es natuerlich :
Wenn ich das Kommando im Terminal ausfuehre, dann unter meinem User.
Aus FHEM heraus halt mit dem User fhem.

Titel: Antw:Python Script
Beitrag von: Otto123 am 08 Oktober 2017, 23:05:15
Ich würde sagen es liegt nicht an den Rechten oder der Ausführung von python, es liegt an den Rechten für verisure. Das python script braucht mehr Rechte an irgendwas.
Aber ich habe davon keine Ahnung ...

Viel Erfolg Otto
Titel: Antw:Python Script
Beitrag von: amenomade am 08 Oktober 2017, 23:53:40
Was ergibt { qx(uname -p 2> /dev/null) } im Kommandofeld von FHEM?
Titel: Antw:Python Script
Beitrag von: Wolfgang Hochweller am 09 Oktober 2017, 00:14:22
Das gibt :
x86_64

Zur Information :
Die Verisurefunktionen kommen von hier :

https://github.com/persandstrom/python-verisure
Fuer die Benutzung hat man zwei Moeglichkeiten :

Command Line
oder
Module

Letzteres habe ich die ganze Zeit probiert, ohne Erfolg.

Gehe ich ueber Command Line, dann sieht das Notify in etwa so aus :


VerisureAlarm:.* {
my @alarm = qx(vsure "user" "pass"  armstate);;
fhem ( "setstate VerisureAlarm @alarm[2]" );;
}


Das funktioniert, als @alarm bekomme ich folgendes :

    "date": "2017-10-08T12:54:56.000Z",
    "statusType": "DISARMED",
    "name": "Wolfgang Hochweller",
    "changedVia": "TAG",
    "cid": "00767376"

als @alarm[2]   bekomme ich also

"statusType": "DISARMED"

Die Reihenfolge in @alarm ist aber nicht festgelegt,
ich muss also noch das richtige raussuchen, denn haben will ich nur :  DISARMED





Titel: Antw:Python Script
Beitrag von: amenomade am 09 Oktober 2017, 01:28:41
ZitatDas gibt :
x86_64
Dann verstehe ich nicht, warum das ganze nicht funktioniert.

Aber Du hast es fast schon mit der andere Lösung.
Mit decode_json oder einer Regex, solltest Du es jetzt schaffen.