Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_torrents service to qBittorrent integration #106501

Merged

Conversation

finder39
Copy link
Contributor

@finder39 finder39 commented Dec 27, 2023

Breaking change

Proposed change

This adds a get_torrent() service to the qBittorrent inragration

image

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

This brings the QBittorrent integration to be more in line with the Transmission integration. It updates how the integration is written, along with adding sensors for Active Torrents, Inactive Torrents, Paused Torrents, Total Torrents, Seeding Torrents, Started Torrents.
This adds a service with a response to be able to get the list of torrents within qBittorrent
@home-assistant
Copy link

Hey there @geoffreylagaisse, mind taking a look at this pull request as it has been labeled with an integration (qbittorrent) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of qbittorrent can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign qbittorrent Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@joostlek
Copy link
Member

@engrbm87 can you also take a look at this? Since I also promoted the use of a service call for transmission, maybe it's an idea to use a similar return schema so it's easier for custom card creators to create a card that works for both integrations.

@engrbm87
Copy link
Contributor

@engrbm87 can you also take a look at this? Since I also promoted the use of a service call for transmission, maybe it's an idea to use a similar return schema so it's easier for custom card creators to create a card that works for both integrations.

Sure, I will work on something similar for transmission. But I would like to know how the response can be displayed on the frontend (like a markdown card).

@finder39
Copy link
Contributor Author

finder39 commented Dec 27, 2023

@engrbm87 can you also take a look at this? Since I also promoted the use of a service call for transmission, maybe it's an idea to use a similar return schema so it's easier for custom card creators to create a card that works for both integrations.

Sure, I will work on something similar for transmission. But I would like to know how the response can be displayed on the frontend (like a markdown card).

I built a custom card that does that for qBittorrent with this new service. I can upload it to github later today

@engrbm87
Copy link
Contributor

@engrbm87 can you also take a look at this? Since I also promoted the use of a service call for transmission, maybe it's an idea to use a similar return schema so it's easier for custom card creators to create a card that works for both integrations.

Sure, I will work on something similar for transmission. But I would like to know how the response can be displayed on the frontend (like a markdown card).

I built a custom card that does that for qBittorrent with this new service. I can upload it to github later today

Will this card work for any service response or is it specific for qBittorent. It would be hard to build a separate card for each integration.

@finder39
Copy link
Contributor Author

finder39 commented Dec 27, 2023

@engrbm87 can you also take a look at this? Since I also promoted the use of a service call for transmission, maybe it's an idea to use a similar return schema so it's easier for custom card creators to create a card that works for both integrations.

Sure, I will work on something similar for transmission. But I would like to know how the response can be displayed on the frontend (like a markdown card).

I built a custom card that does that for qBittorrent with this new service. I can upload it to github later today

Will this card work for any service response or is it specific for qBittorent. It would be hard to build a separate card for each integration.

I mean the card is specifically built for qBittorrent in this case, but its using the standard HA way of calling a service with a response. So the knowledge gained from me building it can be used elsewhere.

I built it off of https://github.com/amaximus/transmission-card, but customized it for qbittorrent and its new service call

@joostlek
Copy link
Member

I'm slowly playing with the idea of "standardised service calls". We of course have them for our entity platforms like "climate.set_hvac" or "calendar.get_events". But I think it would benefit some types of integrations as well to have a standard service call. For example public transport integrations or download integrations.

And currently there are no plans afaik to support something like that in core itself, but I'll fish around checking what others think of it and maybe we can implement it.

This doesn't have to hold us back into making 2 "custom" services return in the same data structure

@joostlek
Copy link
Member

And the idea behind it is that we don't create a complete new entity platform for public transport or download integrations, but just keep using these mostly sensor based integrations

@finder39 finder39 marked this pull request as ready for review April 15, 2024 18:27
@home-assistant home-assistant bot requested a review from joostlek April 15, 2024 18:27
homeassistant/components/qbittorrent/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/qbittorrent/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/qbittorrent/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/qbittorrent/coordinator.py Outdated Show resolved Hide resolved
homeassistant/components/qbittorrent/coordinator.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft April 16, 2024 15:31
@finder39 finder39 changed the title Add get_torrent service to qBittorrent integration Add get_torrents service to qBittorrent integration Apr 17, 2024
@finder39
Copy link
Contributor Author

The requested changes have been made

@finder39 finder39 marked this pull request as ready for review April 18, 2024 00:20
homeassistant/components/qbittorrent/strings.json Outdated Show resolved Hide resolved
homeassistant/components/qbittorrent/coordinator.py Outdated Show resolved Hide resolved
homeassistant/components/qbittorrent/helpers.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft April 18, 2024 06:47
@finder39 finder39 marked this pull request as ready for review April 18, 2024 19:55
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @finder39 👍

@emontnemery emontnemery merged commit 4cce751 into home-assistant:dev Apr 19, 2024
24 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Apr 20, 2024
Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the comments in a new PR. Thanks!

DOMAIN,
SERVICE_GET_TORRENTS,
handle_get_torrents,
supports_response=SupportsResponse.ONLY,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing a service validation schema for the services.

@@ -7,6 +7,13 @@
DEFAULT_NAME = "qBittorrent"
DEFAULT_URL = "http://127.0.0.1:8080"

STATE_ATTR_TORRENTS = "torrents"
STATE_ATTR_ALL_TORRENTS = "all_torrents"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not state attributes. Please rename the constants.

STATE_UP_DOWN = "up_down"
STATE_SEEDING = "seeding"
STATE_DOWNLOADING = "downloading"

SERVICE_GET_TORRENTS = "get_torrents"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need constants for strings that are only used once.


def __init__(self, hass: HomeAssistant, client: Client) -> None:
"""Initialize coordinator."""
self.client = client
# self.main_data: dict[str, int] = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented code. Please remove it.

Comment on lines +28 to +33
self.total_torrents: dict[str, int] = {}
self.active_torrents: dict[str, int] = {}
self.inactive_torrents: dict[str, int] = {}
self.paused_torrents: dict[str, int] = {}
self.seeding_torrents: dict[str, int] = {}
self.started_torrents: dict[str, int] = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These instance attributes are never used. Please remove them.

try:
return await self.hass.async_add_executor_job(self.client.sync_main_data)
except LoginRequired as exc:
raise ConfigEntryError("Invalid authentication") from exc
raise HomeAssistantError(str(exc)) from exc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we change the exception in this PR? That looks unrelated to the new services.

@@ -10,3 +13,48 @@ def setup_client(url: str, username: str, password: str, verify_ssl: bool) -> Cl
# Get an arbitrary attribute to test if connection succeeds
client.get_alternative_speed_status()
return client


def seconds_to_hhmmss(seconds) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter is missing a type annotation. Please type the whole signature when adding type annotations.

"fields": {
"device_id": {
"name": "[%key:common::config_flow::data::device%]",
"description": "Which service to grab the list from"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"grab the list" is too colloquial English and not correctly explained what it's referring to.

@home-assistant home-assistant unlocked this conversation Apr 29, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants