refactor: make controller_message async using aiohttp
Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
13
server.py
13
server.py
@@ -38,13 +38,18 @@ 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):
|
async def controller_message(app, message):
|
||||||
payload = {mysecrets.CONTROLLER_KEY: message}
|
payload = {mysecrets.CONTROLLER_KEY: message}
|
||||||
logging.info('Controller message: %s', message)
|
logging.info('Controller message: %s', message)
|
||||||
r = requests.post(mysecrets.CONTROLLER_URL, data=payload, timeout=10)
|
session = app['client_session']
|
||||||
if r.status_code == 200:
|
try:
|
||||||
|
async with session.post(mysecrets.CONTROLLER_URL, data=payload, timeout=10) as response:
|
||||||
|
if response.status == 200:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
logging.error(f'Unable to communicate with controller! Message: {message}, Status: {response.status}')
|
||||||
|
return False
|
||||||
|
except Exception:
|
||||||
logging.exception('Unable to communicate with controller! Message: ' + message)
|
logging.exception('Unable to communicate with controller! Message: ' + message)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -199,7 +204,7 @@ 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:
|
||||||
msg = f"ALERT: Garage door has been open for {threshold} minutes."
|
msg = f"ALERT: Garage door has been open for {threshold} minutes."
|
||||||
controller_message(msg)
|
await controller_message(app, msg)
|
||||||
logging.info(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':
|
||||||
|
Reference in New Issue
Block a user