HomeAssistant notification
This commit is contained in:
36
main.py
36
main.py
@@ -3,6 +3,7 @@ import network
|
|||||||
import ubinascii
|
import ubinascii
|
||||||
import machine
|
import machine
|
||||||
import uos
|
import uos
|
||||||
|
import sys
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import urequests as requests
|
import urequests as requests
|
||||||
@@ -82,7 +83,7 @@ def disconnect_wifi():
|
|||||||
wlan.deinit()
|
wlan.deinit()
|
||||||
print('Disconnected')
|
print('Disconnected')
|
||||||
|
|
||||||
def send_notification(title, tags):
|
def send_notification(title, tags, send_ha_notification = False):
|
||||||
try:
|
try:
|
||||||
send_notification_to_server('https://ntfy.sh/' + ntfy_topic, title, tags)
|
send_notification_to_server('https://ntfy.sh/' + ntfy_topic, title, tags)
|
||||||
except:
|
except:
|
||||||
@@ -90,6 +91,16 @@ def send_notification(title, tags):
|
|||||||
blink_onboard_led(3)
|
blink_onboard_led(3)
|
||||||
send_notification_to_server('https://ntfy.adix.link/' + ntfy_topic, title, tags)
|
send_notification_to_server('https://ntfy.adix.link/' + ntfy_topic, title, tags)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if send_ha_notification:
|
||||||
|
if homeassistant_url is not None and homeassistant_token is not None:
|
||||||
|
send_homeassistant_notification(homeassistant_url)
|
||||||
|
else:
|
||||||
|
print('HomeAssistant notification is on, but config is missing', homeassistant_url, homeassistant_token)
|
||||||
|
except Exception as e:
|
||||||
|
print('Error sending HA notification')
|
||||||
|
sys.print_exception(e)
|
||||||
|
|
||||||
def send_notification_to_server(notify_url, title, tags):
|
def send_notification_to_server(notify_url, title, tags):
|
||||||
print('Sending notification to ' + notify_url + '...')
|
print('Sending notification to ' + notify_url + '...')
|
||||||
# Send notification
|
# Send notification
|
||||||
@@ -101,6 +112,17 @@ def send_notification_to_server(notify_url, title, tags):
|
|||||||
print(request.content)
|
print(request.content)
|
||||||
request.close()
|
request.close()
|
||||||
|
|
||||||
|
def send_homeassistant_notification(ha_url):
|
||||||
|
print('Sending notification to HA on ' + ha_url + '...')
|
||||||
|
# Send notification
|
||||||
|
request = requests.post(ha_url + '/api/services/input_button/press', data='{"entity_id": "input_button.doorbell"}', headers={
|
||||||
|
'Authorization': 'Bearer ' + (homeassistant_token if homeassistant_token is not None else ""),
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
})
|
||||||
|
print(request.content)
|
||||||
|
request.close()
|
||||||
|
|
||||||
|
|
||||||
def send_health_update():
|
def send_health_update():
|
||||||
try:
|
try:
|
||||||
health_url = healthr_url + '/health/' + healthr_service_name
|
health_url = healthr_url + '/health/' + healthr_service_name
|
||||||
@@ -144,6 +166,16 @@ ntfy_topic = settings['ntfy_topic']
|
|||||||
healthr_url = settings['healthr_url']
|
healthr_url = settings['healthr_url']
|
||||||
healthr_service_name = settings['healthr_service_name']
|
healthr_service_name = settings['healthr_service_name']
|
||||||
|
|
||||||
|
homeassistant_url = None
|
||||||
|
homeassistant_token = None
|
||||||
|
try:
|
||||||
|
homeassistant_url = settings['homeassistant_url']
|
||||||
|
homeassistant_token = settings['homeassistant_token']
|
||||||
|
except:
|
||||||
|
print('HomeAssistant config is missing', homeassistant_url, homeassistant_token)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print('Starting up...')
|
print('Starting up...')
|
||||||
|
|
||||||
wlan = network.WLAN(network.STA_IF)
|
wlan = network.WLAN(network.STA_IF)
|
||||||
@@ -227,7 +259,7 @@ while True:
|
|||||||
if connect_wifi():
|
if connect_wifi():
|
||||||
print('Sending notification...')
|
print('Sending notification...')
|
||||||
|
|
||||||
send_notification(title = 'Dany Csengo', tags = 'bell')
|
send_notification(title = 'Dany Csengo', tags = 'bell', send_ha_notification=True)
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
check_version_update()
|
check_version_update()
|
||||||
|
|||||||
Reference in New Issue
Block a user