#!/usr/bin/env python3
# ==========================================================================================================================
import requests, warnings, tempfile
from requests.auth import HTTPDigestAuth
# URL der Fritzbox
fritzbox_url = "https://192.168.178.1"
# Benutzername
Username = "User"
# Passwort
Password = "123456"
# Geräteliste anlegen
# 09995 0123456 --> DECT 301
# 08761 0123456 --> DECT 200
# 11630 0123456 --> DECT 200
# 13077 0123456 --> DECT 500, gibt hier keine Werte aus
device_list = ['09995 0123456','08761 0123456','11630 0123456','13077 0123456']
# Linie ziehen
print ("------------------------------------")
# Geräte aus der Geräteliste abfragen
# ==========================================================================================================================
for i, device_ain in enumerate(device_list):
# Header erstellen
request_headers = {'Content-Type': 'text/xml; charset="utf-8"', 'SoapAction': 'urn:dslforum-org:service:X_AVM-DE_Homeauto:1#GetSpecificDeviceInfos'}
# Daten erstellen für POST
request_data = '<?xml version=\"1.0\"?> \
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" \
s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> \
<s:Body> \
<u:GetSpecificDeviceInfos xmlns:u=\"urn:dslforum-org:service:X_AVM-DE_Homeauto:1\"> \
<NewAIN>00000 0000000</NewAIN> \
</u:GetSpecificDeviceInfos> \
</s:Body> \
</s:Envelope>'
# AIN durch die aus der Liste austauschen
request_data = request_data.replace("00000 0000000", device_ain)
# selbstsigniertes Zertifikat der Fritzbox verursacht eine Warnung, daher Fehler unterdrücken
warnings.simplefilter('ignore')
# Anfrage starten
request_result = requests.post(fritzbox_url + ":49443/upnp/control/x_homeauto", auth=HTTPDigestAuth(Username, Password), headers=request_headers, data=request_data, verify=False)
# Warnungen wieder erlauben
warnings.resetwarnings()
# Zum Debuggen, alles Ausgeben
#print (request_result.text)
# Checken, ob es Fehler gibt und wenn ja, welche. Wenn nicht, dann weiter...
if "s:Fault" in request_result.text:
# Datenanfrage in einzelne Variablen packen
fault_string = str((request_result.text).split("<faultstring>")[1].split("</faultstring")[0])
fault_error_code = int((request_result.text).split("<errorCode>")[1].split("</errorCode>")[0])
fault_error_description = str((request_result.text).split("<errorDescription>")[1].split("</errorDescription>")[0])
print ("AIN: ",device_ain)
print ("Fault string: ",fault_string)
print ("Error code: ",fault_error_code)
print ("Error description: ",fault_error_description)
print ("Possible reason: device_list contains wrong AIN, device is not connected due to an empty battery!")
else:
# Datenanfrage in einzelne Variablen packen
device_id = int((request_result.text).split("<NewDeviceId>")[1].split("</NewDeviceId>")[0])
device_function_bitmask = int((request_result.text).split("<NewFunctionBitMask>")[1].split("</NewFunctionBitMask>")[0])
device_firmware = float((request_result.text).split("<NewFirmwareVersion>")[1].split("</NewFirmwareVersion>")[0])
device_manufacturer = str((request_result.text).split("<NewManufacturer>")[1].split("</NewManufacturer>")[0])
device_product_name = str((request_result.text).split("<NewProductName>")[1].split("</NewProductName>")[0])
device_device_name = str((request_result.text).split("<NewDeviceName>")[1].split("</NewDeviceName>")[0])
device_present = str((request_result.text).split("<NewPresent>")[1].split("</NewPresent>")[0])
# Funktionen herausfinden
device_multimeter_enabled = str((request_result.text).split("<NewMultimeterIsEnabled>")[1].split("</NewMultimeterIsEnabled>")[0])
device_temperature_enabled = str((request_result.text).split("<NewTemperatureIsEnabled>")[1].split("</NewTemperatureIsEnabled>")[0])
device_switch_enabled = str((request_result.text).split("<NewSwitchIsEnabled>")[1].split("</NewSwitchIsEnabled>")[0])
device_hkr_enabled = str((request_result.text).split("<NewHkrIsEnabled>")[1].split("</NewHkrIsEnabled>")[0])
# Ausgabe der einzelnen Variablen
print ("AIN: ",device_ain)
print ("ID: ",device_id)
print ("Function bitmask: ",device_function_bitmask)
print ("Firmware version: ",device_firmware)
print ("Manufacturer: ",device_manufacturer)
print ("Product name: ",device_product_name)
print ("Device name: ",device_device_name)
print ("Connection: ",device_present)
if device_multimeter_enabled == 'ENABLED':
device_multimeter_valid = str((request_result.text).split("<NewMultimeterIsValid>")[1].split("</NewMultimeterIsValid>")[0])
device_multimeter_power = float((request_result.text).split("<NewMultimeterPower>")[1].split("</NewMultimeterPower>")[0])/100
device_multimeter_energy = float((request_result.text).split("<NewMultimeterEnergy>")[1].split("</NewMultimeterEnergy>")[0])/1000
print ("Multimeter function: ",device_multimeter_enabled)
print ("Multimeter values: ",device_multimeter_valid)
print ("Multimeter power: ",device_multimeter_power,"W")
print ("Multimeter energy: ",device_multimeter_energy,"kWh")
if device_temperature_enabled == 'ENABLED':
device_temperature_valid = str((request_result.text).split("<NewTemperatureIsValid>")[1].split("</NewTemperatureIsValid>")[0])
device_temperature = float((request_result.text).split("<NewTemperatureCelsius>")[1].split("</NewTemperatureCelsius>")[0])/10
device_temperature_offset = float((request_result.text).split("<NewTemperatureOffset>")[1].split("</NewTemperatureOffset>")[0])/10
device_temperature_set = float((request_result.text).split("<NewHkrSetTemperature>")[1].split("</NewHkrSetTemperature>")[0])/10
device_temperature_is = float((request_result.text).split("<NewHkrIsTemperature>")[1].split("</NewHkrIsTemperature>")[0])/10
print ("Temperature function: ",device_temperature_enabled)
print ("Temperature values: ",device_temperature_valid)
print ("Temperature w. offs: ",device_temperature,"°C")
print ("Temperature offset: ",device_temperature_offset,"°C")
print ("Temperature set: ",device_temperature_set,"°C")
print ("Temperature w/o offs: ",device_temperature_is,"°C")
if device_switch_enabled == 'ENABLED':
device_switch_valid = str((request_result.text).split("<NewSwitchIsValid>")[1].split("</NewSwitchIsValid>")[0])
device_switch_state = str((request_result.text).split("<NewSwitchState>")[1].split("</NewSwitchState>")[0])
device_switch_mode = str((request_result.text).split("<NewSwitchMode>")[1].split("</NewSwitchMode>")[0])
device_switch_lock = int((request_result.text).split("<NewSwitchLock>")[1].split("</NewSwitchLock>")[0])
print ("Switch function: ",device_switch_enabled)
print ("Switch values: ",device_switch_valid)
print ("Switch state: ",device_switch_state)
print ("Switch mode: ",device_switch_mode)
print ("Switch lock: ",device_switch_lock)
if device_hkr_enabled == 'ENABLED':
device_hkr_valid = str((request_result.text).split("<NewHkrIsValid>")[1].split("</NewHkrIsValid>")[0])
device_hkr_temp_is = float((request_result.text).split("<NewHkrIsTemperature>")[1].split("</NewHkrIsTemperature>")[0])/10
device_hkr_vent_set = str((request_result.text).split("<NewHkrSetVentilStatus>")[1].split("</NewHkrSetVentilStatus>")[0])
device_hkr_temp_set = float((request_result.text).split("<NewHkrSetTemperature>")[1].split("</NewHkrSetTemperature>")[0])/10
device_hkr_vent_red = str((request_result.text).split("<NewHkrReduceVentilStatus>")[1].split("</NewHkrReduceVentilStatus>")[0])
device_hkr_temp_red = float((request_result.text).split("<NewHkrReduceTemperature>")[1].split("</NewHkrReduceTemperature>")[0])/10
device_hkr_vent_com = str((request_result.text).split("<NewHkrComfortVentilStatus>")[1].split("</NewHkrComfortVentilStatus>")[0])
device_hkr_temp_com = float((request_result.text).split("<NewHkrComfortTemperature>")[1].split("</NewHkrComfortTemperature>")[0])/10
print ("Valve function: ",device_hkr_enabled)
print ("Valve values: ",device_hkr_valid)
print ("Valve temperature: ",device_hkr_temp_is,"°C")
print ("Valve state: ",device_hkr_vent_set)
print ("Valve temp set: ",device_hkr_temp_set,"°C")
print ("Valve reduced state: ",device_hkr_vent_red)
print ("Valve reduced temp: ",device_hkr_temp_red,"°C")
print ("Valve comfort state: ",device_hkr_vent_com)
print ("Valve comfort temp: ",device_hkr_temp_com,"°C")
print ("------------------------------------")
#Ende
# ==========================================================================================================================