Skip to content

Commit

Permalink
Merge pull request #12 from djotaku/devel
Browse files Browse the repository at this point in the history
Adding Config for MQTT
  • Loading branch information
djotaku authored Oct 4, 2020
2 parents 27728d5 + f82578f commit 4e7c595
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,24 @@ On that model, if you connect it with the red wire, it goes high (1) if the gara
As of v0.3.0 it now works with MQTT and is useful for using with Home Assistant:

![mqtt animated gif](https://raw.githubusercontent.com/djotaku/raspi_garage_alert/master/screenshots/mqtt.gif)

You should have the following config files in $HOME/.config/raspigaragealert/: mqtt.conf and matrix.conf

*mqtt.conf*
```json
{
"channel":"something/somethingelse",
"server":"mqtt server url or ip address",
"client_id":"client_if for raspberry pi"
}
```

*matrix.conf*
```json
{
"server":"url or IP address of server",
"room":"room ID will look like !randomletters:server.com"
"username":"name of user for posting",
"password":"password for that user"
}
```
2 changes: 1 addition & 1 deletion raspigaragealert/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def main():
xdg = xdgenvpy.XDGPedanticPackage('raspigaragealert')
my_door = door.door(7)
mqtt_service = mqtt.Publisher("garage/door1", "tanukimario.mushroomkingdom") # testing values - will need to make user configurable
mqtt_service = mqtt.Publisher(xdg) # testing values - will need to make user configurable
my_matrix_bot = matrix.MatrixBot(xdg)
loop = True
success = False
Expand Down
2 changes: 1 addition & 1 deletion raspigaragealert/services/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, xdg):
try:
with open(f'{self.xdg.XDG_CONFIG_HOME}/matrix.conf') as file:
self.config = json.load(file)
print("Config loaded.")
print("Matrix Config loaded.")
file.close()
except FileNotFoundError:
print(f"Settings not found at {self.xdg.XDG_CONFIG_HOME}")
Expand Down
12 changes: 8 additions & 4 deletions raspigaragealert/services/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Publish state via MQTT."""

import json
import paho.mqtt.publish as publish


Expand All @@ -9,10 +10,13 @@ class Publisher():
:param channel: The channel that the messages will be published on.
:param server: The MQTT server messages will be sent to.
"""
def __init__(self, channel, server):
self.channel = channel
self.server = server
self.client_id = "garage_pi"
def __init__(self, xdg):
with open(f'{xdg.XDG_CONFIG_HOME}/mqtt.conf') as file:
config = json.load(file)
print("mqtt config laoded")
self.channel = config.get("channel")
self.server = config.get("server")
self.client_id = config.get("client_id")

def publish(self, message) -> bool:
"""Publish message to MQTT server.
Expand Down

0 comments on commit 4e7c595

Please sign in to comment.