Is it possible to test for a service availability?
I have a script that calls notify.push part way through the script. Once in a while the push service isn’t available and the rest of the script doesn’t finish running. I’d like to be able to test for the notify.push service so I can conditionally call the service so it will not thrown and error and allow the rest of the script to run.
In most of my scripts run the notifications at the end so it’s not usually an issue but in this particular script I have to call it near the beginning and it causes an issue if the service isn’t available.
Restarting too many times in a row for one. I think Chrome cuts you off after too many connection attempts similar to a lot of the other API’s. I’m using packages and it is a real pain in the butt rebooting all the time to test out new code! Unfortunately the benefits of using packages outweighs that one negative.
The push service has also failed to initialize for me a couple of times after a normal weekly reboot and/or power failure.
Not a super critical thing but the automation is for my alarm clock so I’d like to make it robust as possible.
I’m going to go out on a limb here but I think maybe only Appdaemon can provide something like “try-except” where you try to access the service and if it fails (exception) you can do something graceful to handle it.
To add on to that, instead of Appdaemon you could try just using a simple python script and wrap the service call in a try/except block. Then call that script from the automation.
Ugh. Finally got a handle on YAML, currently just cutting my teeth on Jinja2, and now you’re telling me I have to dive into python too!! Guess it’s time - been putting it off…lol.
The exceptions in jinja look interesting though. I might poke around with that. Doesn’t seem to be any way native in HA to test, I’ve been googling for a couple days now.
I’ve been looking for ways to test for the existence of a service in automations and found this thread. From what I’ve found there’s no direct way to test for the existence of a service. What is possible to do is listen for service_registered and service_removed events and set a boolean variable accordingly. Then that boolean can be tested in automations and scripts.
(You could use state: absent to test if the service does not exist.)
This would allow not having to listen to events and create a custom variable to keep track of the existence of a service, but, alas, I’ve not found anything like this.
However, with the event tracking method, or even if they added a method for testing directly the existence of a service, I’m pretty sure you’d be facing a race condition. I admit I’ve not researched issues of race conditions in automations and script but I’d expect that it is theoretically possible for logic coded in yaml to:
Check the existence of a service and find that it is present.
[Then, something somewhere else causes the service to go out of existence.]
Try to use the service and bomb because the service no longer exists.
AFAICT Home Assistant does no lock states between condition checks and anything that follows. So adding a check to yaml-encoded scripts would reduce how often your script aborts early, but it would not eliminate all such cases.
If what you want is for your current code to continue irrespective of whether notify.push is successful or not, you could:
Isolate notify.push in a separate script, and invoke that script. Scripts are services, so you can invoke them directly as services or you can invoke them using script.turn_on. (The two methods are described here.) However, I’d expect a failure in a directly invoked script to propagate back to the invoking code, so direct invocation probably does not help. On the other hand, I don’t know whether script.turn_on considers itself successful if the script started at all, or whether it considers itself successful if the script was successful. In the latter case it would propagate a script failure back to the invoking code, and it would not be useful. You’d have to try it to know.
You could move notify.push to an isolated automation which is triggered by a custom event, then in the script where you had notify.push, you’d instead trigger the custom event. Then it won’t matter if the new automation completes or not. However, this may have sequencing consequences that are not acceptable to your use-case scenario. I’m pretty sure that there are no guarantees as to when the automation gets the event so the notification may happen later than you’d wish.
If these don’t work, then I think you’d have to use Python. A python_script would probably be enough though and is much simpler to put together than adding AppDaemon. (I see tboyce1 already mentioned python_script.)
You may be interested in pnbruckner’s observations about script behavior. Apparently, adding a small delay to the script achieves the goal of preventing a failed script from blocking all further execution.
So far there is no way to check if some service is available or not.
Example:
The “google_sheets.append_sheet” service is provided by Google Sheets.
User may disable this integration.
If disabled - calling the service causes an error.
Let’s not force a user to exclude/comment all code with using the “google_sheets.append_sheet” service just because he disabled the integration.
Use Watchman, which does detect missing services. You could check if the service in question is in the watchman sensor attributes and use an if/else to avoid the service call if the integration is disabled.
I agree they are not “great” options, but they are options.
It would be nice if both unavailable services and entities were detected intrinsically and iterated by HA. The popularity of my unavailable entities template is a testament to this.