Skip to content

Commit

Permalink
Merge pull request #1001 from onkelandy/unifi
Browse files Browse the repository at this point in the history
Unifi Plugin: make it work on Unifi (Express) device, minor updates
  • Loading branch information
onkelandy authored Feb 28, 2025
2 parents f687805 + 6fe7364 commit e72cb7e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
11 changes: 8 additions & 3 deletions unifi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

class UniFiConst(object):
PARAMETER_URL = 'unifi_controller_url'
PARAMETER_TYPE = 'unifi_controller_type'
PARAMETER_USER = 'unifi_user'
PARAMETER_PWD = 'unifi_password'
PARAMETER_SITE_ID = 'unifi_site_id'
Expand Down Expand Up @@ -138,7 +139,9 @@ def get_items(self, filter=None):
def _generate_item_key(self, source:str):
step1 = source.lower().replace(' ', '_')
step2 = re.sub('[^0-9a-z_]+', '_', step1)
return step2.replace("___", "_").replace("__", "_")
step3 = step2.replace("___", "_").replace("__", "_")
step4 = step3.strip("_")
return step4

def _get_device_node_item(self, node_data, parent_type=""):
n = node_data['node']
Expand Down Expand Up @@ -289,7 +292,7 @@ class UniFiControllerClient(SmartPlugin):
the update functions for the items
"""

PLUGIN_VERSION = '1.6.3'
PLUGIN_VERSION = '1.6.4'

def __init__(self, sh, *args, **kwargs):
"""
Expand All @@ -303,6 +306,7 @@ def __init__(self, sh, *args, **kwargs):

# get the parameters for the plugin (as defined in metadata plugin.yaml):
self._unifi_controller_url = self.get_parameter_value(UniFiConst.PARAMETER_URL)
self._unifi_controller_type = self.get_parameter_value(UniFiConst.PARAMETER_TYPE)
self._unifi_user = self.get_parameter_value(UniFiConst.PARAMETER_USER)
self._unifi_password = self.get_parameter_value(UniFiConst.PARAMETER_PWD)
self._unifi_site_id = self.get_parameter_value(UniFiConst.PARAMETER_SITE_ID)
Expand All @@ -311,6 +315,7 @@ def __init__(self, sh, *args, **kwargs):
password=self._unifi_password,
site=self._unifi_site_id,
baseurl=self._unifi_controller_url,
unifitype=self._unifi_controller_type,
verify_ssl=False))

# cycle time in seconds, only needed, if hardware/interface needs to be
Expand Down Expand Up @@ -361,7 +366,7 @@ def _log_item_info(self, item, msg: str, enable_logging=True, defaulting=False):
if defaulting:
self._model.append_item_issue(item, "2: " + msg, 2)
else:
self._model.append_item_issue(item, "1: "+msg, 1)
self._model.append_item_issue(item, "1: " + msg, 1)

if enable_logging:
self.logger.info(msg + " in item " + item.property.path)
Expand Down
12 changes: 11 additions & 1 deletion unifi/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugin:
# documentation: https://github.com/smarthomeNG/smarthome/wiki/CLI-Plugin # url of documentation (wiki) page
support: https://knx-user-forum.de/forum/supportforen/smarthome-py/1278068-unifi-controller-api-wlan

version: 1.6.3 # Plugin version
version: 1.6.4 # Plugin version
sh_minversion: '1.9.0' # minimum shNG version to use this plugin
# sh_maxversion: # maximum shNG version to use this plugin (leave empty if latest)
multi_instance: False # plugin supports multi instance
Expand All @@ -28,6 +28,16 @@ parameters:
de: 'Basis URL des Controller, z.B. https://unifi:8443'
en: 'Base URL of the controller, e.g. https://unifi:8443'

unifi_controller_type:
type: str
default: 'software'
valid_list:
- 'device'
- 'software'
description:
de: 'Basis URL des Controller, z.B. https://unifi:8443'
en: 'Base URL of the controller, e.g. https://unifi:8443'

unifi_user:
type: str
mandatory: True
Expand Down
25 changes: 18 additions & 7 deletions unifi/ubiquiti/unifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class API(object):
_device_cache = {}
_request_count = 0

def __init__(self, username: str = "ubnt", password: str = "ubnt", site: str = "default", baseurl: str = "https://unifi:8443", verify_ssl: bool = True):
def __init__(self, username: str = "ubnt", password: str = "ubnt", site: str = "default", baseurl: str = "https://unifi:8443", verify_ssl: bool = True, unifitype: str = "software"):
"""
Initiates tha api with default settings if none other are set.
Expand All @@ -55,7 +55,18 @@ def __init__(self, username: str = "ubnt", password: str = "ubnt", site: str = "
self._login_data['password'] = password
self._site = site
self._verify_ssl = verify_ssl
self._baseurl = baseurl
self._headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"User-Agent": "Mozilla/5.0"
}
if unifitype == 'device':
self._baseurl = f'{baseurl}/proxy/network'
self._loginurl = f'{baseurl}/api/auth/login'
else:
self._baseurl = baseurl
self._loginurl = f'{baseurl}/api/login'
self._unifitype = unifitype
self._session = Session()
self._request_count = 0

Expand Down Expand Up @@ -87,7 +98,7 @@ def _get_url(self, url, recursion=False):

if not r.ok:
if current_status_code == 401:
raise LoggedInException("Invalid login, or login has expired")
raise LoggedInException(f"Invalid login while getting, or login has expired. r: {r} {current_status_code}")

data = r.json()['data']
return data
Expand All @@ -106,7 +117,7 @@ def _put_url(self, url, body, recursion=False):
current_status_code = r.status_code
if not r.ok:
if current_status_code == 401:
raise LoggedInException("Invalid login, or login has expired")
raise LoggedInException("Invalid login while putting, or login has expired")
else:
raise LoggedInException("code {}".format(current_status_code))

Expand All @@ -127,8 +138,8 @@ def login(self):
if self._is_logged_in:
return

current_status_code = self._session.post("{}/api/login".format(
self._baseurl), data=json.dumps(self._login_data), verify=self._verify_ssl).status_code
current_status_code = self._session.post("{}".format(
self._loginurl), data=json.dumps(self._login_data), headers=self._headers, verify=self._verify_ssl).status_code
self._request_count = self._request_count + 1
if current_status_code == 400:
raise LoggedInException("Failed to log in to api with provided credentials")
Expand Down Expand Up @@ -311,7 +322,7 @@ def get_port_profile_for(self, switch_mac: str, port_number: int):
break
if not poFound:
raise DataException("Could not match any port in data to given port-number {}".format(port_number))
port_prof = poData[poIndex]['portconf_id']
port_prof = poData[poIndex].get('portconf_id')

profiles = self._get_port_profiles()
if len(profiles) == 0:
Expand Down
2 changes: 2 additions & 0 deletions unifi/user_doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Getestet mit:
* UniFi Controller 5.10.23 im Docker Container auf Synology
* UniFi UAP AC Lite, Mesh, Longrange, Pro
* UniFi Switch US-8-60W
* Unifi Express


Konfiguration
=============
Expand Down
2 changes: 1 addition & 1 deletion unifi/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
<th>{{ _('Item Wert') }}</th>
<th>{{ _('Letztes Update') }}</th>
<th>{{ _('Letzter Change') }}</th>
<th>{{ _('Probleme') }}</th>
<th>{{ _('Log') }}</th>
</tr>
</thead>
<tbody>
Expand Down

0 comments on commit e72cb7e

Please sign in to comment.