UPDATED: Automating Unifi WiFi SSID password changes and QR code generation

Hello,
after many days, I’m still not able to solve the return code 2 error.
i stumbled upon this GitHub - rootnegativ1/unifi-wifi: Home Assistant integration to change wireless passwords on UniFi Network controllers and seems to work really well. If anyone else wanna give it a try.

You read the whole thread?

So let me see if I have this straight - you’re asking me why I didn’t use the built-in integration that didn’t exist when I wrote this code, and was added because of the popularity of what I developed?

I don’t even know how to answer that.

yeah i did read the whole thread. before i posted. and again just in case after you replied.

and no, that’s not what i’m asking. I’m asking you if there’s a reason you aren’t using that built-in integration now that it exists. Because, i noticed you had updated your code as various comments came in through the thread and as you found ways to make it better. This part you didn’t update so I was wondering if you had a specific reason you preferred this and wanted to understand why it might be advantageous. That’s why i said i noticed you didn’t “update” your code to reflect it.

up to you if you want to answer the question, although i don’t get the passive aggression here. either way i appreciate your hard work on this it is great.

2 Likes

Ah. My apologies, I literally didn’t understand what you were asking. Not passive-aggressive, just extremely confused. LOL

Having said that, maybe you did read the entire thread, but I’m thinking you didn’t pay close enough attention to the dates.

My last update to the code was a full year before the integration was released, so… I guess now I find myself wondering why I would update my code to include code that is already part of an integration that can just easily be added without going through all the manual shenanigans I went through to make it work originally?

So yeah, I guess I’m still a bit confused by the question. LOL

If you want to use the integration, by all means - please do. In fact, I encourage it at this point. My code worked, but it was a bit “dirty”, since I’m not a full-time programmer like a lot of these guys are. I know enough to get by, but my formal education was a quarter century ago, and I ultimately went into the systems side of things, so I’ve only ever coded out of necessity, which is what this was.

The integration is much cleaner, and I presume will remain supported for some time to come. If it meets your needs, then I would absolutely go that route.

I continue to be humbled by the fact that my dirty old code written in my basement home office was the genesis for this feature addition to the official integration, but I’m a firm believer in “if it ain’t broke, don’t fix it”. It still works just fine in my environment, so… :wink:

Thank you for the kind words about my hard work, BTW. I definitely appreciate it.

1 Like

I should also add some password re-generation, I feel like that is the missing piece to offer a good guest experience :slight_smile:

I have iterated many times over the code to where it is now, I am only a coder by night and it’s been a long time since I worked as it. So don’t feel intimidated. It is really fun to work with home assistant.

Oh! I thought that had been added since it was a big part of what I wrote. Oh well, that’ll teach me to not test out the new stuff before recommending it as a full replacement! LOL

And yes, coding is fun, I agree - my issue is that once I get it working, I usually stop there. It could be ugly, inefficient, etc - but if it works, I’m happy. But that’s the difference between developing for yourself vs developing for someone else. :slight_smile:

For me its 50/50, I’m learning and having fun and at the same time users benefit and feedback.

got it. thanks for the info i’ll mess around with it and see how it goes! your code is still essential for the automating of changing the guest password so it’s much appreciated.

@Robban is there a way to access the png(or jpg) of the qr from the integration so it can be displayed however we want?

1 Like

I can’t get this custom component to work. I get error in log and don’t know how to find out what is wrong.

Logger: custom_components.unifi_wifi.coordinator
Source: helpers/update_coordinator.py:353
Integration: Unifi Wifi (documentation, issues)
First occurred: 10:52:00 (1 occurrences)
Last logged: 10:52:00

Authentication failed while fetching klubben319 data: {'meta': {'rc': 'error', 'msg': 'api.err.LoginRequired'}, 'data': []}

Posted here as well:

No idea what that is, but it’s not related at all to the topic of this thread.

Please do. I was digging through your integration for this kind of functionality when I came upon this.

