import appdaemon.plugins.hass.hassapi as hass
class TelegramBot(hass.Hass):
def initialize(self):
# Start listening for Telegram updates
try:
self.listen_event(self.receive_telegram_text, "telegram_text")
self.log("*** Successfully subscribed to telegram_text events")
except:
self.log("*** Failed to subscribe to telegram_text events")
def receive_telegram_text(self, event_id, payload_event, *args):
# Do something with the text
self.log("*** Received message from Telegram")
The problem is that I never see the telegram_text event being passed to my python code. The â*** Successfully subscribed to telegram_text eventsâ message is received and despite being able to see the telegram_text event being fired by listening to it on the HA Developer Tools / Events page, it never seems to get to Appdaemon (i.e I donât see the â*** Received message from Telegramâ message in the Appdaemon log),
Any help solving this would be much appreciated.
Thanks
Are there any errors in the AppDaemon error log?
Did you try to subscribe to another event to see if it works?
Why do you have the try-except for the event listener? I canât think of a scenario where you would enter the except clause. I doesnât make sense to use try-except here in my opinion.
Tomas / Burningstone,
Changed my code to listen for all events. Once again I received the message to say the subscription was successful, but no events are received by the callback function.
Try and except are there because I thought (maybe mistakenly - I am a newbie to python) that if the self.listen_event failed it would execute the âexceptâ branch of the code, so that I would know it had failed.
The Appdaemon log shows no errors.
Thanks for your assistance.
A little more info. On restart of appdaemon, my code which subscribes to all events results in the following in the appdaemon log:
2019-08-14 17:40:55.894599 INFO AppDaemon: Initializing app bot using class TelegramBot from module telegram
2019-08-14 17:40:55.897176 INFO bot: *** Successfully subscribed to all events
2019-08-14 17:40:55.898231 INFO AppDaemon: App initialization complete
2019-08-14 17:40:55.900365 INFO bot: appd_started
2019-08-14 17:40:55.900643 INFO bot: {}
I was thinking maybe my subscribe to the telegram_text event was being issued too early and I need to wait a while for appdaemon to start. However it does seem strange that I receive the appd_started event, but subsequently no other events are reported by the callback function.
EDIT: Thinking about it further, I cannot just wait for appd_started event to be fired as that would not work if I updated the bot app code, but did not restart appdaemon, or the bot code was reloaded by appdaemon for a reason other than a restart of appdaemon.
Try to fire an event manually on the events page in HA, like Event Type test and {"test": "test event"} as the event data. It should be received by the appdaemon app.
Tomas
Just tried firing the test event, as suggested. Received the message that the event was fired successfully, but nothing reported by my appdaemon script, which is subscribed to all events.
I only have one other app running, but that does not make use of any events or HA entity status, it simply drops a MariaDB table for the Traccar plugin which can cause some issues if left lying around.
Appdaemon seems to connect OK as I can see the connection message in the log:
2019-08-14 18:09:10.974025 INFO AppDaemon: HASS: Connected to Home Assistant 0.97.2
I donât think it should make any difference, but just in case, this is a HASS.IO installation and Appdaemon was installed via the Hass.io Add-on-store