greendeck/greendeck/mpristest.py
2023-01-02 16:25:54 -06:00

86 lines
2.4 KiB
Python

import asyncio
import logging
import pathlib
import asyncdbus
import asyncdbus.message_bus
import click
from greendeck.config import Config
from greendeck.lib.logging import setup_logging
logger = logging.getLogger(__name__)
async def _main(config: Config):
async with asyncdbus.message_bus.MessageBus().connect() as bus:
bus: asyncdbus.message_bus.MessageBus
# print(type(bus))
# print(dir(bus))
# print(help(bus.introspect))
i = await bus.introspect("org.freedesktop.DBus", "/org/freedesktop/DBus")
obj = await bus.get_proxy_object(
"org.freedesktop.DBus", "/org/freedesktop/DBus", i
)
# n = await obj.ListNames()
dbus = await obj.get_interface("org.freedesktop.DBus")
names = await dbus.call_list_names()
found = False
for name in names:
if name.startswith("org.mpris.MediaPlayer2."):
logger.info(f"{name}")
found = True
logger.info(f"{found}")
introspection = await bus.introspect(
"org.mpris.MediaPlayer2.Plexamp", "/org/mpris/MediaPlayer2"
)
obj = await bus.get_proxy_object(
"org.mpris.MediaPlayer2.Plexamp", "/org/mpris/MediaPlayer2", introspection
)
player = await obj.get_interface("org.mpris.MediaPlayer2.Player")
properties = await obj.get_interface("org.freedesktop.DBus.Properties")
logger.info(f"{dir(properties)}")
p = await properties.call_get_all("org.mpris.MediaPlayer2.Player")
logger.info(f"{p}")
def on_properties_changed(
interface_name, changed_properties, invalidated_properties
):
for changed, variant in changed_properties.items():
logger.info(f"property changed: {changed} - {variant.value}")
await properties.on_properties_changed(on_properties_changed)
await asyncio.sleep(1000000)
@click.command
@click.option(
"--config",
"config_path",
type=click.Path(
exists=True,
file_okay=True,
dir_okay=False,
allow_dash=False,
path_type=pathlib.Path,
),
default="./config.yaml",
)
def main(
config_path: pathlib.Path,
):
setup_logging()
config = Config.parse_raw(config_path.read_bytes(), proto="yaml")
asyncio.run(_main(config))
if __name__ == "__main__":
main()