Skip to content

Commit

Permalink
fix: correct use of async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobLichterfeld committed Mar 19, 2024
1 parent 97f8072 commit 92325d4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Bug Fixes

- fix: correct use of async functions

## [0.7.0] - 2023-03-18

### New Features
Expand Down
14 changes: 7 additions & 7 deletions src/teslamte_telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
and sends them to a Telegram chat."""
import os
import sys
import time
import logging
import asyncio
import paho.mqtt.client as mqtt
from telegram import Bot
from telegram.constants import ParseMode
Expand Down Expand Up @@ -139,7 +139,7 @@ def setup_telegram_bot():
return bot, chat_id


def check_state_and_send_messages(bot, chat_id):
async def check_state_and_send_messages(bot, chat_id):
""" Check the state and send messages if necessary """
logging.debug("Checking state and sending messages...")
global state # pylint: disable=global-variable-not-assigned
Expand All @@ -156,7 +156,7 @@ def check_state_and_send_messages(bot, chat_id):
+ " for your Tesla is available!"

logging.debug("Sending message.")
bot.send_message(
await bot.send_message(
chat_id,
text=message_text,
parse_mode=ParseMode.HTML,
Expand All @@ -169,7 +169,7 @@ def check_state_and_send_messages(bot, chat_id):


# Main function
def main():
async def main():
""" Main function"""
logging.info("Starting the Teslamate Telegram bot.")
client = setup_mqtt_client()
Expand All @@ -178,10 +178,10 @@ def main():
client.loop_start()
try:
while True:
check_state_and_send_messages(bot, chat_id)
await check_state_and_send_messages(bot, chat_id)

logging.debug("Sleeping for 1 second.")
time.sleep(1)
await asyncio.sleep(1)
except KeyboardInterrupt:
logging.info("Exiting after receiving SIGINT (Ctrl+C) signal.")

Expand All @@ -194,4 +194,4 @@ def main():

# Entry point
if __name__ == "__main__":
main()
asyncio.run(main())

0 comments on commit 92325d4

Please sign in to comment.