From 46cd29c477aa4fcfeac5b7cef5cdb1883e45261a Mon Sep 17 00:00:00 2001 From: Nitwel Date: Sun, 10 Dec 2023 00:34:54 +0100 Subject: [PATCH] add button entity --- content/entities/button/button.gd | 30 ++++++++++++++++++++++++++ content/entities/button/button.tscn | 24 +++++++++++++++++++++ content/ui/menu/edit/entity_creator.gd | 5 ++++- lib/home_apis/hass_ws/hass.gd | 3 +++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 content/entities/button/button.gd create mode 100644 content/entities/button/button.tscn diff --git a/content/entities/button/button.gd b/content/entities/button/button.gd new file mode 100644 index 0000000..179411e --- /dev/null +++ b/content/entities/button/button.gd @@ -0,0 +1,30 @@ +extends Node3D + +var entity_id = "button.plug_printer_2" +@onready var button = $Button + +func _ready(): + var stateInfo = await HomeApi.get_state(entity_id) + + if stateInfo == null: + return + + set_state(stateInfo) + + await HomeApi.watch_state(entity_id, func(new_state): + set_state(new_state) + ) + + button.on_button_down.connect(func(): + HomeApi.set_state(entity_id, "pressed") + ) + +func set_state(state): + if state.attributes.has("friendly_name"): + button.label = state.attributes["friendly_name"] + +func _save(): + return { + "transform": transform, + "entity_id": entity_id + } diff --git a/content/entities/button/button.tscn b/content/entities/button/button.tscn new file mode 100644 index 0000000..c19ce81 --- /dev/null +++ b/content/entities/button/button.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=6 format=3 uid="uid://c2j7nev6qx25s"] + +[ext_resource type="Script" path="res://content/entities/button/button.gd" id="1_ja7lt"] +[ext_resource type="PackedScene" uid="uid://bsjqdvkt0u87c" path="res://content/ui/components/button/button.tscn" id="1_r4tef"] +[ext_resource type="Script" path="res://content/functions/movable.gd" id="3_vrobf"] +[ext_resource type="Script" path="res://content/functions/occludable.gd" id="4_7upxo"] + +[sub_resource type="BoxShape3D" id="BoxShape3D_um5pa"] +size = Vector3(0.0700684, 0.011734, 0.0703125) + +[node name="Button" type="StaticBody3D" groups=["entity"]] +script = ExtResource("1_ja7lt") + +[node name="Button" parent="." instance=ExtResource("1_r4tef")] + +[node name="Occludable" type="Node" parent="."] +script = ExtResource("4_7upxo") + +[node name="Movable" type="Node" parent="."] +script = ExtResource("3_vrobf") + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.00222015, 0) +shape = SubResource("BoxShape3D_um5pa") diff --git a/content/ui/menu/edit/entity_creator.gd b/content/ui/menu/edit/entity_creator.gd index 240bf91..25eb24b 100644 --- a/content/ui/menu/edit/entity_creator.gd +++ b/content/ui/menu/edit/entity_creator.gd @@ -5,6 +5,7 @@ const Light = preload("res://content/entities/light/light.tscn") const Sensor = preload("res://content/entities/sensor/sensor.tscn") const MediaPlayer = preload("res://content/entities/media_player/media_player.tscn") const Camera = preload("res://content/entities/camera/camera.tscn") +const ButtonEntity = preload("res://content/entities/button/button.tscn") static func create_entity(type: String, id: String): var entity = null @@ -20,8 +21,10 @@ static func create_entity(type: String, id: String): entity = MediaPlayer.instantiate() "camera": entity = Camera.instantiate() + "button": + entity = ButtonEntity.instantiate() _: return null - + entity.entity_id = id return entity \ No newline at end of file diff --git a/lib/home_apis/hass_ws/hass.gd b/lib/home_apis/hass_ws/hass.gd index 29f847d..b66f9ce 100644 --- a/lib/home_apis/hass_ws/hass.gd +++ b/lib/home_apis/hass_ws/hass.gd @@ -256,6 +256,9 @@ func set_state(entity: String, state: String, attributes: Dictionary = {}): service = "media_previous_track" elif state == "volume": service = "volume_set" + elif domain == 'button': + if state == 'pressed': + service = 'press' if service == null: return null