diff --git a/darkmute.py b/darkmute.py index dfaf3433b9acade00eae1307b9fdbf105d7a76ef..b1bfb1ec7db681d69eb87979d946c19794764ca4 100644 --- a/darkmute.py +++ b/darkmute.py @@ -1,31 +1,33 @@ -import RPi.GPIO as GPIO +import asyncio import time -import snapcast.control -from snapcast import get_snapcast_client -from snapcast import add_server +import RPi.GPIO as GPIO +from snap import get_snapcast_client + SENSOR_PIN = 7 MUTE_BRIGHTNESS = 0.1 def dark_mute(): - '''Runs measure_brightness and prints output''' - server = snapcast.control.Snapserver(add_server(), - snapcast.control.CONTROL_PORT) - client = get_snapcast_client(server) + '''Uses brighness level to mute or unmute device''' + # Snapcast library only supports asynchronous so need to wrap in async loop + loop = asyncio.new_event_loop() + client = get_snapcast_client(loop) GPIO.setmode(GPIO.BOARD) try: while True: - # TODO: track current state, don't send constant updates brightness = measure_brightness(SENSOR_PIN) if brightness < MUTE_BRIGHTNESS: - client.muted = True + # client.muted did not work so need client.set_muted + # loop used here and also passed into snap.py to get server + loop.run_until_complete(client.set_muted(True)) else: - client.muted = False + loop.run_until_complete(client.set_muted(False)) except KeyboardInterrupt: pass finally: - GPIO.cleanup() + GPIO.cleanup() + loop.close() def measure_brightness(pin_to_circuit):