Unifi sensor custom component

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

Great news …thanks a lot mate

In case you weren’t factoring it in to your rewrite, I just spotted this in my logs and wanted to let you know…
No ‘version’ key in the manifest file for custom integration ‘my_unifi’. This will not be allowed in a future version of Home Assistant. Please report this to the maintainer of 'my_unifi’
First part of the Breaking Changes for 2021.3 :+1: