2024-01-27 16:13:43 +02:00
|
|
|
extends Entity
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
const Entity = preload ("../entity.gd")
|
2023-10-31 23:15:33 +02:00
|
|
|
|
|
|
|
@onready var sprite: AnimatedSprite3D = $Icon
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2024-01-27 16:13:43 +02:00
|
|
|
super()
|
2023-11-24 02:36:31 +02:00
|
|
|
var stateInfo = await HomeApi.get_state(entity_id)
|
2023-11-05 14:59:33 +02:00
|
|
|
if stateInfo == null:
|
|
|
|
return
|
|
|
|
|
2023-10-31 23:15:33 +02:00
|
|
|
if stateInfo["state"] == "on":
|
|
|
|
sprite.set_frame(0)
|
|
|
|
else:
|
|
|
|
sprite.set_frame(1)
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
icon.value = "toggle_" + stateInfo["state"]
|
|
|
|
|
2023-11-24 02:36:31 +02:00
|
|
|
await HomeApi.watch_state(entity_id, func(new_state):
|
2023-11-03 23:00:05 +02:00
|
|
|
if new_state["state"] == "on":
|
|
|
|
sprite.set_frame(0)
|
|
|
|
else:
|
|
|
|
sprite.set_frame(1)
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
icon.value="toggle_" + new_state["state"]
|
|
|
|
)
|
2023-10-31 23:15:33 +02:00
|
|
|
|
2023-11-05 22:32:50 +02:00
|
|
|
func _on_click(event):
|
2023-11-24 02:36:31 +02:00
|
|
|
HomeApi.set_state(entity_id, "off" if sprite.get_frame() == 0 else "on")
|
2023-10-31 23:15:33 +02:00
|
|
|
if sprite.get_frame() == 0:
|
|
|
|
sprite.set_frame(1)
|
|
|
|
else:
|
|
|
|
sprite.set_frame(0)
|
|
|
|
|
|
|
|
func _on_request_completed():
|
2024-01-25 17:29:33 +02:00
|
|
|
pass
|