|
13 | 13 | TransmissionConnectError, |
14 | 14 | TransmissionError, |
15 | 15 | ) |
16 | | -import voluptuous as vol |
17 | 16 |
|
18 | | -from homeassistant.config_entries import ConfigEntryState |
19 | 17 | from homeassistant.const import ( |
20 | 18 | CONF_HOST, |
21 | | - CONF_ID, |
22 | 19 | CONF_NAME, |
23 | 20 | CONF_PASSWORD, |
24 | 21 | CONF_PATH, |
|
27 | 24 | CONF_USERNAME, |
28 | 25 | Platform, |
29 | 26 | ) |
30 | | -from homeassistant.core import HomeAssistant, ServiceCall, callback |
31 | | -from homeassistant.exceptions import ( |
32 | | - ConfigEntryAuthFailed, |
33 | | - ConfigEntryNotReady, |
34 | | - HomeAssistantError, |
35 | | -) |
36 | | -from homeassistant.helpers import ( |
37 | | - config_validation as cv, |
38 | | - entity_registry as er, |
39 | | - selector, |
40 | | -) |
| 27 | +from homeassistant.core import HomeAssistant, callback |
| 28 | +from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady |
| 29 | +from homeassistant.helpers import config_validation as cv, entity_registry as er |
41 | 30 | from homeassistant.helpers.typing import ConfigType |
42 | 31 |
|
43 | | -from .const import ( |
44 | | - ATTR_DELETE_DATA, |
45 | | - ATTR_DOWNLOAD_PATH, |
46 | | - ATTR_TORRENT, |
47 | | - CONF_ENTRY_ID, |
48 | | - DEFAULT_DELETE_DATA, |
49 | | - DEFAULT_PATH, |
50 | | - DEFAULT_SSL, |
51 | | - DOMAIN, |
52 | | - SERVICE_ADD_TORRENT, |
53 | | - SERVICE_REMOVE_TORRENT, |
54 | | - SERVICE_START_TORRENT, |
55 | | - SERVICE_STOP_TORRENT, |
56 | | -) |
| 32 | +from .const import DEFAULT_PATH, DEFAULT_SSL, DOMAIN |
57 | 33 | from .coordinator import TransmissionConfigEntry, TransmissionDataUpdateCoordinator |
58 | 34 | from .errors import AuthenticationError, CannotConnect, UnknownError |
| 35 | +from .services import async_setup_services |
59 | 36 |
|
60 | 37 | _LOGGER = logging.getLogger(__name__) |
61 | 38 |
|
|
76 | 53 | "Turtle Mode": "turtle_mode", |
77 | 54 | } |
78 | 55 |
|
79 | | -SERVICE_BASE_SCHEMA = vol.Schema( |
80 | | - { |
81 | | - vol.Required(CONF_ENTRY_ID): selector.ConfigEntrySelector( |
82 | | - {"integration": DOMAIN} |
83 | | - ), |
84 | | - } |
85 | | -) |
86 | | - |
87 | | -SERVICE_ADD_TORRENT_SCHEMA = vol.All( |
88 | | - SERVICE_BASE_SCHEMA.extend( |
89 | | - { |
90 | | - vol.Required(ATTR_TORRENT): cv.string, |
91 | | - vol.Optional(ATTR_DOWNLOAD_PATH): cv.string, |
92 | | - } |
93 | | - ), |
94 | | -) |
95 | | - |
96 | | - |
97 | | -SERVICE_REMOVE_TORRENT_SCHEMA = vol.All( |
98 | | - SERVICE_BASE_SCHEMA.extend( |
99 | | - { |
100 | | - vol.Required(CONF_ID): cv.positive_int, |
101 | | - vol.Optional(ATTR_DELETE_DATA, default=DEFAULT_DELETE_DATA): cv.boolean, |
102 | | - } |
103 | | - ) |
104 | | -) |
105 | | - |
106 | | -SERVICE_START_TORRENT_SCHEMA = vol.All( |
107 | | - SERVICE_BASE_SCHEMA.extend({vol.Required(CONF_ID): cv.positive_int}), |
108 | | -) |
109 | | - |
110 | | -SERVICE_STOP_TORRENT_SCHEMA = vol.All( |
111 | | - SERVICE_BASE_SCHEMA.extend( |
112 | | - { |
113 | | - vol.Required(CONF_ID): cv.positive_int, |
114 | | - } |
115 | | - ) |
116 | | -) |
117 | 56 |
|
118 | 57 | CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) |
119 | 58 |
|
120 | 59 |
|
121 | 60 | async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: |
122 | 61 | """Set up the Transmission component.""" |
123 | | - setup_hass_services(hass) |
| 62 | + async_setup_services(hass) |
124 | 63 | return True |
125 | 64 |
|
126 | 65 |
|
@@ -203,95 +142,6 @@ async def async_migrate_entry( |
203 | 142 | return True |
204 | 143 |
|
205 | 144 |
|
206 | | -def _get_coordinator_from_service_data( |
207 | | - hass: HomeAssistant, entry_id: str |
208 | | -) -> TransmissionDataUpdateCoordinator: |
209 | | - """Return coordinator for entry id.""" |
210 | | - entry: TransmissionConfigEntry | None = hass.config_entries.async_get_entry( |
211 | | - entry_id |
212 | | - ) |
213 | | - if entry is None or entry.state is not ConfigEntryState.LOADED: |
214 | | - raise HomeAssistantError(f"Config entry {entry_id} is not found or not loaded") |
215 | | - return entry.runtime_data |
216 | | - |
217 | | - |
218 | | -def setup_hass_services(hass: HomeAssistant) -> None: |
219 | | - """Home Assistant services.""" |
220 | | - |
221 | | - async def add_torrent(service: ServiceCall) -> None: |
222 | | - """Add new torrent to download.""" |
223 | | - entry_id: str = service.data[CONF_ENTRY_ID] |
224 | | - coordinator = _get_coordinator_from_service_data(hass, entry_id) |
225 | | - torrent: str = service.data[ATTR_TORRENT] |
226 | | - download_path: str | None = service.data.get(ATTR_DOWNLOAD_PATH) |
227 | | - if torrent.startswith( |
228 | | - ("http", "ftp:", "magnet:") |
229 | | - ) or hass.config.is_allowed_path(torrent): |
230 | | - if download_path: |
231 | | - await hass.async_add_executor_job( |
232 | | - partial( |
233 | | - coordinator.api.add_torrent, torrent, download_dir=download_path |
234 | | - ) |
235 | | - ) |
236 | | - else: |
237 | | - await hass.async_add_executor_job(coordinator.api.add_torrent, torrent) |
238 | | - await coordinator.async_request_refresh() |
239 | | - else: |
240 | | - _LOGGER.warning("Could not add torrent: unsupported type or no permission") |
241 | | - |
242 | | - async def start_torrent(service: ServiceCall) -> None: |
243 | | - """Start torrent.""" |
244 | | - entry_id: str = service.data[CONF_ENTRY_ID] |
245 | | - coordinator = _get_coordinator_from_service_data(hass, entry_id) |
246 | | - torrent_id = service.data[CONF_ID] |
247 | | - await hass.async_add_executor_job(coordinator.api.start_torrent, torrent_id) |
248 | | - await coordinator.async_request_refresh() |
249 | | - |
250 | | - async def stop_torrent(service: ServiceCall) -> None: |
251 | | - """Stop torrent.""" |
252 | | - entry_id: str = service.data[CONF_ENTRY_ID] |
253 | | - coordinator = _get_coordinator_from_service_data(hass, entry_id) |
254 | | - torrent_id = service.data[CONF_ID] |
255 | | - await hass.async_add_executor_job(coordinator.api.stop_torrent, torrent_id) |
256 | | - await coordinator.async_request_refresh() |
257 | | - |
258 | | - async def remove_torrent(service: ServiceCall) -> None: |
259 | | - """Remove torrent.""" |
260 | | - entry_id: str = service.data[CONF_ENTRY_ID] |
261 | | - coordinator = _get_coordinator_from_service_data(hass, entry_id) |
262 | | - torrent_id = service.data[CONF_ID] |
263 | | - delete_data = service.data[ATTR_DELETE_DATA] |
264 | | - await hass.async_add_executor_job( |
265 | | - partial(coordinator.api.remove_torrent, torrent_id, delete_data=delete_data) |
266 | | - ) |
267 | | - await coordinator.async_request_refresh() |
268 | | - |
269 | | - hass.services.async_register( |
270 | | - DOMAIN, SERVICE_ADD_TORRENT, add_torrent, schema=SERVICE_ADD_TORRENT_SCHEMA |
271 | | - ) |
272 | | - |
273 | | - hass.services.async_register( |
274 | | - DOMAIN, |
275 | | - SERVICE_REMOVE_TORRENT, |
276 | | - remove_torrent, |
277 | | - schema=SERVICE_REMOVE_TORRENT_SCHEMA, |
278 | | - ) |
279 | | - |
280 | | - hass.services.async_register( |
281 | | - DOMAIN, |
282 | | - SERVICE_START_TORRENT, |
283 | | - start_torrent, |
284 | | - schema=SERVICE_START_TORRENT_SCHEMA, |
285 | | - ) |
286 | | - |
287 | | - hass.services.async_register( |
288 | | - DOMAIN, |
289 | | - SERVICE_STOP_TORRENT, |
290 | | - stop_torrent, |
291 | | - schema=SERVICE_STOP_TORRENT_SCHEMA, |
292 | | - ) |
293 | | - |
294 | | - |
295 | 145 | async def get_api( |
296 | 146 | hass: HomeAssistant, entry: dict[str, Any] |
297 | 147 | ) -> transmission_rpc.Client: |
|
0 commit comments