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

Update template switch to new format #128944

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

Petro31
Copy link
Contributor

@Petro31 Petro31 commented Oct 21, 2024

Breaking change

No

Proposed change

Add switch to new template yaml format

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 @PhracturedBlue, @tetienne, @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of template 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 template 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.

@Petro31 Petro31 marked this pull request as draft October 21, 2024 21:09
@Petro31 Petro31 force-pushed the add-new-template-switch-config branch from efc4262 to 7404c25 Compare October 27, 2024 14:31
@Petro31 Petro31 marked this pull request as ready for review October 27, 2024 14:51
homeassistant/components/template/switch.py Outdated Show resolved Hide resolved
@@ -219,3 +297,90 @@ async def async_turn_off(self, **kwargs: Any) -> None:
if self._template is None:
self._state = False
self.async_write_ha_state()


class TriggerSwitchEntity(TriggerEntity, SwitchEntity, RestoreEntity):
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be better to have an AbstractSwitchTemplate class from which both SwitchTemplate and TriggerSwitchEntity` inherit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be a nice idea, however none of the existing template entities do this and there are challenges that arise. It is something that I want to do, but I want it to be done at a integration level, not domain level.

async def async_turn_on(self, **kwargs: Any) -> None:
"""Fire the on action."""
if self._on_script:
await self.async_run_script(self._on_script, context=self._context)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see the need for a new method which takes the script as a parameter. Either async_run_script should be defined in this class and grab the required script from the object properties OR just add this as one of the variables when calling self._on_script.async_run(...).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That new method will be used by all trigger based template entities (light, fan, select, etc) to provide the action scripts with the this variable, just like non trigger based template entities. This function is needed to fix this long standing issue. It's added here first and it will be added to all other domains at a later date.

@frenck
Copy link
Member

frenck commented Nov 8, 2024

Hi there @Petro31 👋

There is a merge conflict, could you take a look?

Thanks! 👍

../Frenck

@frenck frenck marked this pull request as draft November 8, 2024 22:28
@Petro31 Petro31 force-pushed the add-new-template-switch-config branch from a97d21c to 3e6c9d1 Compare November 20, 2024 11:09
@Petro31 Petro31 marked this pull request as ready for review November 23, 2024 10:56
Copy link
Member

@joostlek joostlek left a comment

Choose a reason for hiding this comment

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

Tests are failing, can you take a look?

@home-assistant
Copy link

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.

@home-assistant home-assistant bot marked this pull request as draft December 17, 2024 18:59
@Petro31 Petro31 force-pushed the add-new-template-switch-config branch from c301956 to b54c34c Compare December 24, 2024 13:25
@Petro31 Petro31 marked this pull request as ready for review December 24, 2024 13:28
@home-assistant home-assistant bot requested a review from joostlek December 24, 2024 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants