2024-01-27 16:13:43 +02:00
|
|
|
extends Entity
|
|
|
|
|
2024-03-12 15:05:45 +02:00
|
|
|
const Entity = preload ("../entity.gd")
|
2023-11-03 23:00:05 +02:00
|
|
|
|
|
|
|
@onready var label: Label3D = $Label
|
2024-03-12 15:05:45 +02:00
|
|
|
@onready var collision_shape = $CollisionShape3D
|
2024-04-11 17:51:30 +03:00
|
|
|
@onready var chart_button = $Button
|
2023-11-03 23:00:05 +02:00
|
|
|
|
2024-04-05 20:29:18 +03:00
|
|
|
var sensor_data = {}
|
2024-04-10 16:59:27 +03:00
|
|
|
var unit = null
|
2024-04-11 17:51:30 +03:00
|
|
|
var is_text = true
|
2024-04-05 20:29:18 +03:00
|
|
|
|
2023-11-03 23:00:05 +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 = "sensors"
|
|
|
|
|
2023-11-24 02:36:31 +02:00
|
|
|
var stateInfo = await HomeApi.get_state(entity_id)
|
2023-11-14 01:51:48 +02:00
|
|
|
set_text(stateInfo)
|
2023-11-03 23:00:05 +02:00
|
|
|
|
2023-11-24 02:36:31 +02:00
|
|
|
await HomeApi.watch_state(entity_id, func(new_state):
|
2023-11-14 01:51:48 +02:00
|
|
|
set_text(new_state)
|
2023-11-03 23:00:05 +02:00
|
|
|
)
|
|
|
|
|
2024-04-11 17:51:30 +03:00
|
|
|
remove_child(chart_button)
|
|
|
|
|
|
|
|
chart_button.on_button_down.connect(func():
|
2024-05-22 19:38:28 +03:00
|
|
|
App.house.create_entity(entity_id, global_position, "line_chart")
|
2024-04-11 17:51:30 +03:00
|
|
|
remove_child(chart_button)
|
|
|
|
)
|
|
|
|
|
|
|
|
func _on_click(_event):
|
|
|
|
if is_text:
|
|
|
|
return
|
|
|
|
|
|
|
|
if chart_button.is_inside_tree() == false:
|
|
|
|
add_child(chart_button)
|
|
|
|
else:
|
|
|
|
remove_child(chart_button)
|
|
|
|
|
2023-11-14 01:51:48 +02:00
|
|
|
func set_text(stateInfo):
|
2024-04-05 20:29:18 +03:00
|
|
|
if stateInfo == null:
|
|
|
|
return
|
|
|
|
|
2023-11-14 01:51:48 +02:00
|
|
|
var text = stateInfo["state"]
|
|
|
|
|
2024-04-11 17:51:30 +03:00
|
|
|
is_text = text.is_valid_float() == false&&text.is_valid_int() == false
|
|
|
|
|
2023-11-14 01:51:48 +02:00
|
|
|
if stateInfo["attributes"]["friendly_name"] != null:
|
|
|
|
text = stateInfo["attributes"]["friendly_name"] + "\n" + text
|
|
|
|
|
2024-03-12 15:05:45 +02:00
|
|
|
if stateInfo["attributes"].has("unit_of_measurement")&&stateInfo["attributes"]["unit_of_measurement"] != null:
|
2024-04-10 16:59:27 +03:00
|
|
|
unit = stateInfo["attributes"]["unit_of_measurement"]
|
2023-11-14 01:51:48 +02:00
|
|
|
text += " " + stateInfo["attributes"]["unit_of_measurement"]
|
|
|
|
|
2024-04-05 20:29:18 +03:00
|
|
|
if stateInfo["attributes"].has("device_class"):
|
|
|
|
sensor_data[stateInfo["attributes"]["device_class"]] = stateInfo["state"]
|
|
|
|
|
2024-03-12 15:05:45 +02:00
|
|
|
label.text = text
|
|
|
|
|
|
|
|
var font = label.get_font()
|
|
|
|
var width = 0
|
|
|
|
var height = 0
|
|
|
|
|
|
|
|
var size = font.get_multiline_string_size(text, HORIZONTAL_ALIGNMENT_CENTER, label.width, label.font_size)
|
|
|
|
|
|
|
|
collision_shape.shape.size.x = size.x * label.pixel_size * 0.5
|
|
|
|
collision_shape.shape.size.y = size.y * label.pixel_size * 0.25
|
2024-04-05 20:29:18 +03:00
|
|
|
|
|
|
|
func get_sensor_data(type: String):
|
|
|
|
if sensor_data.has(type) == false:
|
|
|
|
return null
|
|
|
|
|
|
|
|
return sensor_data[type]
|
2024-04-10 16:59:27 +03:00
|
|
|
|
|
|
|
func get_sensor_unit(type: String):
|
|
|
|
if sensor_data.has(type) == false:
|
|
|
|
return null
|
|
|
|
|
|
|
|
return unit
|