Unifi sensor custom component

Thank you very much for such a quick response! :smiley:

Iā€™ve tried your suggestions - firstly just adding to the URL and then changing the path within the sensor.py file but not having any luck - Iā€™m getting this error in my logsā€¦

2021-01-29 11:57:40 ERROR (MainThread) [homeassistant.components.sensor] my_unifi: Error on device update!
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 466, in async_device_update
    await task
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/my_unifi/sensor.py", line 107, in update
    self._data.update()
  File "/config/custom_components/my_unifi/sensor.py", line 240, in update
    for device in device_list:
TypeError: 'NoneType' object is not iterable

Does that suggest that the changes havenā€™t worked?

Hi,

hmā€¦ not sure. It seems that either it couldnt login (but you should see a error saying that) or the API has changed a little bit more than we thought, because the line 240 is where the number of devices per essid is accounted. I will look more carefully at the code and will ask you to do a few more tests.

Ah, I missed a login error that was a little bit above this in my logs, hence missing it - I know the login is correct as it hasnā€™t changed but perhaps I need to do something with the new OS.

Iā€™ll take a look into this and report back.

facing exactly the same issue and was wndering why nobody sppoke about it like port change and api call changesā€¦ well fortunatley there are people out there :slight_smile: I have a unifi controller running on the UDM basic Dream Machine. Hoope we will fix this as this sensor was just awesome :slight_smile:

hey,

I may have a solution, but need someone to test. I made a simple script from the code of the sensor, but itā€™s made to run ā€œstand aloneā€. Edit the variables USERNAME and PASSWORD. If you have the new controller (of course you do :-), change the UDM to ā€œTrueā€. If everything went as expected it should return a big json with all data from the controller (clients, aps, etc).

Ahā€¦ do NOT add the ā€œproxy/networkā€ in the baseurl.

And where the put this? as Iā€™m intressted to get a bit more info out of my USG

oh sorry, a little python knowledge is required. From a shell prompt:

$ python3 -mvenv unifi
$ source unifi/bin/activate
$ pip install requests

and then you can run it with ā€œpython3 unifi.pyā€.

thank you, similair topic, only focus on APā€™s: Monitoring your Unifi AP - Share your Projects! - Home Assistant Community (home-assistant.io)

running on a docker, with each update this will be gone I expectā€¦ looking for something more permanent but interesting topics to follow, also looking in what is available with snmp to make something usefull,

Apologies as I completely missed your last couple of messages :man_facepalming:

Iā€™ve tried to test your script but get no output whatsoever? Iā€™ve changed the URL, username and password to the correct values and also changed UDM to True but nothing is output to the console :confounded: I also changed UDM to False to see any errors but I get nothing there :-1:

I have managed to bastardise what youā€™ve written into my sensor.py and Iā€™m no longer getting login errors :partying_face: but I am now getting these errors: (All log aspects related to the component included for completeness)

2021-02-23 10:48:34 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for my_unifi which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant.
2021-02-23 10:48:34 INFO (MainThread) [homeassistant.loader] Loaded my_unifi from custom_components.my_unifi
2021-02-23 10:48:42 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.my_unifi
2021-02-23 10:48:43 ERROR (MainThread) [homeassistant.components.sensor] my_unifi: Error on device update!
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 358, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 466, in async_device_update
    await task
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/my_unifi/sensor.py", line 107, in update
    self._data.update()
  File "/config/custom_components/my_unifi/sensor.py", line 239, in update
    device_list = (self.list_clients())
  File "/config/custom_components/my_unifi/sensor.py", line 198, in list_clients
    data = r.json()['data']
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/simplejson/__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

Does this help at all?

hmā€¦ no, it didntā€¦ I need to figure out how to test this, as I dont have a UDM. I will update the custom integration and put a bit of logging inside so it will be easier to test (without the external script).

1 Like

O also have idm and would like to assist here to debug .same issue . What can I do to help ?

I will happily be your guinea pig - If youā€™d like to make it easier to track I can raise an issue on GitHub for it? :+1:

Hello,
Iā€™ve copied my_unifi, and created sensor, but it shows nothing:
image
In logs Iā€™ve got only: ā€œYou are using a custom integration for my_unifi which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant.ā€
This is my configuration:

- platform: my_unifi
  name: my_unifi
  username: user
  password: password
  url: https://192.168.1.5:8443

Iā€™m using home assistant core 2021.2.3.

@Woj

  1. try to use quotes around url and password values. ex: ā€œhttps://192.168.1.5:8443ā€.
  2. which device/controler do you have? Currently, the UDM doesnt work.

@clyra
Iā€™ve tried using " and ā€™ without success. Iā€™m using unifi controler 6.0.43.0 installed on VM on Unraid.

did you changed the default ā€œsiteā€ name?

Yes, and now Iā€™ve added this name. My current config:

- platform: my_unifi
  name: my_unifi
  region: 'Home'
  username: user
  password: password
  url: https://192.168.1.5:8443

and in logs there are:

Unifi: Invalid login, or login has expired

&

my_unifi: Error on device update!
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 358, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 466, in async_device_update
    await task
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/my_unifi/sensor.py", line 107, in update
    self._data.update()
  File "/config/custom_components/my_unifi/sensor.py", line 240, in update
    for device in device_list:
TypeError: 'NoneType' object is not iterable

Open the controller on your browser and look at the url:
https://192.168.1.5:8443/manage/site/default/dashboard

The site will be the element after ā€œsiteā€, in the examplo above, is default and you will not need to se ā€œregionā€. Also, enclose user and password in single quotes ā€™ ā€™

It is working without region - when the site is default. Thanks.

hey guys,

I decided to rewrite my unifi sensor, but this time using the unificontrol library, so hopefully it will work with various unifi versions as soon as the library support it (thereā€™s work done and should be merged in the stable version). So hold on for a few weeks :-).

2 Likes