core icon indicating copy to clipboard operation
core copied to clipboard

Rest sensor - digest issue with the new httpx

Open mateuszdraco opened this issue 2 years ago • 27 comments

The problem

I've spent a lot of time trying to debug why my integration with Hikvision intercom stopped working some time ago. What I found out is that httpx with its new version introduced a change which altered how digest auth works and eventually led to my integration being useless.

I raised this issue with httpx via their discussions, I hope they can fix it soon. My request is to downgrade to 0.23.0 or upgrade to the new version if it's going to appear soon.

What version of Home Assistant Core has the issue?

2023.1.2

What was the last working version of Home Assistant Core?

2023.1.2

What type of installation are you running?

Home Assistant Container

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

mateuszdraco avatar Jan 14 '23 18:01 mateuszdraco

I have exactly the same problem with My Hikvision Intercom devices and version 2023.1.7

Joao-Sousa-71 avatar Jan 29 '23 19:01 Joao-Sousa-71

Same problem in 2023.2.2

psike01 avatar Feb 06 '23 05:02 psike01

So according to my discussion with httpx contributors (big thanks for the support) it seems HomeAssistant should change how it uses this library with digest. It should create httpx.DigestAuth for each call, instead of keeping its single instance.

mateuszdraco avatar Feb 06 '23 07:02 mateuszdraco

I believe I have similar issues hence posting my setup to add information:

rest:
  - authentication: digest
    username: user
    password: [secret]
    scan_interval: 1
    resource: http://192.168.X.X/ISAPI/VideoIntercom/callStatus?format=json
    sensor:
      - name: Doorbell Status
        value_template: "{{ value_json.CallStatus.status }}"

This results in the following log entry (every second):

Logger: homeassistant.helpers.template
Source: helpers/template.py:624
First occurred: 08:18:26 (475 occurrences)
Last logged: 08:26:40

Template variable error: 'value_json' is undefined when rendering '{{ value_json.CallStatus.status }}'

Turning on debug level logging shows the following:

2023-02-08 08:51:07.395 DEBUG (MainThread) [homeassistant.components.rest] Finished fetching rest data data in 0.111 seconds (success: True)
2023-02-08 08:51:07.395 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: 
2023-02-08 08:51:07.396 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.CallStatus.status }}'

Prior to the upgrade yesterday everything was working fine. I assume value_json is undefined as the call is no longer authorized, but this does not show in the log. For now, a rollback to my previous setup (2022.12) is the only option I have as my doorbell is no longer functioning :). I hope a fix can be provided in upcoming releases.

Xirt avatar Feb 08 '23 07:02 Xirt

You can always use my addon meanwhile to capture events from Hikvision :-)

pergolafabio avatar Feb 08 '23 07:02 pergolafabio

I believe that utilizes options for installation I do not have in my container version of HA? If a manual installation option is available, I could give it a try :). Still I believe above issue must be resolved as it might hamper more than just HikVision setups.

Xirt avatar Feb 08 '23 08:02 Xirt

