Skip to content

Commit

Permalink
Merge pull request #108 from toomyem/lwt
Browse files Browse the repository at this point in the history
Added LWT message handling
  • Loading branch information
koenvervloesem authored Mar 14, 2023
2 parents 37e8ab2 + dc65de5 commit 5013838
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
12 changes: 11 additions & 1 deletion TheengsGateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
"ble_scan_time": 5,
"ble_time_between_scans": 5,
"publish_topic": "home/TheengsGateway/BTtoMQTT",
"publish_all": 1,
"lwt_topic": "home/TheengsGateway/LWT",
"subscribe_topic": "home/+/BTtoMQTT/undecoded",
"presence_topic": "home/TheengsGateway/presence",
"presence": 0,
"publish_all": 1,
"log_level": "WARNING",
"discovery": 1,
"hass_discovery": 1,
Expand Down Expand Up @@ -81,6 +82,13 @@ def main():
type=str,
help="MQTT subscribe topic",
)
parser.add_argument(
"-Lt",
"--lwt_topic",
dest="lwt_topic",
type=str,
help="MQTT LWT topic",
)
parser.add_argument(
"-prt",
"--presence_topic",
Expand Down Expand Up @@ -217,6 +225,8 @@ def main():
config["publish_topic"] = args.pub_topic
if args.sub_topic:
config["subscribe_topic"] = args.sub_topic
if args.lwt_topic:
config["lwt_topic"] = args.lwt_topic
if args.presence_topic:
config["presence_topic"] = args.presence_topic
if args.presence is not None:
Expand Down
9 changes: 8 additions & 1 deletion TheengsGateway/ble_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def connect_mqtt(self):
def on_connect(client, userdata, flags, return_code):
if return_code == 0:
logger.info("Connected to MQTT Broker!")
client.publish(self.lwt_topic, "online", 0, True)
self.subscribe(self.sub_topic)
else:
logger.error(
Expand All @@ -87,13 +88,18 @@ def on_disconnect(client, userdata, return_code=0):

self.client = mqtt_client.Client()
self.client.username_pw_set(self.username, self.password)
self.client.will_set(self.lwt_topic, "offline", 0, True)
self.client.on_connect = on_connect
self.client.on_disconnect = on_disconnect
try:
self.client.connect(self.broker, self.port)
except Exception as exception:
logger.error(exception)

def disconnect_mqtt(self):
self.client.publish(self.lwt_topic, "offline", 0, True)
self.client.disconnect()

def subscribe(self, sub_topic):
"""Subscribe to MQTT topic <sub_topic>."""

Expand Down Expand Up @@ -420,6 +426,7 @@ def run(arg):
gw.time_between_scans = config.get("ble_time_between_scans", 0)
gw.sub_topic = config.get("subscribe_topic", "gateway_sub")
gw.pub_topic = config.get("publish_topic", "gateway_pub")
gw.lwt_topic = config["lwt_topic"]
gw.presence_topic = config["presence_topic"]
gw.presence = config["presence"]
gw.publish_all = config["publish_all"]
Expand All @@ -439,7 +446,7 @@ def run(arg):
try:
gw.client.loop_forever()
except (KeyboardInterrupt, SystemExit):
gw.client.disconnect()
gw.disconnect_mqtt()
gw.stopped = True
while gw.running:
pass
Expand Down
4 changes: 3 additions & 1 deletion docs/use/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Note that in the latter case, we can't guarantee that the manufacturer name is c

```shell
C:\Users\1technophile>python -m TheengsGateway -h
usage: -m [-h] [-H HOST] [-P PORT] [-u USER] [-p PWD] [-pt PUB_TOPIC]
usage: -m [-h] [-H HOST] [-P PORT] [-u USER] [-p PWD] [-pt PUB_TOPIC] [-Lt LWT_TOPIC]
[-st SUB_TOPIC] [-pa PUBLISH_ALL] [-sd SCAN_DUR] [-tb TIME_BETWEEN]
[-ll {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-Dt DISCOVERY_TOPIC] [-D DISCOVERY] [-Dh HASS_DISCOVERY]
[-Dn DISCOVERY_DEVICE_NAME] [-Df DISCOVERY_FILTER [DISCOVERY_FILTER ...]]
Expand All @@ -61,6 +61,8 @@ optional arguments:
MQTT publish topic
-st SUB_TOPIC, --sub_topic SUB_TOPIC
MQTT subscribe topic
-Lt LWT_TOPIC, --lwt_topic LWT_TOPIC
MQTT LWT topic
-pa PUBLISH_ALL, --publish_all PUBLISH_ALL
Publish all (1) or only decoded (0) advertisements (default:
1)
Expand Down

0 comments on commit 5013838

Please sign in to comment.