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

58 lines
1.5 KiB
GDScript3
Raw Normal View History

extends StaticBody3D
@export var entity_id = "switch.plug_printer_2"
2023-11-14 01:51:48 +02:00
@export var color_off = Color(0.23, 0.23, 0.23)
@export var color_on = Color(1.0, 0.85, 0.0)
2023-11-29 01:16:25 +02:00
@onready var animation: AnimationPlayer = $AnimationPlayer
@onready var slider: Slider3D = $Slider
var state = true
2023-11-14 01:51:48 +02:00
var brightness = 0 # 0-255
# Called when the node enters the scene tree for the first time.
func _ready():
var stateInfo = await HomeApi.get_state(entity_id)
2023-11-12 18:36:23 +02:00
set_state(stateInfo["state"] == "on")
await HomeApi.watch_state(entity_id, func(new_state):
2023-11-12 18:36:23 +02:00
if (new_state["state"] == "on") == state:
return
set_state(new_state["state"] == "on")
)
2023-11-29 01:16:25 +02:00
slider.on_value_changed.connect(func(new_value):
var value = new_value / 100 * 255
HomeApi.set_state(entity_id, "on" if state else "off", {"brightness": int(value)})
set_state(state, value)
)
func set_state(new_state: bool, new_brightness = null):
if state == false && new_state == false:
return
state = new_state
brightness = new_brightness
2023-11-14 01:51:48 +02:00
2023-11-12 18:36:23 +02:00
if state:
2023-11-14 01:51:48 +02:00
if brightness == null:
2023-11-29 01:16:25 +02:00
animation.speed_scale = 1
animation.play_backwards("light")
2023-11-14 01:51:48 +02:00
else:
2023-11-29 01:16:25 +02:00
var duration = animation.get_animation("light").length
animation.speed_scale = 0
animation.seek(lerpf(0, duration, 1 - (brightness / 255.0)), true)
2023-11-12 18:36:23 +02:00
else:
2023-11-29 01:16:25 +02:00
animation.speed_scale = 1
animation.play("light")
2023-11-14 01:51:48 +02:00
2023-11-05 22:32:50 +02:00
func _on_click(event):
2023-11-12 18:36:23 +02:00
if event.target == self:
2023-11-14 01:51:48 +02:00
var attributes = {}
if !state && brightness != null:
attributes["brightness"] = int(brightness)
HomeApi.set_state(entity_id, "on" if !state else "off", attributes)
2024-01-25 17:29:33 +02:00
set_state(!state, brightness)