Skip to content

Conversation

@andrew-codechimp
Copy link
Contributor

@andrew-codechimp andrew-codechimp commented Nov 28, 2025

Proposed change

Add a delay to switches statuses as Transmission has a delay in reporting the new state, causing the switches to toggle back, then to their real value next update.

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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:

Copilot AI review requested due to automatic review settings November 28, 2025 15:31
@home-assistant
Copy link

Hey there @engrbm87, @JPHutchins, mind taking a look at this pull request as it has been labeled with an integration (transmission) you are listed as a code owner for? Thanks!

Code owner commands

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

@andrew-codechimp andrew-codechimp changed the title Add an optimistic delay to switch statuses on Transmission Add an optimistic value to switch statuses on Transmission Nov 28, 2025
Copilot finished reviewing on behalf of andrew-codechimp November 28, 2025 15:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds optimistic state updates to Transmission switches to prevent UI flickering when toggling switches. The Transmission API has a delay in reporting state changes, causing switches to momentarily revert before showing the correct state. The implementation introduces a 5-second optimistic delay where the switch maintains the locally set state before checking the actual API state.

Key changes:

  • Added time-based optimistic state handling with a 5-second delay window
  • Modified is_on property to return optimistic state during delay period
  • Removed coordinator refresh calls after turn_on/turn_off operations

Comment on lines 66 to 76
@property
def is_on(self) -> bool:
"""Return true if device is on."""
# The Transmission API has a delay in state change after
# calling for a change. This state delay will ensure that HA keeps an
# optimistic value of state during this period to improve the user
# experience and avoid confusion.
if self._last_action > (time.time() - self._state_delay):
return bool(self._attr_is_on)
self._last_action = time.time()
return bool(self.entity_description.is_on_func(self.coordinator))
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

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

The implementation is reinventing Home Assistant's built-in optimistic state handling. The standard approach for devices with delayed state reporting is to use _attr_assumed_state = True, which properly signals to the UI that the state is assumed rather than confirmed. This built-in pattern handles state management more reliably and is the established pattern used throughout Home Assistant integrations.

Consider using the standard assumed state pattern instead:

_attr_assumed_state = True

async def async_turn_on(self, **kwargs: Any) -> None:
    """Turn the device on."""
    self._attr_is_on = True
    self.async_write_ha_state()
    await self.hass.async_add_executor_job(
        self.entity_description.on_func, self.coordinator
    )
    await self.coordinator.async_request_refresh()

This approach is simpler, more maintainable, and follows established Home Assistant patterns.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

_attr_assumed_state does not seem to achieve what is required here.

@andrew-codechimp andrew-codechimp marked this pull request as draft November 28, 2025 15:48
@joostlek
Copy link
Member

Would it maybe make more sense to sleep for a short moment until the change is properly registered?

Copilot finished reviewing on behalf of andrew-codechimp November 29, 2025 09:41
@joostlek
Copy link
Member

Otherwise instead of adding more properties, I'd implement _handle_coordinator_update to update _attr_is_on and set _attr_is_on on turn on and off instead. Yes if the update happens a split second after you toggle it would switch back, but in a way that would be correct as that is the state at that moment

@andrew-codechimp
Copy link
Contributor Author

Would it maybe make more sense to sleep for a short moment until the change is properly registered?

Do you have an example of that? I've seen various switch implementations but not any that sleep, that would be good if I knew how.

@joostlek
Copy link
Member

In Spotify I do a sleep

I'd first try adding await self.coordinator.async_request_refresh() and if that doesn't get the right state because the new state isn't stored yet, you can just add await asyncio.sleep(x) for how long you like. Be sure to make x a constant and patch it in tests to 0

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@andrew-codechimp andrew-codechimp changed the title Add an optimistic value to switch statuses on Transmission Add a delay to switch statuses on Transmission Nov 29, 2025
@andrew-codechimp
Copy link
Contributor Author

Changed approach, the sleep was required before the self.coordinator.async_request_refresh to get this working.
Patched out the sleep in tests.

@andrew-codechimp andrew-codechimp marked this pull request as ready for review November 29, 2025 10:25
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.

You could also make a quick test of this in one of your existing tests and change the patch and then assert the state after the service call

await self.hass.async_add_executor_job(
self.entity_description.on_func, self.coordinator
)
self.async_write_ha_state()
Copy link
Member

Choose a reason for hiding this comment

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

You wouldn't need this state write though!

@home-assistant home-assistant bot marked this pull request as draft November 29, 2025 10:41
@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.

@andrew-codechimp andrew-codechimp marked this pull request as ready for review November 29, 2025 12:35
@home-assistant home-assistant bot requested a review from joostlek November 29, 2025 12:35
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.

2 participants