From 65ef6d9dad92c5764d137debd0342de4542eec18 Mon Sep 17 00:00:00 2001 From: Peter Neuer Date: Mon, 12 Aug 2024 22:06:26 +0000 Subject: [PATCH] FHEM Datenabfrage-Skript erstellt --- scripts/fhem_fetch.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 scripts/fhem_fetch.py diff --git a/scripts/fhem_fetch.py b/scripts/fhem_fetch.py new file mode 100644 index 0000000..d9c7e03 --- /dev/null +++ b/scripts/fhem_fetch.py @@ -0,0 +1,36 @@ +import requests + +# FHEM URL +FHEM_URL = "http://192.168.2.200:8083/fhem" + +# Parameters including CSRF token +PARAMS = { + "cmd": "jsonlist2", + "XHR": "1", + "fwcsrf": "csrf_822558611144652" # CSRF token as a parameter +} + +# Headers including CSRF token +HEADERS = { + "X-FHEM-csrfToken": "csrf_822558611144652", + "Content-Type": "application/x-www-form-urlencoded", + "Accept": "application/json" +} + +# Send the request with both parameters and headers +response = requests.get(FHEM_URL, params=PARAMS, headers=HEADERS) + +# Debugging: Check the status code and response content +print("Response Status Code:", response.status_code) +print("Response Content:", response.text) + +try: + # Attempt to parse the response as JSON + data = response.json() + devices = data['Results'] + for device in devices: + print(f"Device: {device['Name']}, State: {device['Internals']['STATE']}") +except requests.exceptions.JSONDecodeError: + print("Error: Failed to decode JSON response.") +except KeyError: + print("Error: Expected keys not found in the JSON data.")