![[HOW TO] Toggle OPNsense Firewall Rule via Home Assistant (HASS)](https://external-preview.redd.it/OAXSl8SY6T3JK9MGQyKxkoYbqZ71HQRYXLeB8CV0NXg.png?width=1080&crop=smart&auto=webp&s=912f966e123e94e32e7975fe8aebac89450a6b98)
[HOW TO] Toggle OPNsense Firewall Rule via Home Assistant (HASS)
Not sure how many people will find this helpful, but wanted to share my documentation for toggling firewall rules via Home Assistant.
https://gist.github.com/zyphermonkey/bb039afa3d382389c60c4d3a0200efb4
Toggle OPNsense Firewall Rule via Home Assistant (HASS)
OPNsense
Create Account for HASS to authenticate with
System > Access > Users
- Create account in opnsense with
All pagesprivileges - Click
Create and download API key for this user - Create basic auth string for use with cURL.
- combine
key:secret - base64 encode
- prefix with
"Basic "
- combine
- Validate
auth stringvia simple curl commandcurl -k https://192.168.1.1/api/core/system/status \ -H "Authorization: $AUTH_STRING"
Create Alias
For my scenario I want to block a specific MAC so I got it via the DHCP Leases page.
Services>ISC DHCPv4[legacy]>Leases
- Go to Firewall > Aliases > New
- Info
- Enabled:
checked - Name:
<Hostname>
This can be anything you want, but I set it to the Hostname because it makes the most sense. - Type:
MAC address - Content:
<mac_address_recorded_from_leases_page>
- Enabled:
- Apply
Create Firwall Rule
If you don't already have a rule you want to toggle of if you want to create one just for initial testing with HASS.
- Go to Firewall > Rules[new]
- Create whatever
Get Firewall Rule UUID
- Go to Firewall > Rules[new]
- Open Browers developer tools (F12)
- Click
editon rule - The latest line in the
Networktab in Developer Tools should be aGETrequest to/api/firewall/filter/get_rule/<uuid>- Record
uuidfor use in HASS commands
- Record
Home Assistant (HASS)
Create opnsense auth var in secrets.yaml`
- Open
secrets.yamland create new variable with base64 encoded api key from OPNsense.opnsense_api_auth: "Basic <base64_encoded key:secret>"
Create sensor
Home Assistant needs to be able to know the current state of the firewall rule to effectively toggle it (and show it's status on dashboards).
configuration.yaml
sensor:
- platform: rest
name: opnsense_fw_rule_state_yourrulenamehere
resource: https://192.168.1.1/api/firewall/filter/get_rule/<uuid>
method: GET
value_template: "{{ value_json.rule.enabled }}"
headers:
Authorization: !secret opnsense_api_auth
verify_ssl: false
Create switch template & rest commands
- Create switch template in configuration.yaml
template: - switch: - name: internet_access_yourrulenamehere unique_id: internet_access_yourrulenamehere state: "{{ is_state('sensor.opnsense_fw_rule_state_yourrulenamehere', '1') }}" turn_on: service: rest_command.opnsense_rule_on_yourrulenamehere turn_off: service: rest_command.opnsense_rule_off_yourrulenamehere - Create rest commands (apply, enable, disable) in configuration.yaml
rest_command: opnsense_apply: url: https://192.168.1.1/api/firewall/filter/apply method: POST headers: Authorization: !secret opnsense_api_auth verify_ssl: false opnsense_rule_on_yourrulenamehere: url: https://192.168.1.1/api/firewall/filter/toggle_rule/<uuid>/1 method: POST headers: Authorization: !secret opnsense_api_auth verify_ssl: false opnsense_rule_off_yourrulenamehere: url: https://192.168.1.1/api/firewall/filter/toggle_rule/<uuid>/0 method: POST headers: Authorization: !secret opnsense_api_auth verify_ssl: false
Validate
Ensure changes maded to secrets.yaml and configuration.yaml are stable check your configuration after saving.
- Settings > Developer tools > Check configuration
Use in your dashboards/automations
You should now be able to add the switch and sensor to your dashboards with the following:
entity: switch.internet_access_yourrulenamehereentity: sensor.opnsense_fw_rule_state_yourrulenamehere