Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions homeassistant/components/transmission/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
return bool(self.entity_description.is_on_func(self.coordinator))
Comment on lines 65 to 68
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.


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()
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!


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()
Loading