immersive-home/lib/home_adapters/adapter.gd
2023-11-21 11:53:13 +01:00

61 lines
1.5 KiB
GDScript

extends Node
const Hass = preload("res://lib/home_adapters/hass/hass.gd")
const HassWebSocket = preload("res://lib/home_adapters/hass_ws/hass.gd")
enum ADAPTER_TYPES {
HASS,
HASS_WS
}
const adapters = {
ADAPTER_TYPES.HASS: Hass,
ADAPTER_TYPES.HASS_WS: HassWebSocket
}
const methods = [
"get_devices",
"get_device",
"get_state",
"set_state",
"watch_state"
]
var adapter: Node
func _init(type: ADAPTER_TYPES):
var clipboard := DisplayServer.clipboard_get()
if clipboard != null && clipboard.find(" ") != -1:
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()
add_child(adapter)
for method in methods:
assert(adapter.has_method(method), "Adapter does not implement method: " + method)
## 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)
## Updates the state of the entity and returns the resulting state
func set_state(entity: String, state: String, attributes: Dictionary = {}):
return await adapter.set_state(entity, state, attributes)
## Watches the state and each time it changes, calls the callback with the changed state, returns a function to stop watching the state
func watch_state(entity: String, callback: Callable):
return adapter.watch_state(entity, callback)