Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -34,6 +34,7 @@ class EzvizTuyaAutomation:
|
|
34 |
if response.status_code == 200:
|
35 |
devices = response.json().get("devices", [])
|
36 |
return [{"name": dev["name"], "id": dev["deviceSerial"]} for dev in devices]
|
|
|
37 |
return []
|
38 |
|
39 |
def get_tuya_devices(self):
|
@@ -50,8 +51,16 @@ class EzvizTuyaAutomation:
|
|
50 |
if response.status_code == 200:
|
51 |
devices = response.json().get("devices", [])
|
52 |
return [{"name": dev["name"], "id": dev["id"]} for dev in devices]
|
|
|
53 |
return []
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
def check_motion(self):
|
56 |
"""Check for motion events from selected EZVIZ camera"""
|
57 |
if not self.ezviz_access_token or not self.selected_camera:
|
@@ -123,14 +132,26 @@ def create_ui():
|
|
123 |
return "Credentials updated"
|
124 |
|
125 |
def get_devices():
|
126 |
-
#
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
return ezviz_choices, tuya_choices
|
134 |
|
135 |
def start_automation(camera_choice, light_choice):
|
136 |
if not camera_choice or not light_choice:
|
@@ -166,6 +187,8 @@ def create_ui():
|
|
166 |
tuya_id = gr.Textbox(label="Tuya Client ID")
|
167 |
tuya_secret = gr.Textbox(label="Tuya Secret", type="password")
|
168 |
save_btn = gr.Button("Save Credentials")
|
|
|
|
|
169 |
|
170 |
with gr.Row():
|
171 |
refresh_btn = gr.Button("Refresh Device Lists")
|
@@ -186,6 +209,8 @@ def create_ui():
|
|
186 |
inputs=[ezviz_key, ezviz_secret, tuya_id, tuya_secret],
|
187 |
outputs=gr.Textbox(label="Status"))
|
188 |
|
|
|
|
|
189 |
refresh_btn.click(get_devices,
|
190 |
None,
|
191 |
outputs=[camera_dropdown, light_dropdown])
|
|
|
34 |
if response.status_code == 200:
|
35 |
devices = response.json().get("devices", [])
|
36 |
return [{"name": dev["name"], "id": dev["deviceSerial"]} for dev in devices]
|
37 |
+
print(f"EZVIZ API Response: {response.text}") # Debugging
|
38 |
return []
|
39 |
|
40 |
def get_tuya_devices(self):
|
|
|
51 |
if response.status_code == 200:
|
52 |
devices = response.json().get("devices", [])
|
53 |
return [{"name": dev["name"], "id": dev["id"]} for dev in devices]
|
54 |
+
print(f"Tuya API Response: {response.text}") # Debugging
|
55 |
return []
|
56 |
|
57 |
+
def validate_tokens(self):
|
58 |
+
if not self.ezviz_access_token:
|
59 |
+
return "EZVIZ access token is missing or invalid."
|
60 |
+
if not self.tuya_access_token:
|
61 |
+
return "Tuya access token is missing or invalid."
|
62 |
+
return "Tokens validated successfully."
|
63 |
+
|
64 |
def check_motion(self):
|
65 |
"""Check for motion events from selected EZVIZ camera"""
|
66 |
if not self.ezviz_access_token or not self.selected_camera:
|
|
|
132 |
return "Credentials updated"
|
133 |
|
134 |
def get_devices():
|
135 |
+
# Attempt to fetch devices
|
136 |
+
try:
|
137 |
+
ezviz_devices = automation.get_ezviz_devices()
|
138 |
+
tuya_devices = automation.get_tuya_devices()
|
139 |
+
|
140 |
+
if not ezviz_devices:
|
141 |
+
ezviz_choices = ["No devices found or invalid credentials."]
|
142 |
+
else:
|
143 |
+
ezviz_choices = [f"{dev['name']} ({dev['id']})" for dev in ezviz_devices]
|
144 |
+
|
145 |
+
if not tuya_devices:
|
146 |
+
tuya_choices = ["No devices found or invalid credentials."]
|
147 |
+
else:
|
148 |
+
tuya_choices = [f"{dev['name']} ({dev['id']})" for dev in tuya_devices]
|
149 |
+
|
150 |
+
return ezviz_choices, tuya_choices
|
151 |
|
152 |
+
except Exception as e:
|
153 |
+
print(f"Error fetching devices: {e}")
|
154 |
+
return ["Error fetching EZVIZ devices"], ["Error fetching Tuya devices"]
|
|
|
155 |
|
156 |
def start_automation(camera_choice, light_choice):
|
157 |
if not camera_choice or not light_choice:
|
|
|
187 |
tuya_id = gr.Textbox(label="Tuya Client ID")
|
188 |
tuya_secret = gr.Textbox(label="Tuya Secret", type="password")
|
189 |
save_btn = gr.Button("Save Credentials")
|
190 |
+
validate_btn = gr.Button("Validate Tokens")
|
191 |
+
validate_status = gr.Textbox(label="Token Validation Status", interactive=False)
|
192 |
|
193 |
with gr.Row():
|
194 |
refresh_btn = gr.Button("Refresh Device Lists")
|
|
|
209 |
inputs=[ezviz_key, ezviz_secret, tuya_id, tuya_secret],
|
210 |
outputs=gr.Textbox(label="Status"))
|
211 |
|
212 |
+
validate_btn.click(automation.validate_tokens, None, validate_status)
|
213 |
+
|
214 |
refresh_btn.click(get_devices,
|
215 |
None,
|
216 |
outputs=[camera_dropdown, light_dropdown])
|