I think @aimc’s suggestion is the cleanest way, and allows you to log your error appropriately. I did a little test … version one of my app to test the theory below:
import appdaemon.appapi as appapi
import requests
class HelloWorld(appapi.AppDaemon):
def initialize(self):
self.utils = self.get_app('utils')
self._initalize()
def _initalize(self):
for web_address in ['google', 'golllllllllogle']:
try:
r = requests.get('http://www.{}.com'.format(web_address))
except Exception as e:
self.log('Something went wrong, but I\'m not sure what...\n{}'.format(e))
self.log('[Response {0.status_code}] {0.text:.15}...'.format(r))
I don’t use requests
often, so I honestly had no idea what kind of Exception
to look for. Here’s my log output…
2017-03-04 19:18:29.025283 INFO Reloading Module: /home/homeassistant/.homeassistant/appdaemon/conf/apps/hello_world.py
2017-03-04 19:18:29.031341 INFO Loading Object hello_world using class HelloWorld from module hello_world
2017-03-04 19:18:29.198727 INFO hello_world: [Response 200] for google <!doctype html>...
2017-03-04 19:18:29.214649 INFO hello_world: Something went wrong, but I'm not sure what...
HTTPConnectionPool(host='www.golllllllllogle.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x6fef3390>: Failed to establish a new connection: [Errno -2] Name or service not known',))
Great! So it looks like with a bit of research, I could be catching ConnectionError
and handling appropriately. Let’s do that.
import appdaemon.appapi as appapi
import requests
class HelloWorld(appapi.AppDaemon):
def initialize(self):
self.utils = self.get_app('utils')
self._initalize()
def _initalize(self):
for web_address in ['google', 'golllllllllogle']:
try:
r = requests.get('http://www.{}.com'.format(web_address))
except requests.ConnectionError:
self.log('I\'m not sure "{}" exists... :('.format(web_address))
continue
except Exception as e:
self.log('Something went wrong, but I\'m not sure what...\n{}'.format(e))
continue
self.log('[Response {0.status_code}] for {1} {0.text:.15}...'.format(r, web_address))
and my output is …
2017-03-04 19:25:09.025929 INFO Reloading Module: /home/homeassistant/.homeassistant/appdaemon/conf/apps/hello_world.py
2017-03-04 19:25:09.032042 INFO Loading Object hello_world using class HelloWorld from module hello_world
2017-03-04 19:25:09.319005 INFO hello_world: [Response 200] for google <!doctype html>...
2017-03-04 19:25:09.333225 INFO hello_world: I'm not sure "golllllllllogle" exists... :(
So, now that we have a new entry point, that doesn’t keep AppDaemon
from loading the app, you could always essentially redirect your entry point and handle Exceptions appropriately. I used a simple for-loop to debug something I know would error out, but you can very much something similar. This way you’re not dealing with an os
calls, which can be messy. ![:confused: :confused:](https://community.home-assistant.io/images/emoji/twitter/confused.png?v=12)