clean up light and switch

This commit is contained in:
Nitwel 2024-04-18 12:35:57 +02:00
parent 90cfe87ae8
commit 8d1a380977
4 changed files with 183 additions and 199 deletions

View File

@ -15,9 +15,9 @@ const color_wheel_img := preload ("res://assets/canvas.png")
@onready var mode_before = $Modes/Previous @onready var mode_before = $Modes/Previous
@onready var mode_label = $Modes/Label @onready var mode_label = $Modes/Label
var state = true var active = R.state(false)
var brightness = 0 # 0-255 var brightness = R.state(0) # 0-255
var color = color_on var color = R.state(color_on)
var color_supported = false var color_supported = false
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
@ -26,7 +26,7 @@ func _ready():
icon.value = "lightbulb" icon.value = "lightbulb"
var stateInfo = await HomeApi.get_state(entity_id) var stateInfo = await HomeApi.get_state(entity_id)
set_state(stateInfo["state"] == "on", stateInfo["attributes"]) set_state(stateInfo)
if stateInfo.has("attributes")&&stateInfo["attributes"].has("effect_list")&&stateInfo["attributes"]["effect_list"].size() > 0: if stateInfo.has("attributes")&&stateInfo["attributes"].has("effect_list")&&stateInfo["attributes"]["effect_list"].size() > 0:
if stateInfo["attributes"].has("effect")&&stateInfo["attributes"]["effect"] != null: if stateInfo["attributes"].has("effect")&&stateInfo["attributes"]["effect"] != null:
@ -66,78 +66,69 @@ func _ready():
if delta.length() > 1: if delta.length() > 1:
delta=delta.normalized() delta=delta.normalized()
print("delta", delta) var picked_color=color_wheel_img.get_image().get_pixel((delta.x * 0.5 + 0.5) * 1000, (delta.y * 0.5 + 0.5) * 1000)
var color=color_wheel_img.get_image().get_pixel((delta.x * 0.5 + 0.5) * 1000, (delta.y * 0.5 + 0.5) * 1000) color_puck.material_override.albedo_color=picked_color
print("color", color)
color_puck.material_override.albedo_color=color
color_puck.position=Vector3(target_point.x, color_puck.position.y, target_point.z) color_puck.position=Vector3(target_point.x, color_puck.position.y, target_point.z)
var attributes={ var attributes={
"rgb_color": [int(color.r * 255), int(color.g * 255), int(color.b * 255)], "rgb_color": [int(picked_color.r * 255), int(picked_color.g * 255), int(picked_color.b * 255)],
} }
HomeApi.set_state(entity_id, "on", attributes) HomeApi.set_state(entity_id, "on", attributes)
set_state(state, attributes)
) )
color_supported = true color_supported = true
else: else:
remove_child(color_wheel) remove_child(color_wheel)
await HomeApi.watch_state(entity_id, func(new_state): await HomeApi.watch_state(entity_id, func(new_state):
if (new_state["state"] == "on") == state: set_state(new_state)
return
set_state(new_state["state"] == "on", new_state["attributes"])
) )
slider.on_value_changed.connect(func(new_value): slider.on_value_changed.connect(func(new_value):
var value=new_value / 100 * 255 var value=new_value / 100 * 255
HomeApi.set_state(entity_id, "on" if state else "off", {"brightness": int(value)}) HomeApi.set_state(entity_id, "on" if active.value else "off", {"brightness": int(value)})
set_state(state, {"brightness": value})
) )
func set_state(new_state: bool, attributes={}): func set_state(stateInfo):
if state == false&&new_state == false: if active.value == false&&stateInfo["state"] == "off":
return return
state = new_state var attributes = stateInfo["attributes"]
if attributes.has("brightness"): active.value = stateInfo["state"] == "on"
brightness = attributes["brightness"]
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: if attributes.has("rgb_color")&&attributes["rgb_color"] != null:
color = Color(attributes["rgb_color"][0] / 255.0, attributes["rgb_color"][1] / 255.0, attributes["rgb_color"][2] / 255.0, 1) color.value = Color(attributes["rgb_color"][0] / 255.0, attributes["rgb_color"][1] / 255.0, attributes["rgb_color"][2] / 255.0, 1)
var tween = create_tween() var tween = create_tween()
var target_color = color_off var target_color = color_off
if state: if active.value:
if brightness == null: if brightness.value == null:
target_color = color if color_supported else color_on target_color = color.value if color_supported else color_on
else: else:
target_color = color_off.lerp(color if color_supported else color_on, brightness / 255.0) target_color = color_off.lerp(color.value if color_supported else color_on, brightness.value / 255.0)
icon_color.value = target_color icon_color.value = target_color
tween.tween_property(lightbulb, "material_override:albedo_color", target_color, 0.3) tween.tween_property(lightbulb, "material_override:albedo_color", target_color, 0.3)
func _on_click(event): func _on_click(event):
if event.target == self: if event.target == self:
var attributes = {} _toggle()
if !state&&brightness != null:
attributes["brightness"] = int(brightness)
HomeApi.set_state(entity_id, "on" if !state else "off", attributes)
set_state(!state, attributes)
func quick_action(): func quick_action():
_toggle()
func _toggle():
var attributes = {} var attributes = {}
if !state&&brightness != null: if !active.value&&brightness.value != null:
attributes["brightness"] = int(brightness) attributes["brightness"] = int(brightness.value)
HomeApi.set_state(entity_id, "on" if !state else "off", attributes) HomeApi.set_state(entity_id, "off" if active.value else "on", attributes)
set_state(!state, attributes)

