2024-01-27 16:13:43 +02:00
|
|
|
extends Entity
|
|
|
|
|
2024-03-07 15:21:31 +02:00
|
|
|
const Entity = preload ("../entity.gd")
|
2023-11-26 03:01:27 +02:00
|
|
|
|
2023-11-29 01:16:25 +02:00
|
|
|
@export var image_width = 0.15
|
2023-11-26 03:01:27 +02:00
|
|
|
|
|
|
|
@onready var previous = $Previous
|
|
|
|
@onready var next = $Next
|
|
|
|
@onready var play = $Play
|
|
|
|
@onready var logo = $PlayingInfo/Logo
|
|
|
|
@onready var title = $PlayingInfo/Title
|
|
|
|
@onready var artist = $PlayingInfo/Artist
|
|
|
|
@onready var http_request = $PlayingInfo/HTTPRequest
|
2023-11-29 01:47:11 +02:00
|
|
|
@onready var slider = $Slider
|
2023-11-26 03:01:27 +02:00
|
|
|
|
|
|
|
var playing = false
|
2023-11-29 01:47:11 +02:00
|
|
|
var volume = 50
|
|
|
|
|
2023-11-26 03:01:27 +02:00
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2024-01-27 16:13:43 +02:00
|
|
|
super()
|
|
|
|
|
2024-04-17 17:49:58 +03:00
|
|
|
icon.value = "pause_circle"
|
|
|
|
|
2023-11-26 03:01:27 +02:00
|
|
|
var stateInfo = await HomeApi.get_state(entity_id)
|
|
|
|
set_state(stateInfo)
|
|
|
|
|
|
|
|
await HomeApi.watch_state(entity_id, func(new_state):
|
|
|
|
set_state(new_state)
|
|
|
|
)
|
|
|
|
|
|
|
|
previous.on_button_down.connect(func():
|
|
|
|
HomeApi.set_state(entity_id, "previous")
|
|
|
|
)
|
|
|
|
|
|
|
|
play.on_button_down.connect(func():
|
|
|
|
if playing:
|
|
|
|
HomeApi.set_state(entity_id, "pause")
|
|
|
|
else:
|
|
|
|
HomeApi.set_state(entity_id, "play")
|
|
|
|
)
|
|
|
|
|
|
|
|
next.on_button_down.connect(func():
|
|
|
|
HomeApi.set_state(entity_id, "next")
|
|
|
|
)
|
|
|
|
|
2023-11-29 01:47:11 +02:00
|
|
|
slider.on_value_changed.connect(set_volume)
|
|
|
|
|
|
|
|
func set_volume(value):
|
|
|
|
volume = value
|
|
|
|
HomeApi.set_state(entity_id, "volume", {"volume_level": value / 100})
|
2023-11-26 03:01:27 +02:00
|
|
|
|
|
|
|
func set_state(stateInfo):
|
2024-03-07 15:21:31 +02:00
|
|
|
if stateInfo == null:
|
|
|
|
return
|
|
|
|
|
2023-11-26 03:01:27 +02:00
|
|
|
var state = stateInfo["state"]
|
|
|
|
|
|
|
|
if state == "playing":
|
|
|
|
if stateInfo["attributes"].has("entity_picture_local"):
|
|
|
|
load_image(stateInfo["attributes"]["entity_picture_local"])
|
|
|
|
title.text = stateInfo["attributes"]["media_title"]
|
|
|
|
artist.text = stateInfo["attributes"]["media_artist"]
|
|
|
|
|
2023-11-29 01:47:11 +02:00
|
|
|
volume = float(stateInfo["attributes"]["volume_level"]) * 100
|
|
|
|
slider.value = volume
|
|
|
|
|
2023-11-26 03:01:27 +02:00
|
|
|
playing = true
|
|
|
|
play.label = "pause"
|
2024-04-17 17:49:58 +03:00
|
|
|
icon.value = "play_circle"
|
2023-11-26 03:01:27 +02:00
|
|
|
else:
|
|
|
|
playing = false
|
|
|
|
play.label = "play_arrow"
|
2024-04-17 17:49:58 +03:00
|
|
|
icon.value = "pause_circle"
|
2023-11-26 03:01:27 +02:00
|
|
|
|
|
|
|
func load_image(url: String):
|
|
|
|
http_request.request("http://192.168.33.33:8123" + url)
|
|
|
|
|
|
|
|
var result = await http_request.request_completed
|
|
|
|
|
|
|
|
if result[0] != HTTPRequest.RESULT_SUCCESS:
|
|
|
|
print("Error loading image: ", result[0], " ", result[1])
|
|
|
|
return
|
|
|
|
|
|
|
|
var image = Image.new()
|
|
|
|
var error = image.load_jpg_from_buffer(result[3])
|
|
|
|
|
2023-11-29 01:16:25 +02:00
|
|
|
var pixel_size = image_width / image.get_size().x
|
|
|
|
|
2023-11-26 03:01:27 +02:00
|
|
|
if error != OK:
|
|
|
|
print("Error loading image: ", error)
|
|
|
|
return
|
|
|
|
|
|
|
|
var texture = ImageTexture.create_from_image(image)
|
|
|
|
logo.texture = texture
|
2024-01-25 17:29:33 +02:00
|
|
|
logo.pixel_size = pixel_size
|