-
Notifications
You must be signed in to change notification settings - Fork 0
/
mute_button.py
65 lines (60 loc) · 1.83 KB
/
mute_button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import RPi.GPIO as GPIO
import time
import subprocess
import logging
MQTT_HOST = "xxx.xxx.xx.xx" # Replace with your Home Assistant IP
MQTT_USER = "user" # Replace with your MQTT user
MQTT_PASSWORD = "password" # Replace with your MQTT password
MQTT_TOPIC = "mqtt/status" # Replace with your own MQTT topic name
BUTTON_PIN = 17 # GPIO pin for the button
def send_mqtt(message):
"""
Sends an MQTT message to the Home Assistant.
:param message: Message payload (e.g., "on", "off").
"""
try:
logging.info(f"Sending MQTT message: {message}")
subprocess.run(
[
"mosquitto_pub",
"-t", MQTT_TOPIC,
"-h", MQTT_HOST,
"-m", message,
"-u", MQTT_USER,
"-P", MQTT_PASSWORD,
],
text=True,
check=True, # Raise exception if the command fails
)
logging.info("MQTT message sent successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to send MQTT message: {e}")
except Exception as e:
logging.exception("Unexpected exception in send_mqtt")
def setup_gpio():
"""
Configures the GPIO pin for the button.
"""
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN)
def main():
"""
Main loop to monitor button state and send MQTT messages.
"""
setup_gpio()
logging.info("Monitoring button state...")
try:
while True:
state = GPIO.input(BUTTON_PIN)
if state:
send_mqtt("off")
else:
send_mqtt("on")
time.sleep(1)
except KeyboardInterrupt:
logging.info("Exiting on user interrupt.")
finally:
GPIO.cleanup()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()