View File

@ -4,42 +4,35 @@ const Entity = preload ("../entity.gd")
@onready var sprite: AnimatedSprite3D = $Icon @onready var sprite: AnimatedSprite3D = $Icon
var active = R.state(false)
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
super() super()
var stateInfo = await HomeApi.get_state(entity_id) var stateInfo = await HomeApi.get_state(entity_id)
set_state(stateInfo)
await HomeApi.watch_state(entity_id, func(new_state):
set_state(new_state)
)
R.effect(func(_arg):
sprite.set_frame(1 if active.value else 0)
)
func set_state(stateInfo):
if stateInfo == null: if stateInfo == null:
return return
if stateInfo["state"] == "on": active.value = stateInfo["state"] == "on"
sprite.set_frame(0)
else:
sprite.set_frame(1)
icon.value = "toggle_" + stateInfo["state"] icon.value = "toggle_" + stateInfo["state"]
await HomeApi.watch_state(entity_id, func(new_state):
if new_state["state"] == "on":
sprite.set_frame(0)
else:
sprite.set_frame(1)
icon.value="toggle_" + new_state["state"]
)
func _on_click(_event): func _on_click(_event):
HomeApi.set_state(entity_id, "off" if sprite.get_frame() == 0 else "on") _toggle()
if sprite.get_frame() == 0:
sprite.set_frame(1)
else:
sprite.set_frame(0)
func _on_request_completed():
pass
func quick_action(): func quick_action():
HomeApi.set_state(entity_id, "off" if sprite.get_frame() == 0 else "on") _toggle()
if sprite.get_frame() == 0:
sprite.set_frame(1) func _toggle():
else: HomeApi.set_state(entity_id, "off" if active.value else "on")
sprite.set_frame(0)

View File

@ -13,17 +13,17 @@ radius = 0.0482081
animations = [{ animations = [{
"frames": [{ "frames": [{
"duration": 1.0, "duration": 1.0,
"texture": ExtResource("1_w68gw") "texture": ExtResource("2_86ba1")
}, { }, {
"duration": 1.0, "duration": 1.0,
"texture": ExtResource("2_86ba1") "texture": ExtResource("1_w68gw")
}], }],
"loop": true, "loop": true,
"name": &"default", "name": &"default",
"speed": 5.0 "speed": 5.0
}] }]
[node name="Switch" type="StaticBody3D" ] [node name="Switch" type="StaticBody3D"]
collision_mask = 0 collision_mask = 0
script = ExtResource("1_8ffhi") script = ExtResource("1_8ffhi")