Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for NZBGet switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_NAME
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import DATA_COORDINATOR, DOMAIN
14 from .coordinator import NZBGetDataUpdateCoordinator
15 from .entity import NZBGetEntity
16 
17 
19  hass: HomeAssistant,
20  entry: ConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Set up NZBGet sensor based on a config entry."""
24  coordinator: NZBGetDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
25  DATA_COORDINATOR
26  ]
27 
28  switches = [
30  coordinator,
31  entry.entry_id,
32  entry.data[CONF_NAME],
33  ),
34  ]
35 
36  async_add_entities(switches)
37 
38 
40  """Representation of a NZBGet download switch."""
41 
42  _attr_translation_key = "download"
43 
44  def __init__(
45  self,
46  coordinator: NZBGetDataUpdateCoordinator,
47  entry_id: str,
48  entry_name: str,
49  ) -> None:
50  """Initialize a new NZBGet switch."""
51  self._attr_unique_id_attr_unique_id = f"{entry_id}_download"
52 
53  super().__init__(
54  coordinator=coordinator,
55  entry_id=entry_id,
56  entry_name=entry_name,
57  )
58 
59  @property
60  def is_on(self):
61  """Return the state of the switch."""
62  return not self.coordinator.data["status"].get("DownloadPaused", False)
63 
64  async def async_turn_on(self, **kwargs: Any) -> None:
65  """Set downloads to enabled."""
66  await self.hasshasshass.async_add_executor_job(self.coordinator.nzbget.resumedownload)
67  await self.coordinator.async_request_refresh()
68 
69  async def async_turn_off(self, **kwargs: Any) -> None:
70  """Set downloads to paused."""
71  await self.hasshasshass.async_add_executor_job(self.coordinator.nzbget.pausedownload)
72  await self.coordinator.async_request_refresh()
None __init__(self, NZBGetDataUpdateCoordinator coordinator, str entry_id, str entry_name)
Definition: switch.py:49
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:22