Skip to content

Commit

Permalink
add binary sensor support and fix areas
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitwel committed Jun 12, 2024
1 parent 92a19eb commit f533b8a
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 11 deletions.
39 changes: 39 additions & 0 deletions app/content/entities/binary_sensor/binary_sensor.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extends Entity

const Entity = preload ("../entity.gd")

@onready var label: Label3D = $Label
@onready var state: Label3D = $State
@onready var collision_shape = $CollisionShape3D

# Called when the node enters the scene tree for the first time.
func _ready():
super()

icon.value = "sensors"

var stateInfo = await HomeApi.get_state(entity_id)
set_text(stateInfo)

await HomeApi.watch_state(entity_id, func(new_state):
set_text(new_state)
)

func set_text(stateInfo):
if stateInfo == null:
return

state.text = "check_box" if stateInfo["state"] == "True" else "check_box_outline_blank"

if stateInfo["attributes"]["friendly_name"] != null:
label.text = stateInfo["attributes"]["friendly_name"]

var state_font = state.get_font()
var state_size = state_font.get_string_size(state.text, HORIZONTAL_ALIGNMENT_LEFT, -1, state.font_size) * state.pixel_size
var label_font = label.get_font()
var label_size = label_font.get_string_size(label.text, HORIZONTAL_ALIGNMENT_RIGHT, -1, label.font_size) * label.pixel_size

var size = Vector2(max(state_size.x, label_size.x) * 0.5, (state_size.y + label_size.y) * 0.25)

collision_shape.shape.size.x = size.x
collision_shape.shape.size.y = size.y
37 changes: 37 additions & 0 deletions app/content/entities/binary_sensor/binary_sensor.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[gd_scene load_steps=6 format=3 uid="uid://bdnxxqng3s6l"]

[ext_resource type="Script" path="res://content/entities/binary_sensor/binary_sensor.gd" id="1_ac2fh"]
[ext_resource type="FontFile" uid="uid://cs508knjj1lnw" path="res://assets/fonts/Raleway-Medium.ttf" id="2_dqk35"]
[ext_resource type="Script" path="res://content/functions/movable.gd" id="3_mxubb"]
[ext_resource type="FontVariation" uid="uid://sshfnckriqxn" path="res://assets/icons/icons.tres" id="3_pen57"]

[sub_resource type="BoxShape3D" id="BoxShape3D_mjnxo"]
resource_local_to_scene = true
size = Vector3(0.18, 0.03, 0.02)

[node name="BinarySensor" type="StaticBody3D"]
script = ExtResource("1_ac2fh")

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("BoxShape3D_mjnxo")

[node name="Label" type="Label3D" parent="."]
transform = Transform3D(0.4, 0, 0, 0, 0.4, 0, 0, 0, 0.4, 0, 0.02, 0)
pixel_size = 0.001
text = "some text
"
font = ExtResource("2_dqk35")
font_size = 80
outline_size = 0

[node name="State" type="Label3D" parent="."]
transform = Transform3D(0.4, 0, 0, 0, 0.4, 0, 0, 0, 0.4, 0, -0.02, 0)
pixel_size = 0.001
text = "check_box"
font = ExtResource("3_pen57")
font_size = 80
outline_size = 0

[node name="Movable" type="Node" parent="."]
script = ExtResource("3_mxubb")
resizable = true
2 changes: 1 addition & 1 deletion app/content/entities/button/button.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ func set_state(state):
button.label = name

func quick_action():
HomeApi.set_state(entity_id, "pressed")
HomeApi.set_state(entity_id, "pressed")
4 changes: 2 additions & 2 deletions app/content/entities/sensor/sensor.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=6 format=3 uid="uid://xsiy71rsqulj"]

