Files
fhem-extract/scripts/fhem_fetch.py

37 lines
1.0 KiB
Python

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.")