Skip to content

Conversation

@LordMike
Copy link
Contributor

@LordMike LordMike commented Mar 8, 2025

Fixes #245

Updates the async_track_state_change with async_track_state_change_event, as per this post.

@LordMike LordMike changed the base branch from main to dev March 8, 2025 15:42
@LordMike LordMike force-pushed the feature/async_track_state_change_event branch from 8f7afaf to 5c6a7e8 Compare March 8, 2025 15:43
Copy link
Owner

@claytonjn claytonjn left a comment

Choose a reason for hiding this comment

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

Thank you for the pull request, I appreciate the contribution, saving me a bit of time, getting ahead of this change, and benefiting others who use this component.

I left a couple comments on the diff, the one I'm concerned about is a code path that breaks in this commit. If that doesn't affect you and you don't want to fix it let me know and I can follow up with a fix.

@LordMike
Copy link
Contributor Author

LordMike commented Mar 8, 2025

Python is not at all my primary language. I think I've covered your mentions. Happy to help out :)
I entirely missed out on the remaining method call - d'oh.

@claytonjn claytonjn merged commit c7500ad into claytonjn:dev Mar 8, 2025
4 checks passed
@LordMike LordMike deleted the feature/async_track_state_change_event branch March 9, 2025 10:10
thcipriani added a commit to thcipriani/hass-circadian_lighting that referenced this pull request Aug 22, 2025
Whenever a light turns off, I see this in my HomeAssisstant logs:

    Error doing job: Task exception was never retrieved (None)
    Traceback (most recent call last):
    File "/config/custom_components/circadian_lighting/switch.py", line 384,
    in _light_state_changed
    assert new_state and new_state.state == "on"
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError`

This is caused by an `assert` in the `_light_state_changed` function.

I've refactored `_light_state_change` to remove the `assert` while while
keeping the existing function behavior.

When claytonjn#258 replaced `async_track_state_change` with
`async_track_state_change_event` due to upstream changes, there was a
change in the upstream function signature.

Previously, `async_track_state_change` took a `state` argument, so
events were only triggered when a light's state was `on`, so the
assertion in `_light_state_changed` was probably unneeded still, but
fine.

Now, `async_track_state_change_event` sends every event to the
`_light_state_changed` callback. So when the light state changes to
"off" or "unavailable", this assertion throws an exception.

My refactor determines if the light has turned on and triggers a forced
update if so. This is the same logic as before my change, but without an
assertion.

Fixes: claytonjn#262
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

async_track_state_change deprecated

2 participants