From 1a322af57d63ffb29ed9027f5e88b51002b608f4 Mon Sep 17 00:00:00 2001 From: wintrov Date: Sat, 13 May 2023 22:04:18 +0300 Subject: [PATCH 1/5] Update the redalret.py & HA MQTT sensor Restructure data from Pikud Haorf for HA. - Add attributes - Edit HA sensor --- redalert.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/redalert.py b/redalert.py index b95dd62..ed62f7e 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"]) From 1523873ba3619195de1688960485bed5b7ad94d7 Mon Sep 17 00:00:00 2001 From: wintrov Date: Sat, 13 May 2023 22:05:41 +0300 Subject: [PATCH 2/5] Update redalert.py --- redalert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redalert.py b/redalert.py index ed62f7e..14ca38e 100644 --- a/redalert.py +++ b/redalert.py @@ -23,7 +23,7 @@ passw = os.getenv('MQTT_PASS') debug = os.getenv('DEBUG_MODE') region = os.getenv('REGION') -NOTIFIERS = os.getenv('NOTIFIERS', "") +NOTIFIERS = os.getenv('NOTIFIERS', '') MQTT_TOPIC = os.environ.get('MQTT_TOPIC', '/redalert') INCLUDE_TEST_ALERTS = os.getenv('INCLUDE_TEST_ALERTS') From 3b5b1d25c5b1345dda3d04289c22a95d914010ef Mon Sep 17 00:00:00 2001 From: wintrov Date: Sat, 13 May 2023 22:07:58 +0300 Subject: [PATCH 3/5] Update README.md --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 26b7bff..2a259e2 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,18 @@ services: value_template: "{{ value_json.data }}" qos: 1 ``` +#### In some case the above snipet code will not work for you, you can try that: +```yaml + - platform: mqtt + name: "Red Alert" + state_topic: "/redalert/data" + # 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 From 5769561ba08706b9f22a654826ed1c2cce02232a Mon Sep 17 00:00:00 2001 From: wintrov Date: Sat, 13 May 2023 22:10:21 +0300 Subject: [PATCH 4/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a259e2..ed91b73 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ services: ```yaml - platform: mqtt name: "Red Alert" - state_topic: "/redalert/data" + state_topic: "/redalert/" # unit_of_measurement: '%' icon: fas:broadcast-tower value_template: "{{ value }}" From ceebec06f2ce6279ddf229ce1f68ba92f1ae3f2a Mon Sep 17 00:00:00 2001 From: wintrov Date: Wed, 17 May 2023 17:44:19 +0300 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ed91b73..a33c35c 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ services: value_template: "{{ value_json.data }}" qos: 1 ``` -#### In some case the above snipet code will not work for you, you can try that: +#### In some cases, the above snippet code will not work for you; you can try that ```yaml - platform: mqtt name: "Red Alert"