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

Allow iteration in python_script #8134

Merged
merged 2 commits into from
Jun 21, 2017
Merged

Allow iteration in python_script #8134

merged 2 commits into from
Jun 21, 2017

Conversation

balloob
Copy link
Member

@balloob balloob commented Jun 21, 2017

Description:

This will allow us to iterate inside Python scripts and thus also use for-loops.

Example python_script

home = 0
for entity_id in hass.states.entity_ids('device_tracker'):
    state = hass.states.get(entity_id)
    if state.state == 'home':
        home = home + 1

hass.states.set('sensor.people_home', home, {
    'unit_of_measurement': 'people',
    'friendly_name': 'People home'
})

Checklist:

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@pvizeli pvizeli merged commit 6398e92 into dev Jun 21, 2017
@lwis lwis deleted the python-script-allow-iter branch June 21, 2017 16:06
balloob added a commit that referenced this pull request Jun 21, 2017
* Allow iteration in python_script

* Add tests
@balloob
Copy link
Member Author

balloob commented Jun 21, 2017

Cherry-picked for 0.47.1

@balloob balloob mentioned this pull request Jun 21, 2017
@azogue
Copy link
Member

azogue commented Jun 22, 2017

Hi @balloob,

I'm playing with the new component (in 0.47.1) and I have noticed certain lacks related to this entry, I don't know if they are intended:

  • No hass object for comprehensions (NameError: name 'hass' is not defined)
# Not working:
local_data = {k: hass.states.get(k) for k in local_data.keys()}

# Working:
for k in local_data.keys():
    local_data[k] = hass.states.get(k)
  • No key-access to dicts (NameError: name '_getitem_' is not defined):
    close_house = local_data['input_boolean.close_house']
  • No getattr (NameError: name 'getattr' is not defined):
    close_house = getattr(local_data, 'input_boolean.close_house')

This is working:
close_house = local_data.get('input_boolean.close_house')

  • No vars starting with '_': Error loading script testing.py: Line 2: "_variable_name" is an invalid variable name because it starts with "_"

  • Can't use max: NameError: name 'max' is not defined

  • Can't import anything: ImportError: __import__ not found

Do you want me to publish these as a new issue?

@balloob
Copy link
Member Author

balloob commented Jun 22, 2017

Please don't use PRs to report issues. Have a look here: https://community.home-assistant.io/t/python-script-feedback/20186

@home-assistant home-assistant locked and limited conversation to collaborators Jun 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants