From 1365a48e837b8848d4e0b2e6a74a6a52b9185a0d Mon Sep 17 00:00:00 2001 From: Nitwel Date: Sun, 19 May 2024 11:47:05 +0200 Subject: [PATCH] add homeapi debug ui --- .../libs/~libdd3d.windows.editor.x86_64.dll | 3 - .../~libcdt.windows.template_debug.x86_64.dll | 3 - app/lib/globals/home_api.gd | 2 - app/lib/home_apis/hass/hass.gd | 58 ------------ app/lib/home_apis/hass/templates/devices.j2 | 12 --- app/test/lib/home_apis/hass_ws/debug.gd | 34 +++++++ app/test/lib/home_apis/hass_ws/debug.tscn | 93 +++++++++++++++++++ 7 files changed, 127 insertions(+), 78 deletions(-) delete mode 100644 app/addons/debug_draw_3d/libs/~libdd3d.windows.editor.x86_64.dll delete mode 100644 app/addons/godot-cdt/~libcdt.windows.template_debug.x86_64.dll delete mode 100644 app/lib/home_apis/hass/hass.gd delete mode 100644 app/lib/home_apis/hass/templates/devices.j2 create mode 100644 app/test/lib/home_apis/hass_ws/debug.gd create mode 100644 app/test/lib/home_apis/hass_ws/debug.tscn diff --git a/app/addons/debug_draw_3d/libs/~libdd3d.windows.editor.x86_64.dll b/app/addons/debug_draw_3d/libs/~libdd3d.windows.editor.x86_64.dll deleted file mode 100644 index 2206af5..0000000 --- a/app/addons/debug_draw_3d/libs/~libdd3d.windows.editor.x86_64.dll +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ac3ac3647171c53e2f47564df2e25eff19fbfab8bf763d0078fe1c82476d8761 -size 1291264 diff --git a/app/addons/godot-cdt/~libcdt.windows.template_debug.x86_64.dll b/app/addons/godot-cdt/~libcdt.windows.template_debug.x86_64.dll deleted file mode 100644 index aaf3431..0000000 --- a/app/addons/godot-cdt/~libcdt.windows.template_debug.x86_64.dll +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:418c2ca8e73932710184b3bc4de4b40a32fc7ef0369354f8a08fbacd01155f50 -size 871936 diff --git a/app/lib/globals/home_api.gd b/app/lib/globals/home_api.gd index 82bd92c..e37fd31 100644 --- a/app/lib/globals/home_api.gd +++ b/app/lib/globals/home_api.gd @@ -1,13 +1,11 @@ extends Node ## Manages the connection to the home automation system and provides a unified interface to the different home automation systems. -const Hass = preload ("res://lib/home_apis/hass/hass.gd") const EntityGroups = preload ("res://lib/utils/entity_group.gd") const HassWebSocket = preload ("res://lib/home_apis/hass_ws/hass.gd") const VoiceAssistant = preload ("res://lib/home_apis/voice_handler.gd") const apis = { - "hass": Hass, "hass_ws": HassWebSocket } diff --git a/app/lib/home_apis/hass/hass.gd b/app/lib/home_apis/hass/hass.gd deleted file mode 100644 index 88e101c..0000000 --- a/app/lib/home_apis/hass/hass.gd +++ /dev/null @@ -1,58 +0,0 @@ -extends Node - -var url: String = "http://192.168.33.33:8123" -var token: String = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIzZjQ0ZGM2N2Y3YzY0MDc1OGZlMWI2ZjJlNmIxZjRkNSIsImlhdCI6MTY5ODAxMDcyOCwiZXhwIjoyMDEzMzcwNzI4fQ.K6ydLUC-4Q7BNIRCU1nWlI2s6sg9UCiOu-Lpedw2zJc" -var headers: PackedStringArray = PackedStringArray([]) - -var devices_template = FileAccess.get_file_as_string("res://lib/home_apis/hass/templates/devices.j2") - -func _init(url := self.url, token := self.token): - self.url = url - self.token = token - - headers = PackedStringArray(["Authorization: Bearer %s" % token, "Content-Type: application/json"]) - devices_template = devices_template.replace("\n", " ").replace("\t", "").replace("\r", " ").replace("\"", "\\\"") - -func get_devices(): - Request.request("%s/api/template" % [url], headers, HTTPClient.METHOD_POST, "{\"template\": \"%s\"}" % [devices_template]) - var response = await Request.request_completed - var data_string = response[3].get_string_from_utf8().replace("'", "\"") - var json = JSON.parse_string(data_string).data - - return json - -func get_state(entity: String): - var type = entity.split('.')[0] - - Request.request("%s/api/states/%s" % [url, entity], headers, HTTPClient.METHOD_GET) - var response = await Request.request_completed - - var data_string = response[3].get_string_from_utf8().replace("'", "\"") - var json = JSON.parse_string(data_string) - - return json - -func set_state(entity: String, state: String, attributes: Dictionary = {}): - var type = entity.split('.')[0] - var response - - if type == 'switch': - if state == 'on': - Request.request("%s/api/services/switch/turn_on" % [url], headers, HTTPClient.METHOD_POST, "{\"entity_id\": \"%s\"}" % [entity]) - response = await Request.request_completed - elif state == 'off': - Request.request("%s/api/services/switch/turn_off" % [url], headers, HTTPClient.METHOD_POST, "{\"entity_id\": \"%s\"}" % [entity]) - response = await Request.request_completed - elif type == 'light': - if state == 'on': - Request.request("%s/api/services/light/turn_on" % [url], headers, HTTPClient.METHOD_POST, "{\"entity_id\": \"%s\"}" % [entity]) - response = await Request.request_completed - elif state == 'off': - Request.request("%s/api/services/light/turn_off" % [url], headers, HTTPClient.METHOD_POST, "{\"entity_id\": \"%s\"}" % [entity]) - response = await Request.request_completed - - var data_string = response[3].get_string_from_utf8().replace("'", "\"") - var json = JSON.parse_string(data_string) - - return json - diff --git a/app/lib/home_apis/hass/templates/devices.j2 b/app/lib/home_apis/hass/templates/devices.j2 deleted file mode 100644 index d1750ba..0000000 --- a/app/lib/home_apis/hass/templates/devices.j2 +++ /dev/null @@ -1,12 +0,0 @@ -{% set devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('eq',None) | list %} - -{%- set ns = namespace(devices = []) %} -{%- for device in devices %} - {%- set entities = device_entities(device) | list %} - {%- if entities %} - {%- set ns.devices = ns.devices + [ {device: {"name": device_attr(device, "name"), "entities": entities }} ] %} - {%- endif %} -{%- endfor %} -{ - "data": {{ ns.devices }} -} \ No newline at end of file diff --git a/app/test/lib/home_apis/hass_ws/debug.gd b/app/test/lib/home_apis/hass_ws/debug.gd new file mode 100644 index 0000000..12dfb87 --- /dev/null +++ b/app/test/lib/home_apis/hass_ws/debug.gd @@ -0,0 +1,34 @@ +extends Control + +@onready var url_input = $HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer/LineEdit +@onready var token_input = $HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer2/LineEdit2 +@onready var connect_button = $HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/Button +@onready var log_label = $HBoxContainer/PanelContainer2/MarginContainer/VBoxContainer/Label +@onready var code_input = $HBoxContainer/PanelContainer2/MarginContainer/VBoxContainer/CodeEdit + +var expression = Expression.new() + +func _ready(): + connect_button.button_up.connect(func(): + var url=url_input.text + var token=token_input.text + + HomeApi.start_adapter("hass_ws", url, token) + HomeApi.api.LOG_MESSAGES=true + ) + + code_input.text_submitted.connect(func(text): + var error=expression.parse(text) + if error != OK: + print(expression.get_error_text()) + return + expression.execute.call_deferred([], HomeApi) + ) + + HomeApi.on_connect.connect(func(): + connect_button.text="Connected ✅" + ) + + HomeApi.on_disconnect.connect(func(): + connect_button.text="Connect" + ) diff --git a/app/test/lib/home_apis/hass_ws/debug.tscn b/app/test/lib/home_apis/hass_ws/debug.tscn new file mode 100644 index 0000000..656a5de --- /dev/null +++ b/app/test/lib/home_apis/hass_ws/debug.tscn @@ -0,0 +1,93 @@ +[gd_scene load_steps=2 format=3 uid="uid://c1hfl4sj7u0bw"] + +[ext_resource type="Script" path="res://test/lib/home_apis/hass_ws/debug.gd" id="1_rncfy"] + +[node name="Debug" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_rncfy") + +[node name="HBoxContainer" type="HBoxContainer" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +theme_override_constants/separation = 20 + +[node name="PanelContainer" type="PanelContainer" parent="HBoxContainer"] +layout_mode = 2 + +[node name="MarginContainer" type="MarginContainer" parent="HBoxContainer/PanelContainer"] +layout_mode = 2 +theme_override_constants/margin_left = 20 +theme_override_constants/margin_top = 20 +theme_override_constants/margin_right = 20 +theme_override_constants/margin_bottom = 20 + +[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer/PanelContainer/MarginContainer"] +layout_mode = 2 + +[node name="HBoxContainer" type="HBoxContainer" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +layout_mode = 2 +theme_override_constants/separation = 10 + +[node name="Label" type="Label" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +layout_mode = 2 +text = "URL" + +[node name="LineEdit" type="LineEdit" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +custom_minimum_size = Vector2(200, 0) +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_vertical = 4 +text = "ws://192.168.33.33:8123" + +[node name="HBoxContainer2" type="HBoxContainer" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +layout_mode = 2 +theme_override_constants/separation = 10 + +[node name="Label2" type="Label" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer2"] +layout_mode = 2 +text = "Token" + +[node name="LineEdit2" type="LineEdit" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer2"] +layout_mode = 2 +size_flags_horizontal = 3 +placeholder_text = "enter token..." + +[node name="HBoxContainer3" type="HBoxContainer" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +layout_mode = 2 + +[node name="Button" type="Button" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +layout_mode = 2 +text = "Connect" + +[node name="PanelContainer2" type="PanelContainer" parent="HBoxContainer"] +layout_mode = 2 +size_flags_horizontal = 3 + +[node name="MarginContainer" type="MarginContainer" parent="HBoxContainer/PanelContainer2"] +layout_mode = 2 +theme_override_constants/margin_left = 20 +theme_override_constants/margin_top = 20 +theme_override_constants/margin_right = 20 +theme_override_constants/margin_bottom = 20 + +[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer/PanelContainer2/MarginContainer"] +layout_mode = 2 +theme_override_constants/separation = 10 + +[node name="Label" type="Label" parent="HBoxContainer/PanelContainer2/MarginContainer/VBoxContainer"] +layout_mode = 2 +size_flags_vertical = 3 + +[node name="CodeEdit" type="LineEdit" parent="HBoxContainer/PanelContainer2/MarginContainer/VBoxContainer"] +layout_mode = 2 +size_flags_vertical = 8 +placeholder_text = "set_state(\"light.my-lamp\", \"on\")"