kvj / hass_nuki_ng

Better support for Nuki devices in the Home Assistant
MIT License
176 stars 33 forks source link

Cannot configure the Web Api mode (no bridge) #69

Closed jmcollin78 closed 1 year ago

jmcollin78 commented 2 years ago

Hello,

I've got a Smart Lock V3.0 PRO which is working normally. I wanted to add it into home assistant. After adding the integration, create an Web Api Token in Nuki web with success: Capture d’écran 2022-06-25 à 00 25 38

Then I'm stuck in the configuration window of the integration: Capture d’écran 2022-06-25 à 00 24 32

The error message "User Input malformed..." seems indicating the URL or hostname of the bridge is mandatory, but I don't have any bridge and I want only to use the Cloud mode.

Any clues on what I'm doing wrong ?

alexdelprete commented 2 years ago

No, you're missing the first field: IP address of the bridge

jmcollin78 commented 2 years ago

I don't have any bridge as explained

jmcollin78 commented 2 years ago

It seems that the Hostname or IP address of the host is mandatory. I put localhost on this field (and I don't have any bridge) and it seems to work.

alexdelprete commented 2 years ago

The bridge address should not be mandatory if you configure the web token, that's how the component goes into web polling mode.

Are you using the privacy mode of the browser to configure things? Please don't use that, it should work without these workarounds.

jmcollin78 commented 2 years ago

Yes I was in privacy mode. Ok I will try in normal mode.

alexdelprete commented 2 years ago

Here you can see this user configured it in web polling mode: https://github.com/kvj/hass_nuki_ng/issues/67#issuecomment-1165952635

alexdelprete commented 2 years ago

But it seems he had the same issue as you...so probably it's a bug. Hope @kvj will track it down.

jmcollin78 commented 2 years ago

Huumm, putting localhost on "hostname or IP" is certainly not a good idea. My log are full of this error:

2022-06-25 17:49:58 ERROR (MainThread) [custom_components.nuki_ng.nuki] Error while fetching auth:
Traceback (most recent call last):
  File "/config/custom_components/nuki_ng/nuki.py", line 356, in _update
    item["web_auth"] = await self.api.web_list_all_auths(dev_id)
  File "/config/custom_components/nuki_ng/nuki.py", line 188, in web_list_all_auths
    response = await self.web_async_json(
  File "/config/custom_components/nuki_ng/nuki.py", line 176, in web_async_json
    return await self.async_json(
  File "/config/custom_components/nuki_ng/nuki.py", line 38, in async_json
    raise ConnectionError(f"Http response: {response.status_code}")
ConnectionError: Http response: 401

as the issue described here: https://github.com/kvj/hass_nuki_ng/issues/21

Note that the integration works perfectly

alexdelprete commented 2 years ago

Can you do one last try: put None in that field. The problem is that it should be an optional field, and it seems like it's mandatory. Try with None to see if we can bypass it safely for now.

kvj commented 2 years ago

The address field is optional. The error message about the malformed input isn't coming from the integration, but from the Home Assistant. Let me try to do a little trick.

Generally speaking, to configure Nuki 3.0 without a bridge you only need a web token and nothing else

kvj commented 2 years ago

https://github.com/kvj/hass_nuki_ng/releases/tag/0.3.5

alexdelprete commented 2 years ago

The address field is optional. The error message about the malformed input isn't coming from the integration, but from the Home Assistant. Let me try to do a little trick.

I know it was optional, I looked at the code. Nevertheless, without it, it threw the error.

So instead of returning None you used "" and it fixes it? It was an HA issue?

kvj commented 2 years ago

So instead of returning None you used "" and it fixes it? It was an HA issue?

Yeah, for some reason config flow expects right data types as initial values or nothing

jmcollin78 commented 2 years ago

Thank you for your fix. Do I have to wait for an official HA release or is it possible to install the fix alone ? Sorry the update is visible in HACS. I will try this now and let you know

jmcollin78 commented 2 years ago

It is working as expected now. Thank you for your fix speed !

jmcollin78 commented 2 years ago

Hello, one more precision. I look into my logs this morning and I see that I've got many errors like this one (one per 30/sec):

2022-07-29 07:45:08 ERROR (MainThread) [custom_components.nuki_ng.nuki] Error while fetching auth:
Traceback (most recent call last):
  File "/config/custom_components/nuki_ng/nuki.py", line 356, in _update
    item["web_auth"] = await self.api.web_list_all_auths(dev_id)
  File "/config/custom_components/nuki_ng/nuki.py", line 188, in web_list_all_auths
    response = await self.web_async_json(
  File "/config/custom_components/nuki_ng/nuki.py", line 176, in web_async_json
    return await self.async_json(
  File "/config/custom_components/nuki_ng/nuki.py", line 38, in async_json
    raise ConnectionError(f"Http response: {response.status_code}")
ConnectionError: Http response: 401

As far as I can see, I've got this error since a reinstall Nuki integration with the fix described above and leave the bridge field empty. This is the same error as when I type "localhost" in the bridge field (because It was mandatory). Do I need to reopen the case ? I guess yes.

jmcollin78 commented 2 years ago

Hello, my Nuki device configuration seems good (found in core.config_entries):

      {
        "entry_id": "54a8d17e2xxxx2255b",
        "version": 1,
        "domain": "nuki_ng",
        "title": "Verrou porte entrée",
        "data": {
          "hass_url": "http://xxx.xxx.xxx.xxx:8123",
          "web_token": "x",
          "name": "Verrou porte entrée",
          "update_seconds": 30,
          "address": "",
          "token": "",
          "use_hashed": false
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": null,
        "disabled_by": null
      },

This log every 30 sec is very annoying as it makes many writes on the filesystem which is not good for SSD memory card.

jmcollin78 commented 2 years ago

For those who can be interested here in a configuration to put into your configuration.yaml to avoid the log error:

logger:
  default: info
  filters:
    custom_components.nuki_ng.nuki:

     - 'Error while fetching auth'
kvj commented 2 years ago

Hey,

it seems your Web token is invalid, and this error has nothing to do with the Bridge configuration

jmcollin78 commented 2 years ago

Hi @kvj , thank you for your reply. I guess my token is valid because I can succesfully command my Nuki with Home Assistant. Nevertheless, I wanted to generate a new token but I didn't find into the documentation how to set a new token. Any help would be appreciated

jmcollin78 commented 2 years ago

May be it is possible to change the token directly in the file core.config_entries. I find something like this:

{
        "entry_id": "54a8d1xxxxxxx2255b",
        "version": 1,
        "domain": "nuki_ng",
        "title": "Verrou porte entrée",
        "data": {
          "hass_url": "http://<myip>:8123",
          "web_token": "9de787xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx37b7ab7",
          "name": "Verrou porte entrée",
          "update_seconds": 30,
          "address": "",
          "token": "",
          "use_hashed": false
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": null,
        "disabled_by": null
      },

If it is not dangerous I will try to update directly this file and then restart Home Assistant

jmcollin78 commented 1 year ago

I create a new token, put the new value in the web_token attribute and restart. I still have the issue:

2022-08-31 08:46:59.377 WARNING (MainThread) [custom_components.nuki_ng.nuki] Despite being configured, Web API request has failed
2022-08-31 08:46:59.381 ERROR (MainThread) [custom_components.nuki_ng.nuki] Error while fetching auth:
Traceback (most recent call last):
  File "/config/custom_components/nuki_ng/nuki.py", line 415, in _update
    item["web_auth"] = await self.api.web_list_all_auths(web_id)
  File "/config/custom_components/nuki_ng/nuki.py", line 215, in web_list_all_auths
    response = await self.web_async_json(
  File "/config/custom_components/nuki_ng/nuki.py", line 184, in web_async_json
    return await self.async_json(
  File "/config/custom_components/nuki_ng/nuki.py", line 39, in async_json
    raise ConnectionError(f"Http response for {response.request.url}: {response.status_code} {response.reason}")
ConnectionError: Http response for https://api.nuki.io/smartlock/292865238500/auth: 401 Unauthorized

Can someone post the Nuli entry in the core.config_entries please to compare. Thanks in advance

kvj commented 1 year ago

@jmcollin78 why not to delete the integration and add it again? You won't even loose the entity history nor automations (unless you will give a new name)

jmcollin78 commented 1 year ago

I already do that in june with the first problem I have with the bridge URL. I can redo that once more. (this week-end only). What I think is very curious is that it works normally. I can command the Lock, have the status,...

kvj commented 1 year ago

What I think is very curious is that it works normally

Well, as only one web API command fails, I suspect that your Web token doesn't have the permission (scope) to read and control authorization entries

jmcollin78 commented 1 year ago

This is interesting. I don't set all permissions to the token indeed: Capture d’écran 2022-08-31 à 09 19 10

That for security reason but I will try with all permissions.

jmcollin78 commented 1 year ago

With all permissions the error have disappear. Do you know if there is a minimal configuration of permissions ? Are they all required ?

jmcollin78 commented 1 year ago

Reinstalling the integration have solve the issue.