Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for monitoring the qBittorrent API."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.helpers.update_coordinator import CoordinatorEntity
15 
16 from .const import DOMAIN
17 from .coordinator import QBittorrentDataCoordinator
18 
19 
20 @dataclass(frozen=True, kw_only=True)
22  """Describes qBittorren switch."""
23 
24  is_on_func: Callable[[QBittorrentDataCoordinator], bool]
25  turn_on_fn: Callable[[QBittorrentDataCoordinator], None]
26  turn_off_fn: Callable[[QBittorrentDataCoordinator], None]
27  toggle_func: Callable[[QBittorrentDataCoordinator], None]
28 
29 
30 SWITCH_TYPES: tuple[QBittorrentSwitchEntityDescription, ...] = (
32  key="alternative_speed",
33  translation_key="alternative_speed",
34  icon="mdi:speedometer-slow",
35  is_on_func=lambda coordinator: coordinator.get_alt_speed_enabled(),
36  turn_on_fn=lambda coordinator: coordinator.set_alt_speed_enabled(True),
37  turn_off_fn=lambda coordinator: coordinator.set_alt_speed_enabled(False),
38  toggle_func=lambda coordinator: coordinator.toggle_alt_speed_enabled(),
39  ),
40 )
41 
42 
44  hass: HomeAssistant,
45  config_entry: ConfigEntry,
46  async_add_entities: AddEntitiesCallback,
47 ) -> None:
48  """Set up qBittorrent switch entries."""
49 
50  coordinator: QBittorrentDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
51 
53  QBittorrentSwitch(coordinator, config_entry, description)
54  for description in SWITCH_TYPES
55  )
56 
57 
58 class QBittorrentSwitch(CoordinatorEntity[QBittorrentDataCoordinator], SwitchEntity):
59  """Representation of a qBittorrent switch."""
60 
61  _attr_has_entity_name = True
62  entity_description: QBittorrentSwitchEntityDescription
63 
64  def __init__(
65  self,
66  coordinator: QBittorrentDataCoordinator,
67  config_entry: ConfigEntry,
68  entity_description: QBittorrentSwitchEntityDescription,
69  ) -> None:
70  """Initialize qBittorrent switch."""
71  super().__init__(coordinator)
72  self.entity_descriptionentity_description = entity_description
73  self._attr_unique_id_attr_unique_id = f"{config_entry.entry_id}-{entity_description.key}"
74  self._attr_device_info_attr_device_info = DeviceInfo(
75  entry_type=DeviceEntryType.SERVICE,
76  identifiers={(DOMAIN, config_entry.entry_id)},
77  manufacturer="QBittorrent",
78  )
79 
80  @property
81  def is_on(self) -> bool:
82  """Return true if device is on."""
83  return self.entity_descriptionentity_description.is_on_func(self.coordinator)
84 
85  async def async_turn_on(self, **kwargs: Any) -> None:
86  """Turn on this switch."""
87  await self.hasshasshass.async_add_executor_job(
88  self.entity_descriptionentity_description.turn_on_fn, self.coordinator
89  )
90  await self.coordinator.async_request_refresh()
91 
92  async def async_turn_off(self, **kwargs: Any) -> None:
93  """Turn off this switch."""
94  await self.hasshasshass.async_add_executor_job(
95  self.entity_descriptionentity_description.turn_off_fn, self.coordinator
96  )
97  await self.coordinator.async_request_refresh()
98 
99  async def async_toggle(self, **kwargs: Any) -> None:
100  """Toggle the device."""
101  await self.hasshasshass.async_add_executor_job(
102  self.entity_descriptionentity_description.toggle_func, self.coordinator
103  )
104  await self.coordinator.async_request_refresh()
None __init__(self, QBittorrentDataCoordinator coordinator, ConfigEntry config_entry, QBittorrentSwitchEntityDescription entity_description)
Definition: switch.py:69
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:47