EDIT: I did more reading, found out that the Home Assistant user for Unifi must have admin privileges to enable the QR code - not set as view only. That was the modification I had to make to my system to get it to show up.


Can anyone point me in the direction of how to implement this in the Unifi network integration? I can’t seem to find and documentation that really covers implementing the QR code, nor form looking around through the integration either.

If you mean the QR Code for the Wifi Password it’s a disabled image entity, which you first need to enable in the Unifi Integration menu.

Has anybody figured out an easy way to get the password from that QR Code and display it in a helper entity?

What i want to achive is a Wifi Guest Card on my Dashboard displaying the QR Code (-> image entity) + in clear text the wifi password if the QR Code is not working.

Currently i’m doing this with a shell_command, which is triggered after the last Guest is disconnecting and the regenerate password flow is initiated.
But i don’t know if there is an easier approach

shell_command:

#!/bin/sh
TOKEN=
SAVEDIR=/config/www/pictures/camera
FILENAME=guest-qr.jpg
PW=guest-pw.txt


# GET QR Code as JPG
curl -X GET https://hass.local/api/image_proxy/image.qr_code -H "Authorization: Bearer $TOKEN" --output $SAVEDIR/$FILENAME
sleep 2
# Decode QR Code and extract PW
zbarimg $SAVEDIR/$FILENAME | awk -F ":" '{print $6}' | sed 's/;//g' >> $SAVEDIR/$PW
WIFIPW=$(cat $SAVEDIR/$PW)

generate_post_data()
{
        cat <<EOF
{
        "entity_id": "input_text.guest_password",
        "value": "$WIFIPW"
}
EOF
}
# POST to push PW to HASS Service
curl -X POST https://hass.local/api/services/input_text/set_value -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d "$(generate_post_data)"

# CleanUp
rm -rf $SAVEDIR/$FILENAME
rm -rf $SAVEDIR/$PW

Automation:

alias: Refresh Wifi Guest Password
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.gast
    for:
      hours: 0
      minutes: 15
      seconds: 0
    below: 1
condition: []
action:
  - service: button.press
    target:
      entity_id: button.gast_regenerate_password
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: shell_command.guest_qr_decode
    data: {}
mode: single

@Robban , Great work with using the attributes. Always amazes me how people can figure out how to get these templates to work. Its like magic to me sometimes.

I have a question about your entity_picture. Is that a helper defined elsewhere of the qr code already displayed in dashboard view or something like that? I assume that including this would make it publicly available to anyone with the link?? If I am misunderstanding here please tell me.

The QR code is generated in memory and only available through home assistant AFAIK

It is relatively easy to get a screenshot of the QR code using automations. Then I store the SS as .png in /config/www.

From there I also use the QR integration to decode the .png and get the SSID and the password. Then I get those from ESPhome for printing on e-paper.

However, I was wondering…

  1. Since creating a WiFi QR is almost equivalent to writing the WiFi password in plaintext, and since decoding a QR is so easy, is it worth it to make the password a 20-char long string?

  2. Would it not make sense to shorten the password to just 8 or so chars, and expose the guest password as an entity that follows the same enabling behavior as the QR code?

The way I see it, the guest password is there just to prevent a total rando from joining the network while it is enabled. It is not there to provide real access security.

Moreover, if my guest has issues reading the code, typing a 20-char string is a pain, so we’ll end up logging into Unifi, copying the password, and sending it to the guest via WhatsApp or something.

If the password was easily available and it was short, I can just write it next to the QR code on my dashboard/e-paper screen and the guest won’t need tech support if they can’t read the QR for some reason.

Win-win for everyone.

You do you, but I for one will never do any of the things you just suggested. In fact, given what you just wrote, I question the need for you to even have a QR code at all.

We had a PR for that before but decided it would be too sensitive so stated with only the QR Code even though its a fine line in difference

Thanks a lot for taking the time to respond the way you did, Robban.

I’m well aware of the tension and understand why you made that decision.

1 Like