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

163 lines
4.4 KiB
GDScript3
Raw Normal View History

2024-01-27 16:13:43 +02:00
extends Entity
const Entity = preload ("../entity.gd")
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)
@onready var lightbulb = $Lightbulb
2023-11-29 01:16:25 +02:00
@onready var slider: Slider3D = $Slider
2024-01-28 18:13:32 +02:00
@onready var color_wheel = $ColorWheel
@onready var modes: Select3D = $Modes
2024-05-09 13:40:41 +03:00
@onready var snap_sound = $SnapSound
@onready var settings = $Settings
@onready var movable = $Movable
2023-11-29 01:16:25 +02:00
2024-04-18 13:35:57 +03:00
var active = R.state(false)
var brightness = R.state(0) # 0-255
var color = R.state(color_on)
var show_color_wheel = R.state(true)
var color_wheel_supported = R.state(false)
var show_brightness = R.state(true)
var show_modes = R.state(true)
var modes_supported = 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()
2024-05-02 20:35:47 +03:00
if HomeApi.has_connected() == false:
await HomeApi.on_connect
icon.value = "lightbulb"
var stateInfo = await HomeApi.get_state(entity_id)
2024-04-18 13:35:57 +03:00
set_state(stateInfo)
2023-11-12 18:36:23 +02:00
remove_child(settings)
R.effect(func(_arg):
if show_settings.value:
add_child(settings)
elif settings.is_inside_tree():
remove_child(settings)
camera_follower.reset()
App.house.save_all_entities()
)
R.effect(func(_arg):
if show_color_wheel.value&&color_wheel_supported.value:
add_child(color_wheel)
else:
remove_child(color_wheel)
)
R.effect(func(_arg):
if show_brightness.value:
add_child(slider)
else:
remove_child(slider)
)
R.effect(func(_arg):
if show_modes.value&&modes_supported.value:
add_child(modes)
else:
remove_child(modes)
)
if stateInfo.has("attributes")&&stateInfo["attributes"].has("effect_list")&&stateInfo["attributes"]["effect_list"].size() > 0:
modes_supported.value = true
if stateInfo["attributes"].has("effect")&&stateInfo["attributes"]["effect"] != null:
modes.selected = stateInfo["attributes"]["effect"]
2024-01-28 18:30:01 +02:00
var options = {}
2024-01-28 18:30:01 +02:00
for effect in stateInfo["attributes"]["effect_list"]:
options[effect] = effect
2024-01-28 18:30:01 +02:00
modes.options = options
2024-01-28 18:30:01 +02:00
modes.on_select.connect(func(option):
HomeApi.set_state(entity_id, "on", {"effect": option})
2024-01-28 18:30:01 +02:00
)
if stateInfo.has("attributes")&&stateInfo["attributes"].has("supported_color_modes")&&stateInfo["attributes"]["supported_color_modes"].has("rgb"):
color_wheel_supported.value = true
2024-01-28 18:13:32 +02:00
await HomeApi.watch_state(entity_id, func(new_state):
set_state(new_state)
)
2024-01-28 18:13:32 +02:00
color_wheel.on_color_changed.connect(func(new_color):
if color.value == new_color:
return
var attributes={
"rgb_color": [int(new_color.r * 255), int(new_color.g * 255), int(new_color.b * 255)],
}
2024-01-28 18:13:32 +02:00
snap_sound.play()
2024-05-09 13:40:41 +03:00
print("set color", new_color, attributes["rgb_color"])
2024-01-28 18:13:32 +02:00
HomeApi.set_state(entity_id, "on", attributes)
2023-11-12 18:36:23 +02:00
)
2023-11-29 01:16:25 +02:00
slider.on_value_changed.connect(func(new_value):
var value=new_value / 100 * 255
2024-04-18 13:35:57 +03:00
HomeApi.set_state(entity_id, "on" if active.value else "off", {"brightness": int(value)})
2023-11-29 01:16:25 +02:00
)
2024-04-18 13:35:57 +03:00
func set_state(stateInfo):
if active.value == false&&stateInfo["state"] == "off":
2023-11-29 01:16:25 +02:00
return
2024-04-18 13:35:57 +03:00
var attributes = stateInfo["attributes"]
active.value = stateInfo["state"] == "on"
2024-04-18 13:35:57 +03:00
if attributes.has("brightness")&&attributes["brightness"] != null:
brightness.value = attributes["brightness"]
slider.value = attributes["brightness"] / 255.0 * 100
if attributes.has("rgb_color")&&attributes["rgb_color"] != null:
2024-04-18 13:35:57 +03:00
color.value = Color(attributes["rgb_color"][0] / 255.0, attributes["rgb_color"][1] / 255.0, attributes["rgb_color"][2] / 255.0, 1)
print("got color", color.value, attributes["rgb_color"])
color_wheel.color = color.value
var tween = create_tween()
var target_color = color_off
2023-11-14 01:51:48 +02:00
2024-04-18 13:35:57 +03:00
if active.value:
if brightness.value == null:
target_color = color.value if show_color_wheel.value else color_on
2023-11-14 01:51:48 +02:00
else:
target_color = color_off.lerp(color.value if show_color_wheel.value else color_on, brightness.value / 255.0)
2023-11-14 01:51:48 +02:00
icon_color.value = target_color
tween.tween_property(lightbulb, "material_override:albedo_color", target_color, 0.3)
2023-11-05 22:32:50 +02:00
func _on_click(event):
2023-11-12 18:36:23 +02:00
if event.target == self:
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():
2024-05-02 20:35:47 +03:00
HomeApi.set_state(entity_id, "off" if active.value else "on")
func get_options():
return {
"color_wheel": show_color_wheel.value,
"brightness": show_brightness.value,
"modes": show_modes.value,
}
func set_options(options):
if options.has("color_wheel"): show_color_wheel.value = options["color_wheel"]
if options.has("brightness"): show_brightness.value = options["brightness"]
if options.has("modes"): show_modes.value = options["modes"]