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

Feat/component poller suspend #5423

Merged
merged 10 commits into from
Oct 2, 2023

Conversation

xdecock
Copy link
Contributor

@xdecock xdecock commented Sep 24, 2023

What does this implement/fix?

This pr aims at allowing suspend / resume of polling component update, this is mainly in place to help out with "slow component" when managing audio interractions.

PS: Took a bit of time to understand how to start the version of my pull request, tested and it does work as i expected

Tested on a D1 Mini & and C3 Pico

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): fixes esphome/feature-requests#2301

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#3205

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040

Example entry for config.yaml:

# Example config.yaml
esphome:
  name: test
  friendly_name: test
  platformio_options:
    board_build.flash_mode: dio
  on_boot:
    then:
      - logger.log: Wait
      - delay: 10s
      - component.suspend: adc_pin_3
      - logger.log: Stopped
      - delay: 10s
      - logger.log: Resume
      - component.resume: adc_pin_3
      - delay: 10s
      - logger.log: Stop 2
      - component.suspend: adc_pin_3
      - delay: 10s
      - logger.log: Start Slow
      - component.resume:
          id: adc_pin_3
          update_interval: 10s


esp32:
  board: lolin_c3_mini
  framework:
    type: esp-idf

# Enable logging
logger:
  level: VERBOSE
  hardware_uart: USB_SERIAL_JTAG

sensor:
  - platform: adc
    pin: GPIO3
    name: "Test 1"
    update_interval: 2s
    id: adc_pin_3

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

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

@nagyrobi
Copy link
Member

Would it be possible to start the poller in suspended state at boot?

The use case is, for example having to wait for a slave device to start up (as it's slower than the esp), before starting to poll it.

@xdecock
Copy link
Contributor Author

xdecock commented Sep 24, 2023

Would it be possible to start the poller in suspended state at boot?

The use case is, for example having to wait for a slave device to start up (as it's slower than the esp), before starting to poll it.

I thought about that, and imo, the most elegant way to do that, would be to add an action:

  component.set_update_interval;
    id: component_id
    interval: xxxs

This mr do allow to "dynamically update the interval"

The only thing i did not understand is how to create an action with that template.

That being, said, if i'm guided about how to implement those type of actions, i would probably do it as a separate pull request.

I'd then solve this exact scenario this way:

fast_component:
  id: fc_id
  update_interval: never
  
slow_component:
  id: sc_id
  on_startup:
    - component.set_interval:
      id: fc_id
      update_interval: 5s 
      resume: false #not sure about this or the next line
    - component.resume: fc_id

@jesserockz
Copy link
Member

Instead of another action to change the interval, you could add an Optional config item to the start action for update_interval

@xdecock
Copy link
Contributor Author

xdecock commented Sep 25, 2023

Instead of another action to change the interval, you could add an Optional config item to the start action for update_interval

So more something in the vein of

component.resume:
  id: fc_id
  update_interval: 3s

An extreme version would allow to merge those two actions via

component.set_update_interval:
  id: fc_id
  update_interval: never

component.set_update_interval:
  id: fc_id
  update_interval: 5s

# possible helpers
component.set_update_interval:
  id: fc_id
  action: suspend

component.set_update_interval:
  id: fc_id
  action: resume

If this would work, i'd be happy with a pointer to the correct class to declare the correct structure in python, I'm quite happy to update my mr but i'm struggling a bit to find how things work.

@xdecock
Copy link
Contributor Author

xdecock commented Sep 25, 2023

Fast tentative,

I'll debug it later today if it doesn't work, if the logic is sound, i'll redo my local tests, and update the documentation accordingly

@xdecock xdecock force-pushed the feat/component-poller-suspend branch from 3eed0c8 to d31d24e Compare September 25, 2023 15:40
@xdecock
Copy link
Contributor Author

xdecock commented Sep 25, 2023

Updated the example, seems it works correctly on my local devices. i've struggled a bit to understand the code generation logic, so i'm happy with any correction i might have to do

@xdecock xdecock force-pushed the feat/component-poller-suspend branch from f9637c9 to 014d134 Compare September 28, 2023 20:20
esphome/automation.py Outdated Show resolved Hide resolved
@xdecock xdecock force-pushed the feat/component-poller-suspend branch from 7198031 to 9f78ad3 Compare September 29, 2023 13:08
@xdecock
Copy link
Contributor Author

xdecock commented Sep 29, 2023

Would it be possible to start the poller in suspended state at boot?

The use case is, for example having to wait for a slave device to start up (as it's slower than the esp), before starting to poll it.

this is now possible by starting with update_interval: never, and doing a component.resume when the slow component booted up, and set the interval with the resume action

@xdecock xdecock force-pushed the feat/component-poller-suspend branch from 9f78ad3 to a0b9648 Compare October 2, 2023 18:14
@jesserockz jesserockz merged commit a33b8ab into esphome:dev Oct 2, 2023
30 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 4, 2023
bdraco added a commit that referenced this pull request Jan 24, 2024
bdraco added a commit that referenced this pull request Jan 24, 2024
bdraco added a commit that referenced this pull request Jan 24, 2024
bdraco added a commit that referenced this pull request Jan 24, 2024
bdraco added a commit that referenced this pull request Jan 24, 2024
kbx81 pushed a commit that referenced this pull request Jan 24, 2024
* Ensure filename is shown when YAML raises an error

fixes #5423
fixes #5377

* Ensure filename is shown when YAML raises an error

fixes #5423
fixes #5377

* Ensure filename is shown when YAML raises an error

fixes #5423
fixes #5377

* Ensure filename is shown when YAML raises an error

fixes #5423
fixes #5377

* Ensure filename is shown when YAML raises an error

fixes #5423
fixes #5377
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.

Suspend update for PollingComponent
3 participants