update for single button
This commit is contained in:
		
							parent
							
								
									7ee01c0e4a
								
							
						
					
					
						commit
						7551f4b351
					
				
							
								
								
									
										14
									
								
								buttons.py
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								buttons.py
									
									
									
									
									
								
							| @ -1,6 +1,6 @@ | ||||
| import RPi.GPIO as GPIO | ||||
| import time | ||||
| from lib.mqtt import create_client, control_player, create_config | ||||
| from lib.mqtt import create_client, control_player, create_config, check_current_status | ||||
| 
 | ||||
| client = create_client() | ||||
| 
 | ||||
| @ -17,15 +17,23 @@ for pin in BUTTON_PINS: | ||||
| 
 | ||||
| # Callback functions to handle button presses | ||||
| def button_callback_1(channel): | ||||
|     print("Button 1 pressed!") | ||||
|     control_player(client, "PLAY") | ||||
|     if check_current_status() == "PLAY": | ||||
|         print("Button 1 pressed! Pausing playback.") | ||||
|         client = create_client() | ||||
|         control_player(client, "PAUSE") | ||||
|     else: | ||||
|         print("Button 1 pressed! Playing.") | ||||
|         client = create_client() | ||||
|         control_player(client, "PLAY") | ||||
| 
 | ||||
| def button_callback_2(channel): | ||||
|     print("Button 2 pressed!") | ||||
|     client = create_client() | ||||
|     control_player(client, "PAUSE") | ||||
| 
 | ||||
| def button_callback_3(channel): | ||||
|     print("Button 3 pressed!") | ||||
|     client = create_client() | ||||
| 
 | ||||
| # Add event detection for each button press | ||||
| GPIO.add_event_detect(BUTTON_PINS[0], GPIO.FALLING, callback=button_callback_1, bouncetime=200) | ||||
|  | ||||
							
								
								
									
										36
									
								
								lib/mqtt.py
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								lib/mqtt.py
									
									
									
									
									
								
							| @ -192,6 +192,42 @@ def check_current_disc(client: mqtt.Client) -> dict: | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| def check_current_status(client: mqtt.Client) -> str: | ||||
|     """Check the current player status by subscribing to the retained message on the state topic. | ||||
| 
 | ||||
|     Args: | ||||
|         client (mqtt.Client): MQTT Client | ||||
| 
 | ||||
|     Returns: | ||||
|         str: Current object at current_disc/state | ||||
|     """     | ||||
|     def on_message(client, userdata, message): | ||||
|         """Callback function to handle received MQTT messages.""" | ||||
|         userdata['message'] = message.payload.decode() | ||||
|      | ||||
|     # Create a dictionary to store the message | ||||
|     userdata = {'message': None} | ||||
| 
 | ||||
|     # Set the user data and the on_message callback | ||||
|     client.user_data_set(userdata) | ||||
|     client.on_message = on_message | ||||
| 
 | ||||
|     # Subscribe to the topic | ||||
|     client.subscribe("homeassistant/sensor/floppy_player/status/state") | ||||
| 
 | ||||
|     # Run the loop manually until we receive a message | ||||
|     timeout = 5  # Timeout after 5 seconds | ||||
|     while userdata['message'] is None and timeout > 0: | ||||
|         client.loop(timeout=0.1)  # Process network events for a short time | ||||
|         timeout -= 0.1 | ||||
| 
 | ||||
|     # Check if we received the message | ||||
|     if userdata['message'] is None: | ||||
|         raise TimeoutError("No retained message was found.") | ||||
| 
 | ||||
|     return userdata['message'] | ||||
| 
 | ||||
| 
 | ||||
| def update_disc(client: mqtt.Client, disc_message: dict) -> None: | ||||
|     """Update current disc. | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Karl Hudgell
						Karl Hudgell