This is a step-by-step guide for enabling and disabling a firewall rule for a specific IP address using OpenWrt and Home Assistant. This method does not require an OpenWrt password in HA and also allows easy rule control from the OpenWrt GUI. It’s been tested on Home Assistant 2024.10.4 (with Supervisor) running on a Debian 12 server and OpenWrt 23.05.
Most of this info is from the Home Assistant and OpenWrt forums. Thanks to all those people sharing their knowledge and developing the software.
Openwrt:
It is assumed you have the Openwrt firewall rule already set up with a static IP address for the device involved.
1. Install packages:
luci-app-commands
conntrack
Reload the browser’s LuCi page to update the menu.
2. From a command line, log into the router using SSH and execute:
uci show firewall
Find the rule you want to control and make a note of that rule number and device IP address.
3. In LuCi bring up the System/Custom Commands page, select the “Configure” tab and click “Add”.
Enter a description and the entry below in the “Command” field for both the enable and disable strings. Replace <rule #>
with the number you noted above and <IP Address>
with the IP. Select “Public Access” for both rules.
Note: “Public Access” custom commands links need access to the OpenWrt IP address but there is no authentication required. They do require a specific URL with an embedded code to run.
4. Enable firewall rule command entry:
/bin/sh -c "uci set firewall.@rule[<rule #>].enabled=1 && uci commit firewall && service firewall restart &>/dev/null"
5. Disable firewall rule command entry:
/bin/sh -c "uci set firewall.@rule[<rule #>].enabled=0 && uci commit firewall && service firewall restart &>/dev/null && conntrack -D -s <IP Address> &>/dev/null && conntrack -D -d <IP Address> &>/dev/null"
You can enable/disable multiple rules within these entries by adding additional commands separated by “&&”.
Your commands should look like these examples if you’re referencing rule 1 and IP 10.0.0.10:
Enable rule example:
/bin/sh -c "uci set firewall.@rule[1].enabled=1 && uci commit firewall && service firewall restart &>/dev/null"
Disable rule example:
/bin/sh -c "uci set firewall.@rule[1].enabled=0 && uci commit firewall && service firewall restart &>/dev/null && conntrack -D -s 10.0.0.10 &>/dev/null && conntrack -D -d 10.0.0.10 &>/dev/null"
Click “Save & Apply”
Select the “Dashboard” tab and click on “Run” for each command to verify they run successfully and turn on and off your firewall rule. Next select “Link” for each command. You’ll copy the “display result” links shown for use in your HA config file.
Note: If you later add rules to your firewall that change the number of the firewall rule you’ve specified in your custom command, that command must be edited to reflect that change.
On your Home Assistant server:
6. If Curl isn’t installed in your HA server enter the following on a command line (Ubuntu or Debian):
sudo apt update
sudo apt install curl
In Home Assistant:
7. Edit your configuration.yaml file “command_line” section (or create one).
In this example “command_on” disables the firewall rule and allows Internet access. Command_off enables the firewall rule and blocks Internet access.
command_line:
- switch:
name: Your Switch Name
command_on: "curl -k '<Your disable link from above>' &>/dev/null"
command_off: "curl -k '<Your enable link from above>' &>/dev/null"
Your entry should look something like this:
command_line:
-switch:
name: Robot Vac Internet
command_on: "curl -k 'https://10.0.0.1/cgi-bin/luci/command/cfg094344s' &>/dev/null"
command_off: "curl -k 'https://10.0.0.1/cgi-bin/luci/command/cfg0a4344s' &>/dev/null"
After restarting HA you can create automations or add the new entity to your dashboard.
8. If you’d like to replace the default dashboard lighting bolts controls with a slider switch, add a “customize:” section to your configuration.yaml file:
homeassistant:
customize:
switch.your_switch_name:
assumed_state: false
Home Assistant must be restarted for this customize entry to take effect.
Please let me know about any errors or typos in this guide.