add number entity

This commit is contained in:
Nitwel 2024-03-12 14:51:09 +01:00
parent 94cd817d56
commit 9e4315d8cc
5 changed files with 81 additions and 13 deletions

View File

@ -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"])

View File

@ -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")

View File

@ -1,8 +1,7 @@
extends Node extends Node
const Hass = preload("res://lib/home_apis/hass/hass.gd") const Hass = preload ("res://lib/home_apis/hass/hass.gd")
const HassWebSocket = preload("res://lib/home_apis/hass_ws/hass.gd") const HassWebSocket = preload ("res://lib/home_apis/hass_ws/hass.gd")
const apis = { const apis = {
"hass": Hass, "hass": Hass,
@ -28,7 +27,6 @@ func _ready():
if success: if success:
start_adapter(Store.settings.type.to_lower(), Store.settings.url, Store.settings.token) start_adapter(Store.settings.type.to_lower(), Store.settings.url, Store.settings.token)
func start_adapter(type: String, url: String, token: String): func start_adapter(type: String, url: String, token: String):
print("Starting adapter: %s" % type) print("Starting adapter: %s" % type)
@ -81,7 +79,7 @@ func get_state(entity: String):
return await api.get_state(entity) return await api.get_state(entity)
## Updates the state of the entity and returns the resulting state ## 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") assert(has_connected(), "Not connected")
return await api.set_state(entity, state, attributes) 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) return api.watch_state(entity, callback)
func _notification(what): 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() # Store.house.save_local()
pass pass

View File

@ -213,7 +213,7 @@ func watch_state(entity: String, callback: Callable):
return func(): return func():
entitiy_callbacks.remove(entity, callback) 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 domain = entity.split(".")[0]
var service: String var service: String
@ -241,6 +241,9 @@ func set_state(entity: String, state: String, attributes: Dictionary={}):
elif domain == 'button': elif domain == 'button':
if state == 'pressed': if state == 'pressed':
service = 'press' service = 'press'
elif domain == 'number':
service = 'set_value'
attributes["value"] = state
if service == null: if service == null:
return null return null

View File

@ -1,12 +1,13 @@
extends RefCounted extends RefCounted
class_name EntityFactory class_name EntityFactory
const Switch = preload("res://content/entities/switch/switch.tscn") const Switch = preload ("res://content/entities/switch/switch.tscn")
const Light = preload("res://content/entities/light/light.tscn") const Light = preload ("res://content/entities/light/light.tscn")
const Sensor = preload("res://content/entities/sensor/sensor.tscn") const Sensor = preload ("res://content/entities/sensor/sensor.tscn")
const MediaPlayer = preload("res://content/entities/media_player/media_player.tscn") const MediaPlayer = preload ("res://content/entities/media_player/media_player.tscn")
const Camera = preload("res://content/entities/camera/camera.tscn") const Camera = preload ("res://content/entities/camera/camera.tscn")
const ButtonEntity = preload("res://content/entities/button/button.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): static func create_entity(id: String):
var entity = null var entity = null
@ -25,6 +26,8 @@ static func create_entity(id: String):
entity = Camera.instantiate() entity = Camera.instantiate()
"button": "button":
entity = ButtonEntity.instantiate() entity = ButtonEntity.instantiate()
"number":
entity = NumberEntity.instantiate()
_: _:
return null return null