Skip to content

Conversation

edwardtfn
Copy link
Contributor

Description:

Important

This requires PR#10105 to be merged first as it was based on that and changes things introduced by that PR.
If that PR is not going to be merged I will be happy to remove from this one the conflicting parts.

Adds a new control(bool target_state) method to the Switch class API, providing a unified interface for setting switch state based on a boolean parameter.

Additional context

This PR adds a new public API method to the Switch class that complements the existing turn_on(), turn_off(), and toggle() methods. The control() method is particularly useful when:

  • The desired switch state is determined dynamically at runtime
  • Writing lambda expressions that need to set switch state based on conditions
  • API integrations where a boolean state parameter is more convenient

The method internally delegates to the existing turn_on() and turn_off() methods, ensuring consistency with the current implementation and maintaining all existing behavior. This is a companion PR to #10105 which adds the switch.control automation action that utilizes this new API method.

Related issue (if applicable): N/A

Pull request in esphome with YAML changes (if applicable):

Checklist:

  • I am merging into next because this is new documentation that has a matching pull-request in esphome as linked above.
    or

  • I am merging into current because this is a fix, change and/or adjustment in the current documentation and is not for a new component or feature.

  • Link added in /components/index.rst when creating new documents for new components or cookbook.

New Component Images

If you are adding a new component to ESPHome, you can automatically generate a standardized black and white component name image for the documentation.

To generate a component image:

  1. Comment on this pull request with the following command, replacing COMPONENT_NAME with your component name in UPPER_CASE format with underscores (e.g., BME280, SHT3X, DALLAS_TEMP):

    @esphomebot generate image COMPONENT_NAME
    
  2. The ESPHome bot will respond with a downloadable ZIP file containing the SVG image.

  3. Extract the SVG file and place it in the images/ folder of this repository.

  4. Use the image in your component's index table entry in /components/index.rst.

Example: For a component called "DHT22 Temperature Sensor", use:

@esphomebot generate image DHT22

> [!IMPORTANT]
> This requires PR#10105 to be merged first as it was based on that and changes things introduced by that PR.
> If that PR is not going to be merged I will be happy to remove from this one the conflicting parts.

Adds a new `control(bool target_state)` method to the Switch class API, providing a unified interface for setting switch state based on a boolean parameter.

### Additional context
This PR adds a new public API method to the Switch class that complements the existing `turn_on()`, `turn_off()`, and `toggle()` methods. The `control()` method is particularly useful when:

- The desired switch state is determined dynamically at runtime
- Writing lambda expressions that need to set switch state based on conditions
- API integrations where a boolean state parameter is more convenient

The method internally delegates to the existing `turn_on()` and `turn_off()` methods, ensuring consistency with the current implementation and maintaining all existing behavior.
This is a companion PR to #10105 which adds the `switch.control` automation action that utilizes this new API method.
Copy link

netlify bot commented Aug 7, 2025

Deploy Preview for esphome ready!

Name Link
🔨 Latest commit 4254b1b
🔍 Latest deploy log https://app.netlify.com/projects/esphome/deploys/68948a8df7269700088a47c6
😎 Deploy Preview https://deploy-preview-5214--esphome.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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.

1 participant