Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use import in pyscript #696

Closed
Young-Grandad opened this issue Feb 6, 2025 · 4 comments
Closed

Can't use import in pyscript #696

Young-Grandad opened this issue Feb 6, 2025 · 4 comments

Comments

@Young-Grandad
Copy link

Young-Grandad commented Feb 6, 2025

I’m trying to create a simple function using pyscript that needs to import datetime.
I get an error after saving my script in the log:

Exception in </config/pyscript/season.py> line 2: import datetime ^ SyntaxError: invalid syntax (season.py, line 2)

This is my code:
@service(supports_response="only")
import datetime
from datetime import date
def season(return_response=True):
now = date.today()
m = now.month
x = m%12 // 3 + 1
season = x
return season

I have run the same code in an IDLE Shell in Windows (without the @service bit) and it runs without error (and works).

I have installed pyscript as detailed in the documentation, setting the two options for import and global to true in my configuration.yaml file.
My pyscript folder is in the config path (same as configuration.yaml).
Note: I see the deprecated warning as reported in issue #686 in the HASS log.

I have successfully created and run another python script (using pyscript with no imports) earlier, so I know at least it might work. However, it appears that the import statements cause syntax errors, so it looks like “allow_all_imports: true” is not taking effect.

As a double check I completely removed pyscript directories (rm -r) and re-downloaded and installed pyscript again. I made sure that I cancelled the UI configuration so that, I hope, the configuration only comes from the yaml configuration file.

Core 2025.2.0
Supervisor 2025.02.0
Operating System 14.2
Frontend 20250205.0

@craigbarratt
Copy link
Member

The @service decorator has to go immediately before the def season() function declaration, so put the imports first, then the decorator, then the function declaration.

@Young-Grandad
Copy link
Author

Thanks Craig, but I tried this earlier and I get different errors.

I also modified my code (function):

from datetime import datetime
@service(supports_response="only")
def season(return_response=True):
now = datetime.now()
m = now.month
x = m%12 // 3 + 1
season = x
return season

The import syntax error did not reoccur in the HA log, so I attempt to run my code in Developer Tools - action.
pyscript function season()

This resulted in a message Failed to process the returned action response data, expected a dictionary, but got <class 'NoneType'>
I think this occurs because of other errors in the HA log:

`Logger: homeassistant.helpers.script.websocket_api_script
Source: helpers/script.py:2039
First occurred: 13:40:11 (7 occurrences)
Last logged: 14:17:17

websocket_api script: Error executing script. Error for call_service at pos 1: Failed to process the returned action response data, expected a dictionary, but got <class 'NoneType'>
websocket_api script: Error executing script. Error for call_service at pos 1: Failed to process the returned action response data, expected a dictionary, but got <class 'int'>`

`Logger: homeassistant.components.websocket_api.http.connection
Source: components/websocket_api/connection.py:315
integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 13:40:11 (7 occurrences)
Last logged: 14:17:17

[547311759232] Error handling message: Failed to process the returned action response data, expected a dictionary, but got <class 'NoneType'> (home_assistant_error) Grandad from 192.168.1.119 (Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0)

My code works sucessfully using IDLE on a Windows 11 system.

Am I using the incorrect parts of DATETIME?

Regards, Steve

@craigbarratt
Copy link
Member

craigbarratt commented Feb 6, 2025

For a service response, the function needs to return a dict, not a scalar value. Example:

return {"season": x}

@Young-Grandad
Copy link
Author

Thanks again Craig and my apologies, as I know that a function needs to return a dict. I was so wrapped up in trying to solve the import syntax issues that I forgot about the dict. Actually, that piece of code was taken from the main script that I'm using and that does return a dict. I will read the docs again to see if there is anything else that that needs to go before @service.

Regards, Steve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants