2025-06-04 10:03:22 +02:00

175 lines
5.7 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
P110-TAPO-TEST - Speziell für TP-Link Tapo P110-Steckdosen
Testet verschiedene Versionen des PyP100-Moduls
"""
import sys
import time
import socket
import subprocess
from datetime import datetime
# Anmeldedaten
TAPO_USERNAME = "till.tomczak@mercedes-benz.com"
TAPO_PASSWORD = "744563017196"
# Standard-IP-Adressen zum Testen (anpassen an tatsächliche IPs)
TEST_IPS = [
"192.168.0.103", # Diese IPs waren erreichbar im vorherigen Test
"192.168.0.104"
]
def log(message):
"""Logge eine Nachricht mit Zeitstempel"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{timestamp}] {message}")
def check_connection(ip, port=80, timeout=1):
"""Prüft eine TCP-Verbindung"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
result = sock.connect_ex((ip, port))
sock.close()
return result == 0
except:
return False
def install_package(package):
"""Installiert ein Python-Paket"""
try:
log(f"Installiere {package}...")
subprocess.run([sys.executable, "-m", "pip", "install", package, "--force-reinstall"], check=True)
log(f"{package} erfolgreich installiert")
return True
except Exception as e:
log(f"❌ Fehler bei Installation von {package}: {e}")
return False
def test_p110_connection():
"""Testet verschiedene Möglichkeiten, um mit P110-Steckdosen zu kommunizieren"""
log("🚀 TAPO P110 TEST - STARTER")
log(f"👤 Benutzername: {TAPO_USERNAME}")
log(f"🔑 Passwort: {TAPO_PASSWORD}")
# Verfügbare Module testen
log("\n1⃣ SCHRITT 1: Teste verfügbare Module")
try:
from PyP100 import PyP110
log("✅ PyP100 Modul gefunden")
except ImportError:
log("❌ PyP100 Modul nicht gefunden")
install_package("PyP100==0.1.2")
try:
from PyP100 import PyP110
log("✅ PyP100 Modul jetzt installiert")
except ImportError:
log("❌ Konnte PyP100 nicht importieren")
return
# Erreichbare Steckdosen finden
log("\n2⃣ SCHRITT 2: Suche erreichbare IPs")
available_ips = []
for ip in TEST_IPS:
if check_connection(ip):
log(f"✅ IP {ip} ist erreichbar")
available_ips.append(ip)
else:
log(f"❌ IP {ip} nicht erreichbar")
if not available_ips:
log("❌ Keine erreichbaren IPs gefunden!")
return
# P110-Verbindung testen
log("\n3⃣ SCHRITT 3: Teste PyP100 Bibliothek")
for ip in available_ips:
try:
log(f"🔄 Verbinde zu Steckdose {ip} mit PyP100.PyP110...")
# Neue Instanz erstellen
from PyP100 import PyP110
p110 = PyP110.P110(ip, TAPO_USERNAME, TAPO_PASSWORD)
# Handshake und Login
log(" Handshake...")
p110.handshake()
log(" Login...")
p110.login()
# Geräteinformationen abrufen
log(" Geräteinformationen abrufen...")
device_info = p110.getDeviceInfo()
# Erfolg!
log(f"✅ ERFOLG! Steckdose {ip} gefunden")
log(f" Name: {device_info.get('nickname', 'Unbekannt')}")
log(f" Status: {'Eingeschaltet' if device_info.get('device_on', False) else 'Ausgeschaltet'}")
# Ein-/Ausschalten testen
if "--toggle" in sys.argv:
current_state = device_info.get('device_on', False)
if current_state:
log(" Schalte AUS...")
p110.turnOff()
else:
log(" Schalte EIN...")
p110.turnOn()
time.sleep(1)
# Status prüfen
device_info = p110.getDeviceInfo()
new_state = device_info.get('device_on', False)
log(f" Neuer Status: {'Eingeschaltet' if new_state else 'Ausgeschaltet'}")
return True
except Exception as e:
log(f"❌ Fehler bei Verbindung zu {ip}: {e}")
# Alternative Bibliothek testen
log("\n4⃣ SCHRITT 4: Teste PyP100 mit alternativer Version")
if install_package("pytapo==1.1.2"):
try:
import pytapo
from pytapo.tapo import Tapo
for ip in available_ips:
try:
log(f"🔄 Verbinde zu Steckdose {ip} mit pytapo...")
# Neue Verbindung
tapo = Tapo(ip, TAPO_USERNAME, TAPO_PASSWORD)
# Geräteinformationen abrufen
device_info = tapo.get_device_info()
# Erfolg!
log(f"✅ ERFOLG mit pytapo! Steckdose {ip} gefunden")
log(f" Name: {device_info.get('nickname', 'Unbekannt')}")
return True
except Exception as e:
log(f"❌ Fehler bei pytapo-Verbindung zu {ip}: {e}")
except Exception as e:
log(f"❌ Fehler beim Import von pytapo: {e}")
log("\n❌ Keine funktionierenden Tapo-Steckdosen gefunden!")
log("Bitte überprüfen Sie die Anmeldedaten und IP-Adressen")
if __name__ == "__main__":
print("\n======= TAPO P110 TEST =======\n")
test_p110_connection()
print("\n======= TEST BEENDET =======\n")