import requests | |
import json | |
import urllib3 | |
import argparse | |
import udm_wlan_secrets | |
UNIFI_URL = udm_wlan_secrets.unifi_url | |
UNIFI_USER = udm_wlan_secrets.unifi_username | |
UNIFI_PASS = udm_wlan_secrets.unifi_password | |
UNIFI_LOGIN_PATH = '/api/auth/login' | |
UNIFI_WLAN_PATH = '/proxy/network/api/s/default/rest/wlanconf/' | |
# Disable SSL verification warnings | |
urllib3.disable_warnings() | |
class Unifi(object): | |
def __init__(self, host, username, password): | |
self.host = host | |
self.username = username | |
self.password = password | |
self.session = requests.Session() | |
self.csrf = "" | |
def login(self): | |
payload = { | |
"username": self.username, | |
"password": self.password, | |
} | |
r = self.request(UNIFI_LOGIN_PATH, payload) | |
return r.ok | |
def status(self, wlan_id): | |
r = self.request(UNIFI_WLAN_PATH+wlan_id, method='GET') | |
wlan_status = r.json() | |
name = wlan_status['data'][0]['name'] | |
wlan_active = wlan_status['data'][0]['enabled'] | |
if wlan_active == True: | |
print(name+' is ENABLED') | |
elif wlan_active == False: | |
print(name+' is DISABLED') | |
return r.ok | |
def disable(self, wlan_id): | |
payload = { | |
"_id": wlan_id, | |
"enabled": False | |
} | |
r = self.request(UNIFI_WLAN_PATH+wlan_id, payload, method='PUT') | |
return r.ok | |
def enable(self, wlan_id): | |
payload = { | |
"_id": wlan_id, | |
"enabled": True | |
} | |
r = self.request(UNIFI_WLAN_PATH+wlan_id, payload, method='PUT') | |
return r.ok | |
def request(self, path, data={}, method='POST'): | |
uri = 'https://{}{}'.format(self.host, path) | |
headers = { | |
"Accept": "application/json", | |
"Content-Type": "application/json; charset=utf-8", | |
} | |
if self.csrf: | |
headers["X-CSRF-Token"] = self.csrf | |
r = getattr(self.session, method.lower())(uri, json=data, verify=False, headers=headers) | |
try: | |
self.csrf = r.headers['X-CSRF-Token'] | |
except KeyError: | |
pass | |
return r | |
if __name__ == "__main__": | |
u = Unifi(UNIFI_URL,UNIFI_USER,UNIFI_PASS) | |
u.login() | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--wlan_id', help='ID of WLAN Network to Target') | |
parser.add_argument('--function', help='Status / Enable / Disable') | |
args = parser.parse_args() | |
if args.function == 'enable': | |
enable = u.enable(args.wlan_id) | |
status = u.status(args.wlan_id) | |
elif args.function == 'disable': | |
disable = u.disable(args.wlan_id) | |
status = u.status(args.wlan_id) | |
elif args.function == 'status': | |
status = u.status(args.wlan_id) |
unifi_url = '' | |
unifi_username = '' | |
unifi_password = '' |
@alexlmiller, have you been able to implement this as a swith in home assistant.
You script works when trying from a command shell on a random raspberry.
However it doesn't not work on my Home Assistant Blue Edition which is where my Home Assistant is running:
switch:
#Switch creation enabling/disabling Guest Wifi
- platform: command_line
switches:
guest_wifi:
command_on: python3 /config/python_scripts/udm_wlan_control.py --wlan_id XXX --function enable
command_off: python3 /config/python_scripts/udm_wlan_control.py --wlan_id XXX --function disable
command_state: python3 /config/python_scripts/udm_wlan_control.py --wlan_id XXX --function status
friendly_name: Guest Wifi
Any ideas what I might be doing wrong?
Looks like the script fails to run.
@gulli1986 I believe you need to use AppDaemon or Pyscript
@alexlmiller, thanks. I was hoping it would work right off the bat but understands this will not work due to "imports".
Thanks anyway, will look at those alternatives.
Thank you @alexlmiller - using your script with a UDM works perfectly; here is my Home Assistant switch:
switch:
- platform: command_line
switches:
guest_wifi:
command_on: >
python3 /config/scripts/udm_wlan_control.py --wlan_id XXX --function enable
command_off: >
python3 /config/scripts/udm_wlan_control.py --wlan_id XXX --function disable
command_state: >
python3 /config/scripts/udm_wlan_control.py --wlan_id XXX --function status
value_template: >
{% if value == "SSID is ENABLED" %}
true
{% else %}
false
{% endif %}
friendly_name: Guest WiFi
@gulli1986 you can try my bash script here
I put it together for a friend who is using HA OS like you
thanks @rjcds !
Working now, just had a small issue with 1.11.0 update but it is fine now!
@gulli1986 are you referring to my script, or @alexlmiller 's ?
Mine is working on 1.11.0 as far as I can see for a friend with a UDM Pro on 1.11.0
@alexlmiller 's python script is working for me on 1.11.0 on a UDM base
@rjcds I was having trouble with @alexlmiller 's python script and 1.11.0.
I switched to your script, it is now working fine with HA OS and unifi 1.11.0, thanks!
!thanks @rjcds . Works great, appreciate the effort.
Hello @alexlmiller. Just wanted to thank you for your script. Could not get this to work with my UDM PRO, a big thank to you!