Skip to content

Commit

Permalink
Add add_links service (#44)
Browse files Browse the repository at this point in the history
* Bump myjdapi from 1.1.6 to 1.1.7
* Add add_links service
  • Loading branch information
oribafi authored Apr 14, 2024
1 parent e333572 commit 3af5402
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Note: number of links/packages sensors contain state attributes that have inform
- `myjdownloader.restart_and_update`
- `myjdownloader.start_downloads`
- `myjdownloader.stop_downloads`
- `myjdownloader.add_links`

Note: Only select a single _entity_ (e.g., the *_status entity) from the JDownloader when calling a service, not the JDownloader _device_.

Expand Down
2 changes: 2 additions & 0 deletions custom_components/myjdownloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
DOMAIN as MYJDOWNLOADER_DOMAIN,
MYJDAPI_APP_KEY,
SCAN_INTERVAL_SECONDS,
SERVICE_ADD_LINKS,
SERVICE_RESTART_AND_UPDATE,
SERVICE_RUN_UPDATE_CHECK,
SERVICE_START_DOWNLOADS,
Expand Down Expand Up @@ -184,6 +185,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.services.async_remove(MYJDOWNLOADER_DOMAIN, SERVICE_RUN_UPDATE_CHECK)
hass.services.async_remove(MYJDOWNLOADER_DOMAIN, SERVICE_START_DOWNLOADS)
hass.services.async_remove(MYJDOWNLOADER_DOMAIN, SERVICE_STOP_DOWNLOADS)
hass.services.async_remove(MYJDOWNLOADER_DOMAIN, SERVICE_ADD_LINKS)

# unload platforms
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
Expand Down
11 changes: 11 additions & 0 deletions custom_components/myjdownloader/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@
SERVICE_RUN_UPDATE_CHECK = "run_update_check"
SERVICE_START_DOWNLOADS = "start_downloads"
SERVICE_STOP_DOWNLOADS = "stop_downloads"
SERVICE_ADD_LINKS = "add_links"

FIELD_LINKS = "links"
FIELD_PRIORITY = "priority"
FIELD_AUTOSTART = "auto_extract"
FIELD_AUTO_EXTRACT = "autostart"
FIELD_PACKAGE_NAME = "package_name"
FIELD_EXTRACT_PASSWORD = "extract_password"
FIELD_DOWNLOAD_PASSWORD = "download_password"
FIELD_DESTINATION_FOLDER = "destination_folder"
FIELD_OVERWRITE_PACKAGIZER_RULES = "overwrite_packagizer_rules"
35 changes: 35 additions & 0 deletions custom_components/myjdownloader/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,38 @@ async def stop_downloads(self):
"""Service call to stop downloads."""
device = self.hub.get_device(self._device_id)
await self.hub.async_query(device.downloadcontroller.stop_downloads)

async def add_links(
self,
links: list[str],
priority: str,
auto_extract: bool = False,
autostart: bool = False,
destination_folder: str | None = None,
download_password: str | None = None,
extract_password: str | None = None,
overwrite_packagizer_rules: bool = False,
package_name: str | None = None,
):
"""Service call to add links."""
# https://my.jdownloader.org/developers/index.html#tag_244
params = [
{
# assignJobID
"autoExtract": auto_extract,
"autostart": autostart,
# dataURLs
# deepDecrypt
"destinationFolder": destination_folder,
"downloadPassword": download_password,
"extractPassword": extract_password,
"links": str(links),
"overwritePackagizerRules": overwrite_packagizer_rules,
"packageName": package_name,
"priority": priority.upper(),
# sourceUrl
}
]
_LOGGER.warning(params)
device = self.hub.get_device(self._device_id)
await self.hub.async_query(device.linkgrabber.add_links, params)
3 changes: 2 additions & 1 deletion custom_components/myjdownloader/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"restart_and_update": "mdi:restart",
"run_update_check": "mdi:update",
"start_downloads": "mdi:play",
"stop_downloads": "mdi:stop"
"stop_downloads": "mdi:stop",
"add_links": "mdi:plus"
}
}
2 changes: 1 addition & 1 deletion custom_components/myjdownloader/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://github.com/doudz/homeassistant-myjdownloader",
"issue_tracker": "https://github.com/doudz/homeassistant-myjdownloader/issues",
"requirements": ["myjdapi==1.1.6"],
"requirements": ["myjdapi==1.1.7"],
"dependencies": [],
"codeowners": ["@doudz", "@oribafi"],
"version": "2.4.0",
Expand Down
28 changes: 27 additions & 1 deletion custom_components/myjdownloader/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from typing import Any

from myjdapi.myjdapi import Jddevice
import voluptuous as vol

