FHEM Datenabfrage-Skript erstellt
This commit is contained in:
36
scripts/fhem_fetch.py
Normal file
36
scripts/fhem_fetch.py
Normal file
@@ -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.")
|
Reference in New Issue
Block a user