From 9e4315d8cca15fdf5b9ba81c13c1f84f5e9454a6 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Tue, 12 Mar 2024 14:51:09 +0100 Subject: [PATCH] add number entity --- content/entities/number/number.gd | 37 +++++++++++++++++++++++++++++ content/entities/number/number.tscn | 27 +++++++++++++++++++++ lib/globals/home_api.gd | 10 ++++---- lib/home_apis/hass_ws/hass.gd | 5 +++- lib/utils/entity_factory.gd | 15 +++++++----- 5 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 content/entities/number/number.gd create mode 100644 content/entities/number/number.tscn diff --git a/content/entities/number/number.gd b/content/entities/number/number.gd new file mode 100644 index 0000000..b282a25 --- /dev/null +++ b/content/entities/number/number.gd @@ -0,0 +1,37 @@ +extends Entity + +const Entity = preload ("../entity.gd") + +@onready var slider = $Slider + +# Called when the node enters the scene tree for the first time. +func _ready(): + super() + + var stateInfo = await HomeApi.get_state(entity_id) + if stateInfo == null: + return + + set_state(stateInfo) + + await HomeApi.watch_state(entity_id, func(new_state): + set_state(new_state) + ) + + slider.on_value_changed.connect(func(value): + HomeApi.set_state(entity_id, value) + ) + +func set_state(state): + slider.value = float(state["state"]) + + var attributes = state["attributes"] + + if attributes.has("min"): + slider.min = float(attributes["min"]) + + if attributes.has("max"): + slider.max = float(attributes["max"]) + + if attributes.has("step"): + slider.step = float(attributes["step"]) diff --git a/content/entities/number/number.tscn b/content/entities/number/number.tscn new file mode 100644 index 0000000..fd0e532 --- /dev/null +++ b/content/entities/number/number.tscn @@ -0,0 +1,27 @@ +[gd_scene load_steps=6 format=3 uid="uid://bbwedgq63bj84"] + +[ext_resource type="Script" path="res://content/entities/number/number.gd" id="1_26xwp"] +[ext_resource type="PackedScene" uid="uid://pk5k1q8bx0rj" path="res://content/ui/components/slider/slider.tscn" id="2_sninv"] +[ext_resource type="Script" path="res://content/functions/movable.gd" id="3_x8wda"] +[ext_resource type="Script" path="res://content/functions/occludable.gd" id="4_3xwop"] + +[sub_resource type="BoxShape3D" id="BoxShape3D_7mk8w"] +size = Vector3(0.0390625, 0.114258, 0.0142822) + +[node name="Number" type="StaticBody3D"] +script = ExtResource("1_26xwp") + +[node name="Slider" parent="." instance=ExtResource("2_sninv")] +transform = Transform3D(8.74228e-08, 4.37114e-08, 1, 1, 4.37114e-08, -8.74228e-08, -4.37114e-08, 1, -4.37114e-08, 0, 0, 0) +value = 0.0 +size = Vector3(20, 0.8, 2) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.00762939) +shape = SubResource("BoxShape3D_7mk8w") + +[node name="Movable" type="Node" parent="."] +script = ExtResource("3_x8wda") + +[node name="Occludable" type="Node" parent="."] +script = ExtResource("4_3xwop") diff --git a/lib/globals/home_api.gd b/lib/globals/home_api.gd index c922e5a..3f4b523 100644 --- a/lib/globals/home_api.gd +++ b/lib/globals/home_api.gd @@ -1,8 +1,7 @@ extends Node -const Hass = preload("res://lib/home_apis/hass/hass.gd") -const HassWebSocket = preload("res://lib/home_apis/hass_ws/hass.gd") - +const Hass = preload ("res://lib/home_apis/hass/hass.gd") +const HassWebSocket = preload ("res://lib/home_apis/hass_ws/hass.gd") const apis = { "hass": Hass, @@ -28,7 +27,6 @@ func _ready(): if success: start_adapter(Store.settings.type.to_lower(), Store.settings.url, Store.settings.token) - func start_adapter(type: String, url: String, token: String): print("Starting adapter: %s" % type) @@ -81,7 +79,7 @@ func get_state(entity: String): return await api.get_state(entity) ## Updates the state of the entity and returns the resulting state -func set_state(entity: String, state: String, attributes: Dictionary = {}): +func set_state(entity: String, state: Variant, attributes: Dictionary={}): assert(has_connected(), "Not connected") return await api.set_state(entity, state, attributes) @@ -91,6 +89,6 @@ func watch_state(entity: String, callback: Callable): return api.watch_state(entity, callback) func _notification(what): - if what == NOTIFICATION_WM_CLOSE_REQUEST || what == NOTIFICATION_WM_GO_BACK_REQUEST: + if what == NOTIFICATION_WM_CLOSE_REQUEST||what == NOTIFICATION_WM_GO_BACK_REQUEST: # Store.house.save_local() pass diff --git a/lib/home_apis/hass_ws/hass.gd b/lib/home_apis/hass_ws/hass.gd index 2755574..ceca25e 100644 --- a/lib/home_apis/hass_ws/hass.gd +++ b/lib/home_apis/hass_ws/hass.gd @@ -213,7 +213,7 @@ func watch_state(entity: String, callback: Callable): return func(): entitiy_callbacks.remove(entity, callback) -func set_state(entity: String, state: String, attributes: Dictionary={}): +func set_state(entity: String, state: Variant, attributes: Dictionary={}): var domain = entity.split(".")[0] var service: String @@ -241,6 +241,9 @@ func set_state(entity: String, state: String, attributes: Dictionary={}): elif domain == 'button': if state == 'pressed': service = 'press' + elif domain == 'number': + service = 'set_value' + attributes["value"] = state if service == null: return null diff --git a/lib/utils/entity_factory.gd b/lib/utils/entity_factory.gd index 8850d2d..3d56341 100644 --- a/lib/utils/entity_factory.gd +++ b/lib/utils/entity_factory.gd @@ -1,12 +1,13 @@ extends RefCounted class_name EntityFactory -const Switch = preload("res://content/entities/switch/switch.tscn") -const Light = preload("res://content/entities/light/light.tscn") -const Sensor = preload("res://content/entities/sensor/sensor.tscn") -const MediaPlayer = preload("res://content/entities/media_player/media_player.tscn") -const Camera = preload("res://content/entities/camera/camera.tscn") -const ButtonEntity = preload("res://content/entities/button/button.tscn") +const Switch = preload ("res://content/entities/switch/switch.tscn") +const Light = preload ("res://content/entities/light/light.tscn") +const Sensor = preload ("res://content/entities/sensor/sensor.tscn") +const MediaPlayer = preload ("res://content/entities/media_player/media_player.tscn") +const Camera = preload ("res://content/entities/camera/camera.tscn") +const ButtonEntity = preload ("res://content/entities/button/button.tscn") +const NumberEntity = preload ("res://content/entities/number/number.tscn") static func create_entity(id: String): var entity = null @@ -25,6 +26,8 @@ static func create_entity(id: String): entity = Camera.instantiate() "button": entity = ButtonEntity.instantiate() + "number": + entity = NumberEntity.instantiate() _: return null