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
17 from .
import PyLoadConfigEntry
18 from .const
import DOMAIN
19 from .entity
import BasePyLoadEntity
22 @dataclass(kw_only=True, frozen=True)
24 """Describes pyLoad button entity."""
26 press_fn: Callable[[PyLoadAPI], Awaitable[Any]]
30 """PyLoad button Entities."""
32 ABORT_DOWNLOADS =
"abort_downloads"
33 RESTART_FAILED =
"restart_failed"
34 DELETE_FINISHED =
"delete_finished"
38 SENSOR_DESCRIPTIONS: tuple[PyLoadButtonEntityDescription, ...] = (
40 key=PyLoadButtonEntity.ABORT_DOWNLOADS,
41 translation_key=PyLoadButtonEntity.ABORT_DOWNLOADS,
42 press_fn=
lambda api: api.stop_all_downloads(),
45 key=PyLoadButtonEntity.RESTART_FAILED,
46 translation_key=PyLoadButtonEntity.RESTART_FAILED,
47 press_fn=
lambda api: api.restart_failed(),
50 key=PyLoadButtonEntity.DELETE_FINISHED,
51 translation_key=PyLoadButtonEntity.DELETE_FINISHED,
52 press_fn=
lambda api: api.delete_finished(),
55 key=PyLoadButtonEntity.RESTART,
56 translation_key=PyLoadButtonEntity.RESTART,
57 press_fn=
lambda api: api.restart(),
58 entity_registry_enabled_default=
False,
65 entry: PyLoadConfigEntry,
66 async_add_entities: AddEntitiesCallback,
68 """Set up buttons from a config entry."""
70 coordinator = entry.runtime_data
74 for description
in SENSOR_DESCRIPTIONS
79 """Representation of a pyLoad button."""
81 entity_description: PyLoadButtonEntityDescription
84 """Handle the button press."""
87 except CannotConnect
as e:
89 translation_domain=DOMAIN,
90 translation_key=
"service_call_exception",
92 except InvalidAuth
as e:
94 translation_domain=DOMAIN,
95 translation_key=
"service_call_auth_exception",