[ext_resource type="Script" path="res://content/entities/sensor/sensor.gd" id="1_57ac8"]
[ext_resource type="FontFile" uid="uid://drsixxc4tmvjf" path="res://assets/fonts/Montserrat-Medium.ttf" id="2_3sfs5"]
[ext_resource type="Script" path="res://content/functions/movable.gd" id="2_fpq5q"]
[ext_resource type="FontFile" uid="uid://cs508knjj1lnw" path="res://assets/fonts/Raleway-Medium.ttf" id="2_xk6mp"]
[ext_resource type="PackedScene" uid="uid://bsjqdvkt0u87c" path="res://content/ui/components/button/button.tscn" id="5_bmtkc"]

[sub_resource type="BoxShape3D" id="BoxShape3D_phuot"]
Expand All @@ -21,7 +21,7 @@ transform = Transform3D(0.4, 0, 0, 0, 0.4, 0, 0, 0, 0.4, 0, 0, 0)
pixel_size = 0.001
text = "some text
"
font = ExtResource("2_3sfs5")
font = ExtResource("2_xk6mp")
font_size = 80
outline_size = 0

Expand Down
17 changes: 11 additions & 6 deletions app/content/system/areas/areas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func _ready():
Store.house.on_loaded.connect(func():
load_areas()
)

HomeApi.on_connect.connect(func():
sync_areas()
)

func load_areas():
var areas = Store.house.state.areas
Expand All @@ -27,9 +31,7 @@ func load_areas():
for child in get_children():
remove_child(child)
child.queue_free()

print("Loading areas: ", areas)


for area in areas:
var area_scene = AreaScene.instantiate()
area_scene.id = area.id
Expand All @@ -40,10 +42,13 @@ func load_areas():
area_scene.global_rotation = area.rotation
area_scene.size = area.size

if HomeApi.has_integration() == false:
continue
func sync_areas():
if HomeApi.has_integration() == false:
print("No integration found")
return

HomeApi.api.integration_handler.create_area.call_deferred(area.id, area.name)
for area in get_children():
HomeApi.api.integration_handler.create_area.call_deferred(area.id, area.display_name)

func create_area(name: String):
var area = AreaScene.instantiate()
Expand Down
2 changes: 1 addition & 1 deletion app/lib/home_apis/hass_ws/connection.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ signal on_packed_received(packet: Dictionary)

signal _try_connect(success: bool)

const LOG_SENDING := true
const LOG_SENDING := false
const LOG_RECEIVING := false

var socket := WebSocketPeer.new()
Expand Down
1 change: 0 additions & 1 deletion app/lib/home_apis/hass_ws/handlers/integration.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var integration_exists: bool = false

func _init(hass: HASS_API):
self.api = hass
test_integration.call_deferred()

func test_integration():
var response = await api.connection.send_request_packet({
Expand Down
2 changes: 2 additions & 0 deletions app/lib/home_apis/hass_ws/hass.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func _init(url: String, token: String):
assist_handler = AssistHandler.new(self)
history_handler = HistoryHandler.new(self)

await integration_handler.test_integration()

start_subscriptions()

devices_template = devices_template.replace("\n", " ").replace("\t", "").replace("\r", " ")
Expand Down
5 changes: 5 additions & 0 deletions app/lib/utils/entity_factory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const NumberEntity = preload ("res://content/entities/number/number.tscn")
const LineGraphEntity = preload ("res://content/entities/line_chart/line_chart.tscn")
const TimerEntity = preload ("res://content/entities/timer/timer.tscn")
const WeatherEntity = preload ("res://content/entities/weather/weather.tscn")
const BinarySensorEntity = preload ("res://content/entities/binary_sensor/binary_sensor.tscn")

static func create_entity(id: String, type=null):
var entity = null
Expand Down Expand Up @@ -40,6 +41,8 @@ static func create_entity(id: String, type=null):
entity = TimerEntity.instantiate()
"weather":
entity = WeatherEntity.instantiate()
"binary_sensor":
entity = BinarySensorEntity.instantiate()
_:
return null

Expand All @@ -54,6 +57,8 @@ static func get_entity_icon(type: String) -> String:
return "lightbulb"
"sensor":
return "sensors"
"binary_sensor":
return "sensors"
"media_player":
return "play_circle"
"camera":
Expand Down

0 comments on commit f533b8a

Please sign in to comment.