35 lines
919 B
Python
Executable File

#!/usr/bin/env python3
import asyncio
import signal
from contextlib import suppress
import pulsectl_asyncio
import json
async def get_values(pulse):
sink = await pulse.get_sink_by_name("@DEFAULT_SINK@")
result = {
"mute": sink.mute == 1,
"volume": f"{int(sink.volume.value_flat * 100):2}"
}
print(json.dumps(result), flush=True)
return result
async def listen():
async with pulsectl_asyncio.PulseAsync("volume-monitor") as pulse:
await get_values(pulse)
async for _ in pulse.subscribe_events('sink'):
await get_values(pulse)
async def main():
listen_task = asyncio.create_task(listen())
for sig in (signal.SIGTERM, signal.SIGHUP, signal.SIGINT):
loop.add_signal_handler(sig, listen_task.cancel)
with suppress(asyncio.CancelledError):
await listen_task
loop = asyncio.get_event_loop()
loop.run_until_complete(main())