Fetching data from external URL from within python script

I have an automation which is triggered by an event, calls a python script and the script should read data from an external URL. Normally I do that with the requests module, but inside python scripts it is not possible to use imports. Is there any other way to access the URL? Maybe with a hass service? The documentation for it (homeassistant.core — Home Assistant 2022.2.9 documentation) is empty.
RESTful sensor will not work because it does not know when the data is ready.

Not sure if I can help so maybe a bit more info? There is also scraping but I cannot guess what you mean with ‘access the URL’


Same here. I’m implementing a stateless button entity that will call a custom python script. This script will do an HTTP POST (using python requests) to a Particle IoT device to call a function (lock or unlock truck door). The URL will return data in JSON format of the return status. The URL fetch can take up to 30 seconds. If the command was successful, the python script will then send a notification to the app running on my phone that it was successful. Due to the IoT device being on a cell network with possible spotty coverage, successful reception of the command is not guaranteed.

I think the answer is to use AppDaemon.

BTW - I can’t implement this with a switch because I have no way to query the door lock status, given that the state of the door can change through means outside of the script control. I’ve only been able to reverse engineer the transmission of lock and unlock codes sent by the internal lock & unlock buttons for the truck internal CAN bus. There seem to be different codes sent from the remote key fob.