My workaround was to ssh on the machine which hosts HA (in my case it's a vm), uninstall the new httpx and manually install the older version. Everything works, yet I'm not updating HA since then :(

mateuszdraco avatar Feb 08 '23 08:02 mateuszdraco

indeed, but there is also a mqtt version of it

pergolafabio avatar Feb 08 '23 08:02 pergolafabio

My workaround was to ssh on the machine which hosts HA (in my case it's a vm), uninstall the new httpx and manually install the older version. Everything works, yet I'm not updating HA since then :(

I tried the same using the following commands in the shell:

pip uninstall httpx
pip install httpx==0.23.0

After a restart everything works again. Let's hope this "fix" is only needed temporarily.

Xirt avatar Feb 08 '23 09:02 Xirt

Confirmed still broken in 2023.3.1, but above workaround (downgrade) appears to still work.

Xirt avatar Mar 07 '23 07:03 Xirt

Still not working in version 2023.3.5

markus2504 avatar Mar 18 '23 13:03 markus2504

A quick workaround is using the command line sensor instead of the rest sensor

pergolafabio avatar Mar 18 '23 15:03 pergolafabio

Or use my addon, it now supports MQTT and can run as standalone docker :-)

pergolafabio avatar Mar 18 '23 15:03 pergolafabio

I have DS-KB8113-IME1 and the MQTT addon is not stable. How to use command line sensor? Unfortunately, I don't know English.

markus2504 avatar Mar 18 '23 17:03 markus2504

Not stable? Offcourse it's stable, never had any issues with it, all users are very happy.. not had a single complain... What's your issue then , you can open an issue if you want?

Anyway, for shell, look here https://www.home-assistant.io/integrations/sensor.command_line/

pergolafabio avatar Mar 18 '23 17:03 pergolafabio

Or use my addon, it now supports MQTT and can run as standalone docker :-)

would love to, but I run HA Core and as far as I know, that doesn't support add-ons. Are you releasing a standalone version soon?

asdfkid avatar Mar 19 '23 04:03 asdfkid

So according to my discussion with httpx contributors (big thanks for the support) it seems HomeAssistant should change how it uses this library with digest. It should create httpx.DigestAuth for each call, instead of keeping its single instance.

so glad I found this discsusion. I was tearing my hair out trying to figure out why my rest sensor was broken. Interestingly, I also found the following:

  • when HA rest sensor was enabled, the API was giving 401 errors from manual command line curl entries as well
  • if I disabled the rest sensor, after about 20 mins the curl would work (auth lockout timeout?)
  • If I logged into the web interface of my DS-KV6113 that would reset the API lockount instantly (didn't have to wait the 20 mins)

asdfkid avatar Mar 19 '23 04:03 asdfkid

Or use my addon, it now supports MQTT and can run as standalone docker :-)

would love to, but I run HA Core and as far as I know, that doesn't support add-ons. Are you releasing a standalone version soon?

Hi , standalone docker is possible, have a look at the dev branch for the documentation how to run it standalone , it's using MQTT now

pergolafabio avatar Mar 19 '23 06:03 pergolafabio

@pergolafabio I just tried your addon and it looks just awesome! Massive thanks for that! One question: can I detect if the intercom is unavailable?

mateuszdraco avatar Mar 19 '23 11:03 mateuszdraco

You are using the beta, right?

Hmm, when connected off network you mean? Hmm not sure, didn't tested ;-)

pergolafabio avatar Mar 19 '23 11:03 pergolafabio

No, I'm using regular version (I thought beta could be unstable). I can try beta, although I have a couple of places where this is plugged so changing it will take a bit of time ;)

And yes, I have my intercom connected via wifi and sometimes it looses the connectivity. I already replaced my access point and since then it's much better, yet, I'm kind of stressed about it ;)

mateuszdraco avatar Mar 19 '23 12:03 mateuszdraco

Use beta, will be released soon as official, it's much more stable now already, lots of more functions and used mqtt

pergolafabio avatar Mar 19 '23 12:03 pergolafabio

It should detect if it's unable to connect

pergolafabio avatar Mar 19 '23 12:03 pergolafabio

Can I build an automation on top of this? Previously when I didn't have a connectivity I sent myself a phone notification to kick the system manually.

mateuszdraco avatar Mar 19 '23 12:03 mateuszdraco

I put a smart plug at the socket that my Hikvision uses for power and HA automatically reboots (toggle plug twice with a small delay) in case Hikvision is unavailable for a longer time. Which is - despite the very strong network nearby - quite frequently...

Xirt avatar Mar 19 '23 12:03 Xirt

I never tested it, I have a reboot instruction implemented , bit offcourse , it needs network then... Maybe the callsensors will give unavailable, I didn't test it

pergolafabio avatar Mar 19 '23 12:03 pergolafabio

@Xirt yeah, you work the system quite well, in my case after AP change it improved a lot.

mateuszdraco avatar Mar 19 '23 13:03 mateuszdraco

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.