Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions homeassistant/components/transmission/switch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for setting the Transmission BitTorrent client Turtle Mode."""

import asyncio
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
Expand All @@ -12,6 +13,7 @@
from .entity import TransmissionEntity

PARALLEL_UPDATES = 0
AFTER_WRITE_SLEEP = 2


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -70,11 +72,13 @@ async def async_turn_on(self, **kwargs: Any) -> None:
await self.hass.async_add_executor_job(
self.entity_description.on_func, self.coordinator
)
await asyncio.sleep(AFTER_WRITE_SLEEP)
await self.coordinator.async_request_refresh()

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 asyncio.sleep(AFTER_WRITE_SLEEP)
await self.coordinator.async_request_refresh()
7 changes: 7 additions & 0 deletions tests/components/transmission/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,10 @@ def _create_mock_torrent(
return Torrent(fields=torrent_data)

return _create_mock_torrent


@pytest.fixture(autouse=True)
def patch_sleep() -> Generator[None]:
"""Fixture to remove sleep in tests."""
with patch("homeassistant.components.transmission.switch.AFTER_WRITE_SLEEP", 0):
yield
39 changes: 29 additions & 10 deletions tests/components/transmission/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
from syrupy.assertion import SnapshotAssertion
from transmission_rpc.session import Session

from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import (
Expand Down Expand Up @@ -101,10 +102,10 @@ async def test_on_off_switch_with_torrents(


@pytest.mark.parametrize(
("service", "alt_speed_enabled"),
("service", "alt_speed_enabled", "expected_state"),
[
(SERVICE_TURN_ON, True),
(SERVICE_TURN_OFF, False),
(SERVICE_TURN_ON, True, "on"),
(SERVICE_TURN_OFF, False, "off"),
],
)
async def test_turtle_mode_switch(
Expand All @@ -113,19 +114,37 @@ async def test_turtle_mode_switch(
mock_config_entry: MockConfigEntry,
service: str,
alt_speed_enabled: bool,
expected_state: str,
) -> None:
"""Test turtle mode switch."""
"""Test turtle mode switch with sleep delay."""
client = mock_transmission_client.return_value

current_alt_speed = not alt_speed_enabled

def set_session_side_effect(**kwargs):
nonlocal current_alt_speed
if "alt_speed_enabled" in kwargs:
current_alt_speed = kwargs["alt_speed_enabled"]

client.set_session.side_effect = set_session_side_effect
client.get_session.side_effect = lambda: Session(
fields={"alt-speed-enabled": current_alt_speed}
)

mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()

await hass.services.async_call(
SWITCH_DOMAIN,
service,
{ATTR_ENTITY_ID: "switch.transmission_turtle_mode"},
blocking=True,
)
with patch("homeassistant.components.transmission.switch.AFTER_WRITE_SLEEP", 2):
await hass.services.async_call(
SWITCH_DOMAIN,
service,
{ATTR_ENTITY_ID: "switch.transmission_turtle_mode"},
blocking=True,
)

client.set_session.assert_called_once_with(alt_speed_enabled=alt_speed_enabled)

state = hass.states.get("switch.transmission_turtle_mode")
assert state is not None
assert state.state == expected_state
Loading