AdGuard service (e.g. enable_url) failed

I tried to call adguard.enable_url using example data, but got error code 400, invalid url or file path.
Screenshot provided.


I am using AdGuard 2.6.1.

I also tried the script/automation from another post (Social media filter), but that does not work either.

My question is how to use adguard.enable_url. Any example or comments is appreciated.

Below are relevant logs:


Logger: homeassistant.components.websocket_api.http.connection
Source: components/adguard/__init__.py:89
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 3:10:16 AM (3 occurrences)
Last logged: 3:10:29 AM

[2824697048] ('Failed enabling URL on AdGuard Home filter', AdGuardHomeError(400, {'message': 'invalid URL or file path\n'}))
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/adguardhome/filtering.py", line 88, in enable_url
    await self._adguard._request(
  File "/usr/local/lib/python3.8/site-packages/adguardhome/adguardhome.py", line 121, in _request
    raise AdGuardHomeError(
adguardhome.exceptions.AdGuardHomeError: (400, {'message': 'invalid URL or file path\n'})

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 135, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1451, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1486, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/components/adguard/__init__.py", line 89, in enable_url
    await adguard.filtering.enable_url(call.data.get(CONF_URL))
  File "/usr/local/lib/python3.8/site-packages/adguardhome/filtering.py", line 94, in enable_url
    raise AdGuardHomeError(
adguardhome.exceptions.AdGuardHomeError: ('Failed enabling URL on AdGuard Home filter', AdGuardHomeError(400, {'message': 'invalid URL or file path\n'}))

This is an error in the add guard component
I’ve changed my config to get the same result

I used add_url and remove_url as shown in your Github. It is working now. Thanks.

automation:
  - id: enable_youtube
    alias: Enable Youtube
    trigger:
      platform: state
      entity_id: input_boolean.allowyoutube
      to: 'on'
    action:
    - service: adguard.remove_url
      data:
        url: "https://raw.githubusercontent.com/gieljnssns/Social-media-Blocklists/master/adguard-youtube.txt"

  - id: disable_youtube
    alias: Disable Youtube
    trigger:
      platform: state
      entity_id: input_boolean.allowyoutube
      to: 'off'
    action:
    - service: adguard.add_url
      data:
        url: "https://raw.githubusercontent.com/gieljnssns/Social-media-Blocklists/master/adguard-youtube.txt"
        name: Youtube
1 Like

So I have been digging into this a lot. Problem with the enable_url service is within the Adguard Library. The API has changed in the parameters it expects. I created a python script to test my suspicions.

import asyncio
import argparse
import json

from adguardhome import AdGuardHome, AdGuardHomeError

async def main(args_lst):
    """Show example how to get status of your AdGuard Home instance."""
    async with AdGuardHome("IP_ADDRESS", username='USERNAME', password='PASSWORD') as adguard:

        # Enable URL
        try:
            response = await adguard._request(
                "filtering/set_url",
                method="POST",
                json_data={
                    "url": args_lst.url,
                    "data": {
                        "name": args_lst.name,
                        "url": args_lst.url,
                        "enabled": args_lst.enable
                    },
                    "whitelist": False
                    }
            )

            # Verify Response
            response = await adguard._request(
                    "filtering/status",
                    method="GET",
                    json_data={
                        "url": args_lst.url,
                        "data": {
                            "name": args_lst.name,
                            "url": args_lst.url,
                            "enabled": args_lst.enable
                        },
                        "whitelist": False
                        }
                )

            response = json.loads(json.dumps(response))['filters']
            match = list(filter(lambda x:x["name"]==args_lst.name, response))[0]

            print('Name: "{}" Rules Enabled: {}'.format(match['name'], match['rules_count']))

        except AdGuardHomeError as exception:
            raise AdGuardHomeError(
                "Failed enabling URL on AdGuard Home filter", exception
            )

if __name__ == "__main__":

    # Set Up Arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("url")
    parser.add_argument("name")
    parser.add_argument("-e", "--enable",  action="store_true", default=False, help='Enable filter list')

    # Run logic
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(parser.parse_args()))

It is very crude but changes will have to occur in the python package and the home assistant adguard component to pass the extra parameters in the json data.

On another note, Adguard home supports passing a local file path as a custom filter but Home Assistant will block this because it is not a valid URL (http / https).