feat: send open door alerts to controller
This commit is contained in:
14
server.py
14
server.py
@@ -38,6 +38,16 @@ OPEN_ALERT_THRESHOLDS_MINUTES = [5, 15, 30, 60, 120]
|
|||||||
OPEN_ALERTS_SENT_FOR_CURRENT_OPENING = []
|
OPEN_ALERTS_SENT_FOR_CURRENT_OPENING = []
|
||||||
|
|
||||||
|
|
||||||
|
def controller_message(message):
|
||||||
|
payload = {mysecrets.CONTROLLER_KEY: message}
|
||||||
|
logging.info('Controller message: %s', message)
|
||||||
|
r = requests.post(mysecrets.CONTROLLER_URL, data=payload, timeout=10)
|
||||||
|
if r.status_code == 200:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logging.exception('Unable to communicate with controller! Message: ' + message)
|
||||||
|
return False
|
||||||
|
|
||||||
def get_derived_state():
|
def get_derived_state():
|
||||||
"""Derives the state from the prediction history."""
|
"""Derives the state from the prediction history."""
|
||||||
state = "unknown"
|
state = "unknown"
|
||||||
@@ -188,7 +198,9 @@ async def monitor_state_transitions(app):
|
|||||||
|
|
||||||
for threshold in OPEN_ALERT_THRESHOLDS_MINUTES:
|
for threshold in OPEN_ALERT_THRESHOLDS_MINUTES:
|
||||||
if open_duration_minutes >= threshold and threshold not in OPEN_ALERTS_SENT_FOR_CURRENT_OPENING:
|
if open_duration_minutes >= threshold and threshold not in OPEN_ALERTS_SENT_FOR_CURRENT_OPENING:
|
||||||
logging.info(f"ALERT: Garage door has been open for {threshold} minutes.")
|
msg = f"ALERT: Garage door has been open for {threshold} minutes."
|
||||||
|
controller_message(msg)
|
||||||
|
logging.info(msg)
|
||||||
OPEN_ALERTS_SENT_FOR_CURRENT_OPENING.append(threshold)
|
OPEN_ALERTS_SENT_FOR_CURRENT_OPENING.append(threshold)
|
||||||
elif current_state == 'closed':
|
elif current_state == 'closed':
|
||||||
DOOR_OPEN_START_TIME = None
|
DOOR_OPEN_START_TIME = None
|
||||||
|
Reference in New Issue
Block a user