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

Add unique ID to scripts #70345

Merged
merged 1 commit into from
Apr 21, 2022
Merged

Add unique ID to scripts #70345

merged 1 commit into from
Apr 21, 2022

Conversation

frenck
Copy link
Member

@frenck frenck commented Apr 20, 2022

Proposed change

Add a unique ID to the scripts entities, using the same method as we do with all input_* integrations.

The key of a script already has to be unique, thus can be used for scripts as well.

fixes: https://twitter.com/JHerbY2K/status/1516860076316835841

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (script) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@@ -319,6 +319,10 @@ def __init__(self, hass, object_id, cfg, raw_config, blueprint_inputs):
self.description = cfg[CONF_DESCRIPTION]
self.fields = cfg[CONF_FIELDS]

# The object ID of scripts need / are unique already
# they cannot be changed from the UI after creating
self._attr_unique_id = object_id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm actually I don't think this will work.

Copy link
Member

@balloob balloob Apr 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this YAML:

script hello:
  example:
    ...

script:
  example:
    ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does work, as its already bit sketchy actually 😮

async def async_validate_config(hass, config):
    """Validate config."""
    scripts = {}
    for _, p_config in config_per_platform(config, DOMAIN):
        for object_id, cfg in p_config.items():
            cfg = await _try_async_validate_config_item(hass, object_id, cfg, config)
            if cfg is not None:
                scripts[object_id] = cfg

Meaning if a script object ID exists twice, it gets overwritten.
Thus never the same one exists twice.

I think it would be nice to throw a warning in the config validation (instead of just silently overwriting). Will do that in a separate PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to handle this globally but the problem of something in YAML overriding something else exists not only for scripts. I notice this few times that users want to device two entities/devices so they add a section twice and complain why only the last one is working. If we have a way to globally catch this and log a warning it would be better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are referring to duplicate keys in YAML: which is kinda similar, but not the issue discussed in this case.

Having duplicate keys in YAML will actually be logged (we even have tests for that):

image

@frenck frenck marked this pull request as draft April 20, 2022 22:57
@frenck frenck marked this pull request as ready for review April 21, 2022 12:58
@frenck frenck merged commit d08b751 into dev Apr 21, 2022
@frenck frenck deleted the frenck-2022-0943 branch April 21, 2022 18:04
@github-actions github-actions bot locked and limited conversation to collaborators Apr 22, 2022
@frenck frenck added the noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear) label Apr 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed core has-tests integration: script new-feature noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear) small-pr PRs with less than 30 lines.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants