EventsClient renamed to EventClient

remove getter, setter for send.

add persistend data unit test

add hotkey example

default event sub now 0. explicitly define subs in event class.
now subs can be set as kwarg
This commit is contained in:
onyx-and-iris 2022-07-27 19:39:33 +01:00
parent c71d7e4ea9
commit 2a3a86c277
7 changed files with 209 additions and 276 deletions

View File

@ -33,7 +33,7 @@ class Observer:
if __name__ == "__main__": if __name__ == "__main__":
cl = obs.EventsClient() cl = obs.EventClient()
observer = Observer(cl) observer = Observer(cl)
while cmd := input("<Enter> to exit\n"): while cmd := input("<Enter> to exit\n"):

View File

@ -0,0 +1,40 @@
import inspect
import keyboard
import obsstudio_sdk as obs
class Observer:
def __init__(self, cl):
self._cl = cl
self._cl.callback.register(self.on_current_program_scene_changed)
print(f"Registered events: {self._cl.callback.get()}")
@property
def event_identifier(self):
return inspect.stack()[1].function
def on_current_program_scene_changed(self, data):
"""The current program scene has changed."""
print(f"{self.event_identifier}: {data}")
def version():
print(req_cl.get_version())
def set_scene(scene, *args):
req_cl.set_current_program_scene(scene)
if __name__ == "__main__":
req_cl = obs.ReqClient()
req_ev = obs.EventClient()
observer = Observer(req_ev)
keyboard.add_hotkey("1", set_scene, args=("START",))
keyboard.add_hotkey("2", set_scene, args=("BRB",))
keyboard.add_hotkey("3", set_scene, args=("END",))
print("press ctrl+enter to quit")
keyboard.wait("ctrl+enter")

View File

@ -1,4 +1,4 @@
from .events import EventsClient from .events import EventClient
from .reqs import ReqClient from .reqs import ReqClient
__ALL__ = ["ReqClient", "EventsClient"] __ALL__ = ["ReqClient", "EventsClient"]

View File

@ -9,18 +9,13 @@ from random import randint
import tomllib import tomllib
import websocket import websocket
Subs = IntEnum(
"Subs",
"general config scenes inputs transitions filters outputs sceneitems mediainputs vendors ui",
start=0,
)
class ObsClient(object): class ObsClient(object):
DELAY = 0.001 DELAY = 0.001
def __init__(self, **kwargs): def __init__(self, **kwargs):
defaultkwargs = {key: None for key in ["host", "port", "password"]} defaultkwargs = {key: None for key in ["host", "port", "password"]}
defaultkwargs["subs"] = 0
kwargs = defaultkwargs | kwargs kwargs = defaultkwargs | kwargs
for attr, val in kwargs.items(): for attr, val in kwargs.items():
setattr(self, attr, val) setattr(self, attr, val)
@ -59,26 +54,12 @@ class ObsClient(object):
).digest() ).digest()
).decode() ).decode()
all_non_high_volume = (
(1 << Subs.general)
| (1 << Subs.config)
| (1 << Subs.scenes)
| (1 << Subs.inputs)
| (1 << Subs.transitions)
| (1 << Subs.filters)
| (1 << Subs.outputs)
| (1 << Subs.sceneitems)
| (1 << Subs.mediainputs)
| (1 << Subs.vendors)
| (1 << Subs.ui)
)
payload = { payload = {
"op": 1, "op": 1,
"d": { "d": {
"rpcVersion": 1, "rpcVersion": 1,
"authentication": auth, "authentication": auth,
"eventSubscriptions": all_non_high_volume, "eventSubscriptions": self.subs,
}, },
} }
@ -102,7 +83,4 @@ class ObsClient(object):
} }
self.ws.send(json.dumps(payload)) self.ws.send(json.dumps(payload))
response = json.loads(self.ws.recv()) response = json.loads(self.ws.recv())
while "requestId" not in response["d"]:
response = json.loads(self.ws.recv())
time.sleep(self.DELAY)
return response["d"] return response["d"]

View File

@ -1,5 +1,6 @@
import json import json
import time import time
from enum import IntEnum
from threading import Thread from threading import Thread
from .baseclient import ObsClient from .baseclient import ObsClient
@ -11,11 +12,30 @@ defined in official github repo
https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#events https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#events
""" """
Subs = IntEnum(
"Subs",
"general config scenes inputs transitions filters outputs sceneitems mediainputs vendors ui",
start=0,
)
class EventsClient(object):
class EventClient(object):
DELAY = 0.001 DELAY = 0.001
def __init__(self, **kwargs): def __init__(self, **kwargs):
kwargs["subs"] = (
(1 << Subs.general)
| (1 << Subs.config)
| (1 << Subs.scenes)
| (1 << Subs.inputs)
| (1 << Subs.transitions)
| (1 << Subs.filters)
| (1 << Subs.outputs)
| (1 << Subs.sceneitems)
| (1 << Subs.mediainputs)
| (1 << Subs.vendors)
| (1 << Subs.ui)
)
self.base_client = ObsClient(**kwargs) self.base_client = ObsClient(**kwargs)
self.base_client.authenticate() self.base_client.authenticate()
self.callback = Callback() self.callback = Callback()

File diff suppressed because it is too large Load Diff

View File

@ -33,7 +33,19 @@ class TestRequests:
(True), (True),
], ],
) )
def test_set_studio_mode_enabled_true(self, state): def test_studio_mode_enabled(self, state):
req_cl.set_studio_mode_enabled(state) req_cl.set_studio_mode_enabled(state)
resp = req_cl.get_studio_mode_enabled() resp = req_cl.get_studio_mode_enabled()
assert resp["studioModeEnabled"] == state assert resp["studioModeEnabled"] == state
@pytest.mark.parametrize(
"name,data",
[
("val1", 3),
("val2", "hello"),
],
)
def test_persistent_data(self, name, data):
req_cl.set_persistent_data("OBS_WEBSOCKET_DATA_REALM_PROFILE", name, data)
resp = req_cl.get_persistent_data("OBS_WEBSOCKET_DATA_REALM_PROFILE", name)
assert resp["slotValue"] == data