from homeassistant.components.sensor import DOMAIN, SensorEntity, SensorStateClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory, UnitOfDataRate
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_platform
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Expand All @@ -21,7 +22,17 @@
ATTR_PACKAGES,
DATA_MYJDOWNLOADER_CLIENT,
DOMAIN as MYJDOWNLOADER_DOMAIN,
FIELD_AUTO_EXTRACT,
FIELD_AUTOSTART,
FIELD_DESTINATION_FOLDER,
FIELD_DOWNLOAD_PASSWORD,
FIELD_EXTRACT_PASSWORD,
FIELD_LINKS,
FIELD_OVERWRITE_PACKAGIZER_RULES,
FIELD_PACKAGE_NAME,
FIELD_PRIORITY,
SCAN_INTERVAL_SECONDS,
SERVICE_ADD_LINKS,
SERVICE_RESTART_AND_UPDATE,
SERVICE_RUN_UPDATE_CHECK,
SERVICE_START_DOWNLOADS,
Expand Down Expand Up @@ -93,6 +104,21 @@ def async_add_sensor(devices=hub.devices):
{},
"stop_downloads",
)
platform.async_register_entity_service(
SERVICE_ADD_LINKS,
{
vol.Required(FIELD_LINKS): cv.ensure_list(cv.url),
vol.Required(FIELD_PRIORITY): cv.string,
vol.Optional(FIELD_PACKAGE_NAME): cv.string,
vol.Optional(FIELD_AUTOSTART): cv.boolean,
vol.Optional(FIELD_AUTO_EXTRACT): cv.boolean,
vol.Optional(FIELD_EXTRACT_PASSWORD): cv.string,
vol.Optional(FIELD_DOWNLOAD_PASSWORD): cv.string,
vol.Optional(FIELD_DESTINATION_FOLDER): cv.string,
vol.Optional(FIELD_OVERWRITE_PACKAGIZER_RULES): cv.boolean,
},
"add_links",
)


class MyJDownloaderDeviceSensor(MyJDownloaderDeviceEntity, SensorEntity):
Expand Down
67 changes: 67 additions & 0 deletions custom_components/myjdownloader/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,70 @@ stop_downloads:
target:
device:
integration: myjdownloader
add_links:
target:
device:
integration: myjdownloader
fields:
links:
required: true
example: "http://example.org/index.html"
selector:
text:
multiple: true
type: url
autostart:
required: false
example: false
default: false
selector:
boolean:
package_name:
required: false
example: "My Download Package"
selector:
text:
priority:
required: true
example: "default"
default: "default"
selector:
select:
translation_key: "priority"
options:
- highest
- higher
- high
- default
- low
- lower
- lowest
download_password:
required: false
selector:
text:
type: password
extract_password:
required: false
selector:
text:
type: password
auto_extract:
required: false
example: false
default: false
selector:
boolean:
destination_folder:
required: false
advanced: true
example: "/home/ubuntu/Downloads"
selector:
text:
overwrite_packagizer_rules:
required: false
advanced: true
example: false
default: false
selector:
boolean:
55 changes: 55 additions & 0 deletions custom_components/myjdownloader/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,61 @@
"start_downloads": {
"name": "[%key:common::action::start%]",
"description": "Starts downloading."
},
"add_links": {
"description": "Add links to LinkGrabber.",
"name": "Add links",
"fields": {
"links": {
"name": "Links",
"description": "The link(s) to add."
},
"autostart": {
"name": "Autostart",
"description": "Start downloads automatically after adding?"
},
"package_name": {
"name": "Package name",
"description": "The name of the package."
},
"extract_password": {
"name": "Extract password",
"description": "The password for archive extraction."
},
"auto_extract": {
"name": "Auto extract archives",
"description": "Extract downloaded archives automatically?"
},
"priority": {
"name": "Priority",
"description": "The priority of the link(s)."
},
"download_password": {
"name": "Download password",
"description": "The password for the link."
},
"destination_folder": {
"name": "Destination folder",
"description": "The destination folder."
},
"overwrite_packagizer_rules": {
"name": "Overwrite packagizer rules",
"description": "Overwrite packagizer rules?"
}
}
}
},
"selector": {
"priority": {
"options": {
"highest": "Highest",
"higher": "Higher",
"high": "High",
"default": "Default",
"low": "Low",
"lower": "Lower",
"lowest": "Lowest"
}
}
}
}
55 changes: 55 additions & 0 deletions custom_components/myjdownloader/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,62 @@
}
}
},
"selector": {
"priority": {
"options": {
"default": "Default",
"high": "High",
"higher": "Higher",
"highest": "Highest",
"low": "Low",
"lower": "Lower",
"lowest": "Lowest"
}
}
},
"services": {
"add_links": {
"description": "Add links to LinkGrabber.",
"fields": {
"auto_extract": {
"description": "Extract downloaded archives automatically?",
"name": "Auto extract archives"
},
"autostart": {
"description": "Start downloads automatically after adding?",
"name": "Autostart"
},
"destination_folder": {
"description": "The destination folder.",
"name": "Destination folder"
},
"download_password": {
"description": "The password for the link.",
"name": "Download password"
},
"extract_password": {
"description": "The password for archive extraction.",
"name": "Extract password"
},
"links": {
"description": "The link(s) to add.",
"name": "Links"
},
"overwrite_packagizer_rules": {
"description": "Overwrite packagizer rules?",
"name": "Overwrite packagizer rules"
},
"package_name": {
"description": "The name of the package.",
"name": "Package name"
},
"priority": {
"description": "The priority of the link(s).",
"name": "Priority"
}
},
"name": "Add links"
},
"restart_and_update": {
"description": "Restarts and updates JDownloader.",
"name": "Restart and update"
Expand Down

0 comments on commit 3af5402

Please sign in to comment.