-
-
Notifications
You must be signed in to change notification settings - Fork 36k
Add a delay to switch statuses on Transmission #157493
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
base: dev
Are you sure you want to change the base?
Add a delay to switch statuses on Transmission #157493
Conversation
|
Hey there @engrbm87, @JPHutchins, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
There was a problem hiding this 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_onproperty to return optimistic state during delay period - Removed coordinator refresh calls after turn_on/turn_off operations
| @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)) |
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Would it maybe make more sense to sleep for a short moment until the change is properly registered? |
|
Otherwise instead of adding more properties, I'd implement |
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. |
|
In Spotify I do a sleep I'd first try adding |
There was a problem hiding this 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.
|
Changed approach, the sleep was required before the |
joostlek
left a comment
There was a problem hiding this 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() |
There was a problem hiding this comment.
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!
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
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
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: