Skip to content

Conversation

@wjtje
Copy link
Contributor

@wjtje wjtje commented Jan 19, 2025

Breaking change

Proposed change

With this change it allows users to write scripts on Shelly devices which can emit events using Shelly.emitEvent(name, data) that will be send to event entity so automatons can react on it.

I've created this example script (shelly-double-toggle.js) that will count the number of times the input is toggled and send that result using an event to Home Assistant so automatons can react on it.

This is an example of how an event emitted by a Shelly device would show up in Home Assistant.
It shows an event entity for the double toggle script that has send an input_toggle event

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)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format 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.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @balloob, @bieniu, @thecode, @chemelli74, @bdraco, mind taking a look at this pull request as it has been labeled with an integration (shelly) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of shelly can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign shelly Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@frenck
Copy link
Member

frenck commented Jan 19, 2025

Event are hard to use; why not use an event entity?

@wjtje
Copy link
Contributor Author

wjtje commented Jan 19, 2025

How should the user create these entities? Should they automatically be created when a user fires an event from the device?

@thecode
Copy link
Member

thecode commented Jan 20, 2025

How should the user create these entities? Should they automatically be created when a user fires an event from the device?

I think that would be the best option, there is a similar implementation in BTHome.
If you use the our infra for creating the entity it would also get the name of the script (example from the Switch domain)

RPC_SCRIPT_SWITCH = RpcSwitchDescription(

@wjtje
Copy link
Contributor Author

wjtje commented Jan 20, 2025

I've got a proof of concept working where each script gets it's own event entity. But when creating those entities a list of possible event types that can be fired is required. This is not known at the time of creating because the user can create any event they want within a script.
What is the best solution or work around for this?

A partial screenshot of a home assistant dashboard where two event entities are listed, the second one in fired 6 minutes ago with the "something_event" as it's type

@wjtje wjtje marked this pull request as draft January 21, 2025 18:12
@wjtje wjtje marked this pull request as ready for review January 22, 2025 16:42
@wjtje
Copy link
Contributor Author

wjtje commented Jan 23, 2025

I've done the following modifications:

  1. Every script on the device gets it's own event entity.
  2. The different event_types for the event entity are based on the script that's received from the device using Script.GetCode and a regex to parse it.
  3. I've added tests that cover the newly added code.

Are there any other modifications required?

- Added device name to event entity
- The event entity is now only created when a script has any event types
- The test for this entity now uses snapshots
…script and will now be automatically removed when the script no longer exsists
@wjtje wjtje requested a review from bieniu January 30, 2025 09:12
Copy link
Member

@bieniu bieniu left a comment

Choose a reason for hiding this comment

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

Looks good, just one small comment.

@home-assistant home-assistant bot marked this pull request as draft February 1, 2025 18:02
@home-assistant
Copy link

home-assistant bot commented Feb 1, 2025

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

…_listeners and used _event_listeners instead to listen for script events
@wjtje
Copy link
Contributor Author

wjtje commented Feb 1, 2025

I've removed the dangling ATTR_ variables that where left by the first commit as described in #135979 (comment)

@wjtje wjtje marked this pull request as ready for review February 1, 2025 21:36
@home-assistant home-assistant bot requested a review from chemelli74 February 1, 2025 21:36
Copy link
Member

@thecode thecode left a comment

Choose a reason for hiding this comment

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

Thanks @wjtje 👍

Copy link
Member

@bieniu bieniu left a comment

Choose a reason for hiding this comment

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

Looks good, tested with a real device, thanks @wjtje 👍

@thecode thecode changed the title Executing Shelly.emitEvent from scripts will now send it to the hass event bus Add Shelly script events entities Feb 2, 2025
@thecode thecode added the noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear) label Feb 2, 2025
@thecode thecode merged commit 6fa87da into home-assistant:dev Feb 3, 2025
32 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Feb 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla-signed integration: shelly new-feature noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear) Quality Scale: No score small-pr PRs with less than 30 lines.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants