immersive-home/lib/home_adapters/adapter.gd

61 lines
1.5 KiB
GDScript3
Raw Normal View History

2023-10-30 02:21:50 +02:00
extends Node
2023-11-05 17:36:13 +02:00
const Hass = preload("res://lib/home_adapters/hass/hass.gd")
const HassWebSocket = preload("res://lib/home_adapters/hass_ws/hass.gd")
2023-10-30 02:21:50 +02:00
enum ADAPTER_TYPES {
2023-11-03 00:13:55 +02:00
HASS,
HASS_WS
2023-10-30 02:21:50 +02:00
}
const adapters = {
2023-11-05 17:36:13 +02:00
ADAPTER_TYPES.HASS: Hass,
ADAPTER_TYPES.HASS_WS: HassWebSocket
2023-10-30 02:21:50 +02:00
}
const methods = [
2023-11-05 17:36:13 +02:00
"get_devices",
"get_device",
"get_state",
2023-11-05 17:36:13 +02:00
"set_state",
"watch_state"
2023-10-30 02:21:50 +02:00
]
var adapter: Node
func _init(type: ADAPTER_TYPES):
var clipboard := DisplayServer.clipboard_get()
if clipboard != null:
var clip_url = clipboard.split(" ")[0]
var clip_token = clipboard.split(" ")[1]
adapter = adapters[type].new(clip_url, clip_token)
else:
adapter = adapters[type].new()
2023-11-03 00:13:55 +02:00
add_child(adapter)
2023-10-30 02:21:50 +02:00
for method in methods:
assert(adapter.has_method(method), "Adapter does not implement method: " + method)
2023-11-05 17:36:13 +02:00
## Get a list of all devices
func get_devices():
return await adapter.get_devices()
## Get a single device by id
func get_device(id: String):
return await adapter.get_device(id)
## Returns the current state of an entity
func get_state(entity: String):
return await adapter.get_state(entity)
2023-11-05 17:36:13 +02:00
## Updates the state of the entity and returns the resulting state
func set_state(entity: String, state: String, attributes: Dictionary = {}):
2023-11-03 00:13:55 +02:00
return await adapter.set_state(entity, state, attributes)
2023-11-05 17:36:13 +02:00
## Watches the state and each time it changes, calls the callback with the changed state, returns a function to stop watching the state
2023-11-03 00:13:55 +02:00
func watch_state(entity: String, callback: Callable):
return adapter.watch_state(entity, callback)