Toggle Opnsense Unbound Blacklist from HA

I wanted to be able to toggle the blacklist function of Unbound in Opnsense from Home Assistant. The reason is simple: TVNow (a German TV streaming service) doesn’t work with my blacklists and I still haven’t figured out what all to whitelist for it to work. Logging into my OpnSense takes longer than simply clicking a switch in Home Assistant. I wanted to use the Restful Switch, but for the state I have to use GET and for changing the state I have to use POST with different URLs. I didn’t work for me, but maybe somebody can help me out there.
The button:
grafik
It toggles the blacklist function on of of.

My configuration:

switch:
  - platform: template
    switches:
      unbound:
        friendly_name: 'Unbound'
        value_template: "{{ is_state('sensor.unbound_status', '1') }}"
        turn_on:
          service: rest_command.enable_unbound
        turn_off:
          service: rest_command.disable_unbound

rest_command:
  enable_unbound:
    url: https://router/api/unbound/settings/set
    method: post
    content_type:  'application/json; charset=utf-8'
    username: 'key'
    password: 'secret'
    payload: '{"unbound":{"dnsbl":{"enabled":"1"}}}'
  disable_unbound:
    url: https://router/api/unbound/settings/set
    method: post
    content_type:  'application/json; charset=utf-8'
    username: 'key'
    password: 'secret'
    payload: '{"unbound":{"dnsbl":{"enabled":"0"}}}'

sensor:

  - platform: rest
    resource: https://router/api/unbound/overview/isBlockListEnabled
    name: 'Unbound Status'
    value_template: '{{ value_json.dnsbl.enabled }}'
    username: 'key'
    password: 'secret'
    authentication: basic

Pressing the button immediately turns the black list on or of, but it takes a second for the sensor to register the new state

The api has only changed slightly … update as follows and the rest of this still works great!

url for post: https://router/api/unbound/settings/set
payload for post: '{"unbound":{"dnsbl":{"enabled":"0"}}}' (or 1 to enable)

url for get: https://router/api/unbound/overview/isBlockListEnabled

1 Like

Ah, didn’t know that. I’ll try it out.

That works. Thanks. I have changed the first post to reflect the newer API.