Ubiquiti Unifi Controller Switch - Unblock/Block Device Access to Unifi network

Hello all,
I created a Home Assistant custom component that allows for block/unblocking a Group of devices on a Ubiquiti Unifi Controller

Summary: I needed the ability to turn off internet access to a child’s devices, they had 4 of them. So I wrote this HASS component to allow me to flip them on or off with a single button (created a group of switched), or I can fine tune and allow select devices on or off if I want. Also allows me to use Automatons on the HASS server to turn off internet at specified times, and turn back on automatically. This is my first HASS custom component, feel free to improve.

You can find it on GitHub at https://github.com/Lopton/hassUnifiBlockSwitch

August 2019 Edit: This functionality has now been included in the base Home Assistant Device Tracker by someone else. I’m no longer updating or supporting my custom component.
Matt

11 Likes

Nice job and thanks for sharing.

How would it apply for a multi site controller?

Also, for #3 “Place the devices you want control over in a group” means the device trackers in HA or the Host Names on the Controller?

EDIT:

I guess I can specify based on the following?

"""configuration for accessing the Unifi Controller"""
CONF_HOST = 'host'
CONF_PORT = 8443
CONF_SITE_ID = 'site_id'
CONF_USERNAME = 'username'
CONF_PASSWORD = 'password'
CONF_VERIFY_SSL = True
CONF_TIMEOUT = 'timeout'
CONF_WRITE_TIMEOUT = 'write_timeout'
CONF_USER_GROUP_NAME = 'user_group_name'

God I wish I had this 10 years ago. They’re all big enough and ugly enough (19,22,23) to set their own internet hours, but as I say 10 years ago it was a problem!

Thanks. Commenting so i can use this sometime in the future :slight_smile:

Thanks for sharing the code. Will implement tonight :slight_smile:

I have no experience with multi-site controllers so I can’t help there.

For #3 the you need to place the devices in a group on the Ubiquiti Controller. (I did this to limit the number of devices that were imported as switches. However it would be trivial to remove the group restriction and just import all devices on the Ubiquiti Controller.

Can’t seem to get it working. :frowning_face:

Just a note that I updated this to the new 0.92 HASS component standards

2 Likes

Hi Matt,

Firstly, thanks for building this custom component. I’m hoping it’ll do exactly what I need.

I’ve created the config\custom_components\unifi_device_block folder and copied the switch.py file into it.

Having made the necessary changes to

“”“configuration for accessing the Unifi Controller”""
CONF_HOST = ‘my host ip address’
CONF_PORT = 8443
CONF_SITE_ID = ‘default’
CONF_USERNAME = ‘host user’
CONF_PASSWORD = ‘host password’
CONF_VERIFY_SSL = True
CONF_TIMEOUT = 1
CONF_WRITE_TIMEOUT = 1
CONF_USER_GROUP_NAME = ‘Default’

DEFAULT_HOST = ‘my host ip address’
DEFAULT_PORT = 8443
DEFAULT_SITE_ID = ‘default’
DEFAULT_VERIFY_SSL = True
DEFAULT_TIMEOUT = 1
DEFAULT_WRITE_TIMEOUT = 1

I’ve added the necessary configuration to my switches.yaml file and then completely rebooted my HASSIO instance, I’m getting the following error in the configuration checker -

“Integration unifi_device_block not found when trying to verify its switch platform.”

I probably should point out I’m running 0.94.1 on a raspberry Pi 3b.

Any idea what could be causing this?

Thanks,

Mike

You said that you copied the “switch.py” file, I’m going to guess you also need to copy the manifest and init files also?

Let me know

Matt

1 Like

Hi Matt,

Thank you for the hint. I downloaded all files,rebooted and it’s now working!

So the blocking and unblocking script works as expected…

Quick question.

Do you have any issues with devices not being able to access the internet once they’ve been blocked and subsequently unblocked? I’m getting issues with xboxes and android devices not being able to access the internet in the AM after I’ve unblocked them. The only current solution is to restart my USG. Just wondered if this was a common issue?

I have had that happen twice before. It is something with the Ubiquiti USG for sure. However the problem hasn’t occurred for me in months, so I never worried about trying to report it to Ubiquiti.

1 Like

Any idea if you are going to update this to work with newer versions of HA? It’s stopped working as of 0.95.0 due to breaking changes.

Ugg, the changes introduced in .95 (pyunifi -> aiounifi) will require a bit of work. I will work on it, but definitely nothing for 2 weeks.

To be honest 2 weeks is faster than I anticipated. Thank you for building and continuing to support this. It’s been so useful!

Hi!

Blocking clients on UniFi networks will be supported by the UniFi integration in HASS 0.97

2 Likes

This is really good news. Will it allow the same functionality? Being able to setup automations to block and unblock clients?

You configure what Mac addresses you want to be able to block and those will show up as switches in the frontend. Same as the custom component right?

That is correct. This is great news! Thank you