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

40 lines
803 B
GDScript3
Raw Normal View History

2024-01-27 16:13:43 +02:00
extends Entity
const Entity = preload ("../entity.gd")
@onready var sprite: AnimatedSprite3D = $Icon
2024-05-09 13:40:41 +03:00
@onready var snap_sound = $SnapSound
2024-04-18 13:35:57 +03:00
var active = R.state(false)
# Called when the node enters the scene tree for the first time.
func _ready():
2024-01-27 16:13:43 +02:00
super()
var stateInfo = await HomeApi.get_state(entity_id)
2023-11-05 14:59:33 +02:00
2024-04-18 13:35:57 +03:00
set_state(stateInfo)
await HomeApi.watch_state(entity_id, func(new_state):
2024-04-18 13:35:57 +03:00
set_state(new_state)
)
2024-04-18 13:35:57 +03:00
R.effect(func(_arg):
sprite.set_frame(1 if active.value else 0)
)
2024-04-18 13:35:57 +03:00
func set_state(stateInfo):
if stateInfo == null:
return
active.value = stateInfo["state"] == "on"
icon.value = "toggle_" + stateInfo["state"]
2024-04-18 13:35:57 +03:00
func _on_click(_event):
2024-05-09 13:40:41 +03:00
snap_sound.play()
2024-04-18 13:35:57 +03:00
_toggle()
2024-04-17 22:27:09 +03:00
func quick_action():
2024-04-18 13:35:57 +03:00
_toggle()
func _toggle():
HomeApi.set_state(entity_id, "off" if active.value else "on")