home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.65k stars 29.95k forks source link

Python scripts don't allow creation of classes #77712

Open Nightenom opened 2 years ago

Nightenom commented 2 years ago

The problem

Python script integration __init__ is missing definitions of __name__ and __metaclass__ needed for Python3 classes to work - see https://restrictedpython.readthedocs.io/en/latest/usage/basic_usage.html#necessary-setup and https://github.com/zopefoundation/RestrictedPython/blob/master/src/RestrictedPython/tests/transformer/test_classdef.py

Suggested solution based on tests above: __metaclass__=type __name__='__hass__' - can be virtually everything, however __main__ and other common things (used in classic Python3) should be avoided

Other side notes:

My use case if basically replacing automations with python scripts because my logic is big and complex to be defined in UI in sane way

What version of Home Assistant Core has the issue?

2022.7.6

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Python Scripts

Link to integration documentation on our website

https://www.home-assistant.io/integrations/python_script

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

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 "dhw_controller.py", line 1, in <module>
NameError: name '__metaclass__' is not defined

Additional information

No response

probot-home-assistant[bot] commented 2 years ago

python_script documentation python_script source (message by IssueLinks)

Nightenom commented 1 year ago

Still an issue

issue-triage-workflows[bot] commented 1 year ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Nightenom commented 1 year ago

Still an issue

issue-triage-workflows[bot] commented 1 year ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Nightenom commented 1 year ago

👍

HengyueLi commented 1 year ago

+1 Still an issue

HengyueLi commented 1 year ago

@Nightenom Did you solve the problem? I still meet the same problem. For the demo example, I get this error by only appending the following code:

class test:
    pass
Nightenom commented 1 year ago

@Nightenom Did you solve the problem? I still meet the same problem. For the demo example, I get this error by only appending the following code:

Impossible to solve without this issue being solved. I've used struct-based class approach (similar to what you do in simple C)

issue-triage-workflows[bot] commented 1 year ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Nightenom commented 1 year ago

Still an issue

criena commented 11 months ago

I came across the same limitation. Might this be a desired behaviour/limitation? Maybe for security reasons, just like with the lack of imports?

Nightenom commented 11 months ago

Due to complexity of possible solution I doubt it's desired, rather than being a missed config. There are no comments anywhere on why it should not bet available

pbtsrc commented 8 months ago

Not sure, but https://github.com/custom-components/pyscript doesn't seem to have this limitation.

jmot205 commented 7 months ago

Just ran into this issue after refactoring a script to use tidier classes; very frustrating.

issue-triage-workflows[bot] commented 4 months ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Nightenom commented 4 months ago

Still an issue

issue-triage-workflows[bot] commented 1 month ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Nightenom commented 1 month ago

Still an issue