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

82 lines
2.0 KiB
GDScript3
Raw Normal View History

2024-01-27 16:13:43 +02:00
extends Entity
2024-03-12 15:05:45 +02:00
const Entity = preload ("../entity.gd")
@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
2024-04-05 20:29:18 +03:00
var sensor_data = {}
var unit = null
2024-04-11 17:51:30 +03:00
var is_text = true
2024-04-05 20:29:18 +03:00
# Called when the node enters the scene tree for the first time.
func _ready():
2024-01-27 16:13:43 +02:00
super()
icon.value = "sensors"
var stateInfo = await HomeApi.get_state(entity_id)
2023-11-14 01:51:48 +02:00
set_text(stateInfo)
await HomeApi.watch_state(entity_id, func(new_state):
2023-11-14 01:51:48 +02:00
set_text(new_state)
)
2024-04-11 17:51:30 +03:00
remove_child(chart_button)
chart_button.on_button_down.connect(func():
House.body.create_entity(entity_id, global_position, "line_chart")
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:
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]
func get_sensor_unit(type: String):
if sensor_data.has(type) == false:
return null
return unit