Date arithmetic in python_script without "import datetime"

I have some reasonably complex functionality that warranted a python_script instead of just a normal series of actions for my automation. However, I need to do some date arithmetic to get the difference between two dates in text format that are obtained by the script. It seems to be a pain in the but when normally I could just from datetime import datetime.

Has anyone proposed whitelisting some well-known imports so that they could be used in these python scripts? Or is there another relatively easy way to accomplish some date arithmetic, or parsing a datetime to a timestamp without using the imports?

This is the bane of my existance, currently:

2022-10-15 14:35:33.198 ERROR (SyncWorker_1) [homeassistant.components.python_script.calendar_notifications.py] Error executing script: __import__ not found
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 224, in execute
    exec(compiled.code, restricted_globals)
  File "calendar_notifications.py", line 5, in <module>
ImportError: __import__ not found

Well, I think I’ve worked around my issue with date time, by passing the current time as a parameter from home assistant and using string parsing to figure everything out.

My next issue with the Python scripts is that I seem to be unable to call any services from my script using the hass.core interface. Why is this limitation in place? Isn’t the whole point to be able to get and change states and to call services using the script, but take advantage of python’s programmability?

</s> <s>2022-10-15 16:48:15.154 ERROR (SyncWorker_1) [homeassistant.components.python_script.calendar_notifications.py] Error executing script: Not allowed to access HomeAssistant.core</s> <s>

I should have read a bit more. I can use hass.services.call() :slight_smile: