1 """Support for monitoring pyLoad."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable
6 from dataclasses
import dataclass
7 from enum
import StrEnum
10 from pyloadapi.api
import CannotConnect, InvalidAuth, PyLoadAPI
15 SwitchEntityDescription,
21 from .
import PyLoadConfigEntry
22 from .const
import DOMAIN
23 from .coordinator
import PyLoadData
24 from .entity
import BasePyLoadEntity
28 """PyLoad Switch Entities."""
30 PAUSE_RESUME_QUEUE =
"download"
31 RECONNECT =
"reconnect"
34 @dataclass(kw_only=True, frozen=True)
36 """Describes pyLoad switch entity."""
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]
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,
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,
68 entry: PyLoadConfigEntry,
69 async_add_entities: AddEntitiesCallback,
71 """Set up the pyLoad sensors."""
73 coordinator = entry.runtime_data
77 for description
in SENSOR_DESCRIPTIONS
82 """Representation of a pyLoad sensor."""
84 entity_description: PyLoadSwitchEntityDescription
88 """Return the state of the device."""
90 self.coordinator.data,
94 """Turn the entity on."""
97 except CannotConnect
as e:
99 translation_domain=DOMAIN,
100 translation_key=
"service_call_exception",
102 except InvalidAuth
as e:
104 translation_domain=DOMAIN,
105 translation_key=
"service_call_auth_exception",
111 """Turn the entity on."""
114 except CannotConnect
as e:
116 translation_domain=DOMAIN,
117 translation_key=
"service_call_exception",
119 except InvalidAuth
as e:
121 translation_domain=DOMAIN,
122 translation_key=
"service_call_auth_exception",
128 """Toggle the entity."""
131 except CannotConnect
as e:
133 translation_domain=DOMAIN,
134 translation_key=
"service_call_exception",
136 except InvalidAuth
as e:
138 translation_domain=DOMAIN,
139 translation_key=
"service_call_auth_exception",
None async_toggle(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, PyLoadConfigEntry entry, AddEntitiesCallback async_add_entities)