add homeapi debug ui

This commit is contained in:
Nitwel 2024-05-19 11:47:05 +02:00
parent f046a68f99
commit 1365a48e83
7 changed files with 127 additions and 78 deletions

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ac3ac3647171c53e2f47564df2e25eff19fbfab8bf763d0078fe1c82476d8761
size 1291264

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:418c2ca8e73932710184b3bc4de4b40a32fc7ef0369354f8a08fbacd01155f50
size 871936

View File

@ -1,13 +1,11 @@
extends Node extends Node
## Manages the connection to the home automation system and provides a unified interface to the different home automation systems. ## 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 EntityGroups = preload ("res://lib/utils/entity_group.gd")
const HassWebSocket = preload ("res://lib/home_apis/hass_ws/hass.gd") const HassWebSocket = preload ("res://lib/home_apis/hass_ws/hass.gd")
const VoiceAssistant = preload ("res://lib/home_apis/voice_handler.gd") const VoiceAssistant = preload ("res://lib/home_apis/voice_handler.gd")
const apis = { const apis = {
"hass": Hass,
"hass_ws": HassWebSocket "hass_ws": HassWebSocket
} }

View File

@ -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

View File

@ -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 }}
}

View File

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

View File

@ -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\")"