diff --git a/README.md b/README.md index 26b7bff..a33c35c 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,18 @@ services: value_template: "{{ value_json.data }}" qos: 1 ``` +#### In some cases, the above snippet code will not work for you; you can try that +```yaml + - platform: mqtt + name: "Red Alert" + state_topic: "/redalert/" + # unit_of_measurement: '%' + icon: fas:broadcast-tower + value_template: "{{ value }}" + json_attributes_topic: "/redalert/data" + json_attributes_template: "{{ value_json | tojson }}" + qos: 1 +``` #### Alaram state (Value will be on/off) ```yaml diff --git a/redalert.py b/redalert.py index b95dd62..14ca38e 100644 --- a/redalert.py +++ b/redalert.py @@ -1,29 +1,31 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import threading -import paho.mqtt.client as mqtt -import urllib3 import os -from loguru import logger import time -import codecs -import apprise import json - +import urllib3 +import threading +import apprise +import paho.mqtt.client as mqtt +from loguru import logger +############### +# from dotenv import load_dotenv +# load_dotenv() +############### os.environ['PYTHONIOENCODING'] = 'utf-8' os.environ['LANG'] = 'C.UTF-8' #mqtt connection Params server = os.getenv('MQTT_HOST') #Default port is 1883 -port = int(os.getenv('MQTT_PORT')) +port = int(os.getenv('MQTT_PORT', 1883)) user = os.getenv('MQTT_USER') passw = os.getenv('MQTT_PASS') debug = os.getenv('DEBUG_MODE') region = os.getenv('REGION') -NOTIFIERS = os.getenv("NOTIFIERS") -MQTT_TOPIC = os.environ.get("MQTT_TOPIC", "/redalert") -INCLUDE_TEST_ALERTS = os.getenv("INCLUDE_TEST_ALERTS") +NOTIFIERS = os.getenv('NOTIFIERS', '') +MQTT_TOPIC = os.environ.get('MQTT_TOPIC', '/redalert') +INCLUDE_TEST_ALERTS = os.getenv('INCLUDE_TEST_ALERTS') # reader = codecs.getreader('utf-8') @@ -33,7 +35,7 @@ #Setting Request Headers http = urllib3.PoolManager() _headers = {'Referer':'https://www.oref.org.il/','User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",'X-Requested-With':'XMLHttpRequest'} -url= 'https://www.oref.org.il/WarningMessages/alert/alerts.json' +url = 'https://www.oref.org.il/WarningMessages/alert/alerts.json' if debug == 'True': url = 'http://localhost/alerts.json' @@ -53,7 +55,7 @@ def on_connect(client, userdata, flags, rc): logger.error("Connection refused – bad username or password") if rc==5: logger.error("Connection refused – not authorised") - + def on_disconnect(client, userdata, rc): logger.info("disconnecting reason " +str(rc)) client.connected_flag=False @@ -84,7 +86,7 @@ def on_log(client, userdata, level, buf): logger.info("In wait loop") time.sleep(1) logger.info("in Main Loop") -client.loop_stop() #Stop loop +client.loop_stop() #Stop loop if len(NOTIFIERS)!=0: @@ -95,7 +97,8 @@ def on_log(client, userdata, level, buf): apobj.add(job) def alarm_on(data): - client.publish(MQTT_TOPIC + "/data",str(data["data"]),qos=0,retain=False) + to_publish = str({'data': data["data"]}).replace("'", '"') + client.publish(MQTT_TOPIC + "/data", to_publish, qos=0,retain=False) client.publish(MQTT_TOPIC,'on',qos=0,retain=False) if len(NOTIFIERS)!=0: logger.info("Alerting using Notifires") @@ -104,10 +107,10 @@ def alarm_on(data): title=str(data["title"]), ) - def alarm_off(): - client.publish(MQTT_TOPIC + "/alarm",'off',qos=0,retain=False) - client.publish(MQTT_TOPIC,"No active alerts",qos=0,retain=False) + client.publish(MQTT_TOPIC + "/alarm", "off", qos=0,retain=False) + client.publish(MQTT_TOPIC, "No active alerts", qos=0,retain=False) + client.publish(MQTT_TOPIC + "/data", '{"data": []}', qos=0,retain=False) def is_test_alert(alert): # if includes, all alerts are treated as not test @@ -124,6 +127,7 @@ def monitor(): try: if alert_data != '': alert = json.loads(alert_data) + alert["data"] = [item for sublist in alert["data"] for item in sublist.split(', ')] if region in alert["data"] or region=="*": if alert["id"] not in alerts and not is_test_alert(alert): alerts.append(alert["id"])