in home automation

Seamless TV DNS switching with dnsmasq and Home Assistant

Some Black Friday ago, I happened to buy a lifetime subscription to Getflix. It’s a service that allows you to bypass Netflix regional restrictions by using their DNS servers. The advantage is that you don’t get stuck with lower speeds as you probably would with a VPN. The service has been working pretty well apart from a few outages some time ago (looks like Netflix is actively working against them) and I use it to watch US Netflix and Prime Video from Italy. I think there are other similar services so this guide will apply to those too.

When I’m on my PC, I use DNS Jumper to switch between Google and Getflix DNS servers. However, when I wanted to watch US Netflix on my LG TV I had to manually change the network configuration, not very comfortable with a remote. I couldn’t either leave the Getflix DNS always configured, as my family wouldn’t want to have English-only flicks nor lose some Italian-only movies Netflix probably offers.

The solution was to use my Raspberry Pi as a DNS server, point the TV to it and integrate a script into Home Assistant so I could manage the whole thing from my phone together with other lights and switches in my house.

The first thing to do is to install dnsmasq, a DNS and DHCP server, on your Pi:

I wasn’t interested on the DHCP part so I haven’t enabled it. By looking at the dnsmasq manual I found some interesting and useful features:

  • dnsmasq gets its upstream (i.e. ‘reference’) DNS servers from /etc/resolv.conf , a file that on my Pi was auto-generated by resolvconf. You can either change resolvconf configuration (via the /etc/resolveconf.conf  file) or force dnsmasq to ignore the file and use specific DNS servers. I chose the second option.
  • dnsmasq caches DNS queries, which is a good thing in general but bad for our use case. By sending a SIGHUP to its process, we can force a cache cleaning.

We will now create two copies of the dnsmasq config file and a very simple script that switches between them.

We need to add (or uncomment) the following lines in the /etc/dnsmasq.conf  file:

We can now copy the files:

Now we force Getflix DNS servers, appending the following to the /etc/dnsmasq.nflx.conf  config file:

We then do the same for the /etc/dnsmasq.goog.conf file:

We can now write the script that handles the change between the two files. We   touch /home/pi/dnschange  and edit it with our favorite text editor.

Make the script executable:

We’re about done. By running sudo /home/pi/dnschange netflix  we will be able to watch the US Netflix, and by running sudo /home/pi/dnschange google  we will revert back to Google DNS servers.

You can make sure the script is working by running  cat /etc/dnsmasq.conf | grep server= and see if the file has the right servers inside.

Still, it would be quite unpractical to SSH into our Pi from the couch and run the two commands. Let’s integrate the thing with Home Assistant so we can easily run the script from our mobile web browser.

Edit the HA config file and add the following lines. For me, it’s in  /home/homeassistant/.homeassistant/configuration.yaml :

Then, let’s add the homeassistant user to sudoers so it can run the commands as root without asking for a password (make sure your network is secure!), by editing the sudoers file

and adding the following line:

We can now restart Home Assistant and we should be ready to go.

This is how the switch looks on my phone. I’m still a Home Assistant newbie so the interface is a bit bare, but I plan on adding automatic blinds and more lights in the coming weeks.

It’s quite a dirty hack and could be integrated better (e.g. we could make HA verify the DNS change) but it works perfectly for me: the only thing I have to do is kill and re-open the Netflix app on my TV. Happy watching!

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Share on RedditEmail this to someoneFlattr the authorShare on Tumblr

Write a Comment

Comment

 

Loading Disqus Comments ...
Loading Facebook Comments ...