add number entity
This commit is contained in:
parent
94cd817d56
commit
9e4315d8cc
37
content/entities/number/number.gd
Normal file
37
content/entities/number/number.gd
Normal 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"])
|
27
content/entities/number/number.tscn
Normal file
27
content/entities/number/number.tscn
Normal 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")
|
|
@ -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,
|
||||
|
@ -29,7 +28,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)
|
||||
if api != null:
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user