Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for setting the Deluge BitTorrent client in Pause."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchEntity
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import DelugeConfigEntry
13 from .coordinator import DelugeDataUpdateCoordinator
14 from .entity import DelugeEntity
15 
16 
18  hass: HomeAssistant,
19  entry: DelugeConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up the Deluge switch."""
23  async_add_entities([DelugeSwitch(entry.runtime_data)])
24 
25 
27  """Representation of a Deluge switch."""
28 
29  _attr_name = None
30 
31  def __init__(self, coordinator: DelugeDataUpdateCoordinator) -> None:
32  """Initialize the Deluge switch."""
33  super().__init__(coordinator)
34  self._attr_unique_id_attr_unique_id = f"{coordinator.config_entry.entry_id}_enabled"
35 
36  def turn_on(self, **kwargs: Any) -> None:
37  """Turn the device on."""
38  torrent_ids = self.coordinator.api.call("core.get_session_state")
39  self.coordinator.api.call("core.resume_torrent", torrent_ids)
40 
41  def turn_off(self, **kwargs: Any) -> None:
42  """Turn the device off."""
43  torrent_ids = self.coordinator.api.call("core.get_session_state")
44  self.coordinator.api.call("core.pause_torrent", torrent_ids)
45 
46  @property
47  def is_on(self) -> bool:
48  """Return state of the switch."""
49  if self.coordinator.data:
50  data = self.coordinator.data[Platform.SWITCH]
51  for torrent in data.values():
52  item = torrent.popitem()
53  if not item[1]:
54  return True
55  return False
None __init__(self, DelugeDataUpdateCoordinator coordinator)
Definition: switch.py:31
None async_setup_entry(HomeAssistant hass, DelugeConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:21