-
-
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?
Changes from 1 commit
79c8346
6fc165e
ca66f29
1b7347e
bc819ab
4be0b6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| import time | ||
| from typing import Any | ||
|
|
||
| from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription | ||
|
|
@@ -59,22 +60,35 @@ class TransmissionSwitch(TransmissionEntity, SwitchEntity): | |
| """Representation of a Transmission switch.""" | ||
|
|
||
| entity_description: TransmissionSwitchEntityDescription | ||
| _state_delay = 5 # seconds | ||
| _last_action = 0.0 | ||
|
|
||
| @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() | ||
andrew-codechimp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return bool(self.entity_description.is_on_func(self.coordinator)) | ||
|
Comment on lines
65
to
68
|
||
|
|
||
| async def async_turn_on(self, **kwargs: Any) -> None: | ||
| """Turn the device on.""" | ||
| await self.hass.async_add_executor_job( | ||
| self.entity_description.on_func, self.coordinator | ||
| ) | ||
| await self.coordinator.async_request_refresh() | ||
| self._last_action = time.time() | ||
| self._attr_is_on = True | ||
| self.async_write_ha_state() | ||
andrew-codechimp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| async def async_turn_off(self, **kwargs: Any) -> None: | ||
| """Turn the device off.""" | ||
| await self.hass.async_add_executor_job( | ||
| self.entity_description.off_func, self.coordinator | ||
| ) | ||
| await self.coordinator.async_request_refresh() | ||
| self._last_action = time.time() | ||
| self._attr_is_on = False | ||
| self.async_write_ha_state() | ||
andrew-codechimp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
andrew-codechimp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.