tomasbedrich / home-assistant-hikconnect

A Home Assistant integration to communicate with Hikvision smart doorbells via Hik-Connect cloud.
42 stars 10 forks source link

Can't login when user is located in area non-matching his locale #16

Closed jjmuriel closed 2 years ago

jjmuriel commented 2 years ago

Hi, I'm typing my hik-connect username and password info but integration keeps rejecting me. The info works at hik-connect site.

Any idea what I doing wrong?

Version core-2021.12.9
Installation Type Home Assistant Supervised
Development false
Supervisor true
Docker true
User root
Virtual Environment false
Python Version 3.9.7
Operating System Family Linux
Operating System Version 5.10.0-10-amd64
CPU Architecture x86_64

`This error originated from a custom integration.

Logger: custom_components.hikconnect.config_flow Source: custom_components/hikconnect/config_flow.py:26 Integration: Hik-Connect (documentation, issues) First occurred: 10:47:17 (2 occurrences) Last logged: 10:53:23

Hik-Connect login failed Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/hikconnect/api.py", line 75, in login session_id = res_json["loginSession"]["sessionId"] KeyError: 'loginSession'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/config/custom_components/hikconnect/config_flow.py", line 39, in async_step_user await validate_input(self.hass, user_input) File "/config/custom_components/hikconnect/config_flow.py", line 26, in validate_input await api.login(data["username"], data["password"]) File "/usr/local/lib/python3.9/site-packages/hikconnect/api.py", line 77, in login raise LoginError("Unable to parse session_id from response.") from e hikconnect.exceptions.LoginError: Unable to parse session_id from response. `

2022-01-12 10:47:17 DEBUG (MainThread) [hikconnect.api] Got login response '{'meta': {'code': 1100, 'message': '客户端需要重定向用户请求域名(海外)', 'moreInfo': None}, 'loginArea': {'apiDomain': 'apiius.hik-connect.com', 'webDomain': 'ius.hik-connect.com', 'areaName': 'USA', 'areaId': 314}}' 2022-01-12 10:47:17 ERROR (MainThread) [custom_components.hikconnect.config_flow] Hik-Connect login failed Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/hikconnect/api.py", line 75, in login session_id = res_json["loginSession"]["sessionId"] KeyError: 'loginSession' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/config/custom_components/hikconnect/config_flow.py", line 39, in async_step_user await validate_input(self.hass, user_input) File "/config/custom_components/hikconnect/config_flow.py", line 26, in validate_input await api.login(data["username"], data["password"]) File "/usr/local/lib/python3.9/site-packages/hikconnect/api.py", line 77, in login raise LoginError("Unable to parse session_id from response.") from e hikconnect.exceptions.LoginError: Unable to parse session_id from response.

tomasbedrich commented 2 years ago

Hi, can you please follow the debugging guide so that I know what is the issue?

https://github.com/tomasbedrich/home-assistant-hikconnect/blob/main/DEBUGGING.md

In your case, please ensure you perform the login attempt to have it captured in the logs.

jjmuriel commented 2 years ago

Hi, I followed this guide and sent you the logs to your email. Thanks.

tomasbedrich commented 2 years ago

It seems strange to me it tries to connect to US API, while you are probably located in Spain, right? Do you have any possible explanation, like VPN, DNS blocking, or..?

tomasbedrich commented 2 years ago

Moreover aren't there more logs to send? Maybe in this case it also helps if you send me your whole log file + config file, so that I can explain why it connects to US servers.

jjmuriel commented 2 years ago

I'm located in South America and the Hik-Connect App was configurated with USA account. I don't use VPN. I'll send you my whole log and config file, but I have some configurations splited in several YAML files. Maybe, I have to change the account to my country? I'll try and post the result.

jjmuriel commented 2 years ago

If I change the account to my country, I can't add my Hikvision Doorbell in the Hik-Connect App. Instead, with the USA account it works. So, I have to use USA account (no VPN).

tomasbedrich commented 2 years ago

Hm, could you please do nslookup api.hik-connect.com from your computer and paste the result here?

jjmuriel commented 2 years ago

This is the output:

Server:     192.168.111.5
Address:    192.168.111.5#53

Non-authoritative answer:
api.hik-connect.com canonical name = apiisa.hik-connect.com.
apiisa.hik-connect.com  canonical name = apisa-hik-connect-1930596709.sa-east-1.elb.amazonaws.com.
Name:   apisa-hik-connect-1930596709.sa-east-1.elb.amazonaws.com
Address: 54.233.110.148
Name:   apisa-hik-connect-1930596709.sa-east-1.elb.amazonaws.com
Address: 18.228.49.130
tomasbedrich commented 2 years ago

Ok, then it seems clear.

We have to redirect user to API domain returned in login request, if Hikconnect service provides such hint.

Thanks for your bug report and cooperation.

jjmuriel commented 2 years ago

Thank you @tomasbedrich .

tomasbedrich commented 2 years ago

Please try version number 1.1.1 and let me know.

jjmuriel commented 2 years ago

Hi @tomasbedrich, Now I can login, but now:

Screen Shot 2022-01-19 at 7 54 08
This error originated from a custom integration.

Logger: custom_components.hikconnect
Source: custom_components/hikconnect/__init__.py:30
Integration: Hik-Connect (documentation, issues)
First occurred: 07:49:14 (4 occurrences)
Last logged: 07:49:51

Unexpected error fetching hikconnect data: unsupported operand type(s) for -: 'NoneType' and 'datetime.datetime'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 187, in _async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 147, in _async_update_data
    return await self.update_method()
  File "/config/custom_components/hikconnect/__init__.py", line 41, in async_update
    await relogin_if_needed()
  File "/config/custom_components/hikconnect/__init__.py", line 30, in relogin_if_needed
    needed = api.is_refresh_login_needed()
  File "/usr/local/lib/python3.9/site-packages/hikconnect/api.py", line 130, in is_refresh_login_needed
    return (self.login_valid_until - datetime.datetime.now()) < datetime.timedelta(
TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.datetime'
tomasbedrich commented 2 years ago

Could you please provide logs again to my e-mail?

jjmuriel commented 2 years ago

Sure, I just sent you the logs.

tomasbedrich commented 2 years ago

Can you please try version 1.1.2?

jjmuriel commented 2 years ago

I did it, now there is not error, but I have 2 devices with the same name and no entities: image

Device no. 1 image

Device no. 2 image

I have this video doorbell: https://us.hikvision.com/en/products/more-products/video-intercom/door-station/3-mp-outdoor-wi-fi-smart-doorbell-camera-ds-hd1 I sent you the log file again. Thank you @tomasbedrich .

tomasbedrich commented 2 years ago

Okay, I'm glad that you are able to log in, so this bug is fixed.

The issue what you mention is #15. This is expected behavior, because currently the integrations supports locks only. Unfortunately, given your setup, the integration is not going to be useful for you (at least until something else than locks is implemented).

jjmuriel commented 2 years ago

I understand, thank you.