Merge pull request #74 from Nitwel/button

Add button entity
This commit is contained in:
Nitwel 2023-12-10 00:35:20 +01:00 committed by GitHub
commit c283ab251e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 1 deletions

View File

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

View File

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

View File

@ -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,6 +21,8 @@ static func create_entity(type: String, id: String):
entity = MediaPlayer.instantiate()
"camera":
entity = Camera.instantiate()
"button":
entity = ButtonEntity.instantiate()
_:
return null

View File

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