immersive-home/content/entities/switch/switch.gd

39 lines
805 B
GDScript3
Raw Normal View History

extends StaticBody3D
@export var entity_id = "switch.plug_printer_2"
@onready var sprite: AnimatedSprite3D = $Icon
# Called when the node enters the scene tree for the first time.
func _ready():
var stateInfo = await HomeApi.get_state(entity_id)
2023-11-05 14:59:33 +02:00
if stateInfo == null:
return
if stateInfo["state"] == "on":
sprite.set_frame(0)
else:
sprite.set_frame(1)
await HomeApi.watch_state(entity_id, func(new_state):
if new_state["state"] == "on":
sprite.set_frame(0)
else:
sprite.set_frame(1)
)
2023-11-05 22:32:50 +02:00
func _on_click(event):
HomeApi.set_state(entity_id, "off" if sprite.get_frame() == 0 else "on")
if sprite.get_frame() == 0:
sprite.set_frame(1)
else:
sprite.set_frame(0)
func _on_request_completed():
pass
2023-12-09 19:39:38 +02:00
func _save():
return {
"transform": transform,
"entity_id": entity_id
}