Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for monitoring pyLoad."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 from enum import StrEnum
8 from typing import Any
9 
10 from pyloadapi.api import CannotConnect, InvalidAuth, PyLoadAPI
11 
13  SwitchDeviceClass,
14  SwitchEntity,
15  SwitchEntityDescription,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import ServiceValidationError
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import PyLoadConfigEntry
22 from .const import DOMAIN
23 from .coordinator import PyLoadData
24 from .entity import BasePyLoadEntity
25 
26 
27 class PyLoadSwitch(StrEnum):
28  """PyLoad Switch Entities."""
29 
30  PAUSE_RESUME_QUEUE = "download"
31  RECONNECT = "reconnect"
32 
33 
34 @dataclass(kw_only=True, frozen=True)
36  """Describes pyLoad switch entity."""
37 
38  turn_on_fn: Callable[[PyLoadAPI], Awaitable[Any]]
39  turn_off_fn: Callable[[PyLoadAPI], Awaitable[Any]]
40  toggle_fn: Callable[[PyLoadAPI], Awaitable[Any]]
41  value_fn: Callable[[PyLoadData], bool]
42 
43 
44 SENSOR_DESCRIPTIONS: tuple[PyLoadSwitchEntityDescription, ...] = (
46  key=PyLoadSwitch.PAUSE_RESUME_QUEUE,
47  translation_key=PyLoadSwitch.PAUSE_RESUME_QUEUE,
48  device_class=SwitchDeviceClass.SWITCH,
49  turn_on_fn=lambda api: api.unpause(),
50  turn_off_fn=lambda api: api.pause(),
51  toggle_fn=lambda api: api.toggle_pause(),
52  value_fn=lambda data: data.download,
53  ),
55  key=PyLoadSwitch.RECONNECT,
56  translation_key=PyLoadSwitch.RECONNECT,
57  device_class=SwitchDeviceClass.SWITCH,
58  turn_on_fn=lambda api: api.toggle_reconnect(),
59  turn_off_fn=lambda api: api.toggle_reconnect(),
60  toggle_fn=lambda api: api.toggle_reconnect(),
61  value_fn=lambda data: data.reconnect,
62  ),
63 )
64 
65 
67  hass: HomeAssistant,
68  entry: PyLoadConfigEntry,
69  async_add_entities: AddEntitiesCallback,
70 ) -> None:
71  """Set up the pyLoad sensors."""
72 
73  coordinator = entry.runtime_data
74 
76  PyLoadSwitchEntity(coordinator, description)
77  for description in SENSOR_DESCRIPTIONS
78  )
79 
80 
82  """Representation of a pyLoad sensor."""
83 
84  entity_description: PyLoadSwitchEntityDescription
85 
86  @property
87  def is_on(self) -> bool | None:
88  """Return the state of the device."""
89  return self.entity_descriptionentity_description.value_fn(
90  self.coordinator.data,
91  )
92 
93  async def async_turn_on(self, **kwargs: Any) -> None:
94  """Turn the entity on."""
95  try:
96  await self.entity_descriptionentity_description.turn_on_fn(self.coordinator.pyload)
97  except CannotConnect as e:
99  translation_domain=DOMAIN,
100  translation_key="service_call_exception",
101  ) from e
102  except InvalidAuth as e:
104  translation_domain=DOMAIN,
105  translation_key="service_call_auth_exception",
106  ) from e
107 
108  await self.coordinator.async_refresh()
109 
110  async def async_turn_off(self, **kwargs: Any) -> None:
111  """Turn the entity on."""
112  try:
113  await self.entity_descriptionentity_description.turn_off_fn(self.coordinator.pyload)
114  except CannotConnect as e:
116  translation_domain=DOMAIN,
117  translation_key="service_call_exception",
118  ) from e
119  except InvalidAuth as e:
121  translation_domain=DOMAIN,
122  translation_key="service_call_auth_exception",
123  ) from e
124 
125  await self.coordinator.async_refresh()
126 
127  async def async_toggle(self, **kwargs: Any) -> None:
128  """Toggle the entity."""
129  try:
130  await self.entity_descriptionentity_description.toggle_fn(self.coordinator.pyload)
131  except CannotConnect as e:
133  translation_domain=DOMAIN,
134  translation_key="service_call_exception",
135  ) from e
136  except InvalidAuth as e:
138  translation_domain=DOMAIN,
139  translation_key="service_call_auth_exception",
140  ) from e
141 
142  await self.coordinator.async_refresh()
None async_setup_entry(HomeAssistant hass, PyLoadConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:70