buttons.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import RPi.GPIO as GPIO
  3. import time
  4. from lib.mqtt import create_client, control_player, create_config, check_current_status, check_current_disc, update_disc
  5. from lib.home_assistant import load_disc, pause_media, play_media_again, set_shuffle, next_track
  6. from dotenv import load_dotenv
  7. load_dotenv()
  8. use_mqtt = os.environ["use_mqtt"].lower() == "true"
  9. if use_mqtt:
  10. client = create_client()
  11. create_config(client)
  12. # Debug: Print environment variable
  13. print(f"Using MQTT: {use_mqtt}")
  14. # GPIO button pin setup
  15. BUTTON_PINS = [17, 18, 27, 22]
  16. GPIO.setmode(GPIO.BCM) # Use BCM numbering
  17. # Set up each button pin as input with internal pull-up resistors
  18. for pin in BUTTON_PINS:
  19. GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  20. # Button Handlers
  21. def handle_button_1():
  22. if use_mqtt:
  23. if check_current_status(client) == "PLAY":
  24. print("Pausing playback via MQTT.")
  25. control_player(client, "PAUSE")
  26. else:
  27. print("Playing media via MQTT.")
  28. control_player(client, "PLAY")
  29. else:
  30. print("Pausing/Playing via Home Assistant.")
  31. if check_current_status(client) == "PLAY":
  32. pause_media()
  33. else:
  34. play_media_again()
  35. def handle_button_2():
  36. print("Skipping to the next track.")
  37. if use_mqtt:
  38. control_player(client, "SKIP")
  39. control_player(client, "PLAY")
  40. else:
  41. next_track()
  42. def handle_button_3():
  43. start_time = time.time()
  44. if use_mqtt:
  45. disc_data = check_current_disc(client)
  46. # Wait until the button is released or held for more than 3 seconds
  47. while GPIO.input(BUTTON_PINS[2]) == GPIO.LOW:
  48. if time.time() - start_time >= 3:
  49. print("Button 3 held for 3 seconds! Resetting to RADIO2 via MQTT.")
  50. update_disc(client, ["ytmusic", disc_data['name'], disc_data['type'], "RESET"])
  51. control_player(client, "PLAY")
  52. update_disc(client, ["ytmusic", disc_data['name'], disc_data['type'], disc_data['id']])
  53. return # Exit after sending RADIO2
  54. print("Button 3 pressed briefly! Sending RADIO via MQTT.")
  55. control_player(client, "RADIO")
  56. else:
  57. print("Button 3 functionality not implemented for Home Assistant.")
  58. def handle_button_4():
  59. print("Toggling shuffle mode.")
  60. if use_mqtt:
  61. control_player(client, "SHUFFLE")
  62. control_player(client, "PLAY")
  63. else:
  64. set_shuffle()
  65. # GPIO Callback Handlers
  66. def button_callback(channel):
  67. if channel == BUTTON_PINS[0]:
  68. handle_button_1()
  69. elif channel == BUTTON_PINS[1]:
  70. handle_button_2()
  71. elif channel == BUTTON_PINS[2]:
  72. handle_button_3()
  73. elif channel == BUTTON_PINS[3]:
  74. handle_button_4()
  75. # Add event detection for each button press
  76. for pin in BUTTON_PINS:
  77. GPIO.add_event_detect(pin, GPIO.FALLING, callback=button_callback, bouncetime=200)
  78. try:
  79. # Keep the script running to detect button presses
  80. while True:
  81. time.sleep(1) # Sleep to reduce CPU usage
  82. except KeyboardInterrupt:
  83. pass
  84. finally:
  85. GPIO.cleanup() # Clean up GPIO on exit