-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrpi_gpio_switch.py
More file actions
executable file
·59 lines (44 loc) · 1.67 KB
/
Copy pathrpi_gpio_switch.py
File metadata and controls
executable file
·59 lines (44 loc) · 1.67 KB
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
#!/usr/bin/env python3
"""Example of how to expose a GPIO of a Raspberry Pi to Home Assistant.
This script should have direct access to the GPIO of the Raspberry (we assume
that the script is running in the RPi itself). So that justifies the use of an
optimistic switch.
This example uses the library RPi.GPIO. But the main idea can be used with any
similar library, and even different boards with different GPIO capabilities.
In addition to being controlled through Home Assistant (and receiving the update
through the callback method) changes on the switch can be triggered through
Python itself.
"""
from ham import MqttManager
from ham.switch import OptimisticSwitch
from time import sleep
import os
import RPi.GPIO as GPIO
MQTT_USERNAME = os.environ["MQTT_USERNAME"]
MQTT_PASSWORD = os.environ["MQTT_PASSWORD"]
MQTT_HOST = os.environ["MQTT_HOST"]
GPIO_PIN = os.environ["GPIO_PIN"]
class GPIOSwitch(OptimisticSwitch):
name = "GPIO Switch"
short_id = "gpioswitch"
def __init__(self, pin):
super().__init__()
self.pin = pin
GPIO.setup(self.pin, GPIO.OUT)
def callback(self, state: bool):
super().callback(state)
if state:
GPIO.output(self.pin, GPIO.HIGH)
else:
GPIO.output(self.pin, GPIO.LOW)
if __name__ == "__main__":
GPIO.setmode(GPIO.BOARD)
main_switch = GPIOSwitch(GPIO_PIN)
manager = MqttManager(MQTT_HOST, username=MQTT_USERNAME, password=MQTT_PASSWORD)
manager.add_thing(main_switch)
manager.start()
print("Entering an infinite loop, Ctrl+C multiple times to exit.")
while True:
sleep(30)
# Auto switches on every 30 seconds
main_switch.state = True