pergolafabio / Hikvision-Addons

Home Assistant: Hikvision Doorbell
179 stars 41 forks source link

DS-K1T501SF door lock not exposed #117

Closed eumanecs closed 1 year ago

eumanecs commented 1 year ago

What happened?

I have AccessControl Terminal DS-K1T501SF dos not support ISAPI, looks like it dos not exposes to plugin door lock expose the locks in the payload.

I have been using fiddler i has able to capter actual POST POST https://apiieu.hik-connect.com/api/device/transmit HTTP/1.1 Host: apiieu.hik-connect.com Content-Type: application/x-www-form-urlencoded; charset=utf-8 Connection: keep-alive Accept: application/json User-Agent: HikConnect/20230606 (iPhone; iOS 16.5; Scale/3.00) Accept-Language: en-LV;q=1, ru-LV;q=0.9, lv-LV;q=0.8 Accept-Encoding: gzip, deflate, br Content-Length: 569

When unlocked from Hik-connect cloud in the body of post message there is provided aditinal 6 digit password to operate the unlock action

Add-on

Hikvision Doorbell

What version of the add-on are you running?

2.1 & 3.0.0

Installation type

Home Assistant add-on

Relevant log output

Version 2.1
2023-06-17 05:18:59.847 | DEBUG    | config:loadConfig:31 - Loading config from file
loop[2] find 6 mac and 12 ip
2023-06-17 05:19:00.233 | ERROR    | __main__:<module>:142 - NET_DVR_Login_V30 failed, error code = 153

Verion 3.0.0
2023-06-17 05:17:03.184 | DEBUG    | config:mqtt_config_from_supervisor:36 - Requesting MQTT service configuration to supervisor
2023-06-17 05:17:03.207 | INFO     | sdk.utils:loadSDK:44 - Using OS: Linux with architecture: x86_64
loop[2] find 2 mac and 0 ip
Traceback (most recent call last):
  File "/app/main.py", line 91, in <module>
    asyncio.run(main())
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/app/main.py", line 42, in main
    doorbell.authenticate()
  File "/app/doorbell.py", line 62, in authenticate
    self._type = DeviceType(self._device_info.wDevType)
  File "/usr/local/lib/python3.10/enum.py", line 385, in __call__
    return cls.__new__(cls, value)
  File "/usr/local/lib/python3.10/enum.py", line 710, in __new__
    raise ve_exc
ValueError: 861 is not a valid DeviceType

Relevant configuration

template:
  - sensor:
      - name: hikvision_door
        state: "off"
      - name: hikvision_callstatus
        state: "off"
      - name: hikvision_motion
        state: "off"
      - name: hikvision_tamper
        state: "off"
      - name: hikvision_dismiss
        state: "off"
service: hassio.addon_stdin
data:
  addon: aff2db71_hikvision_doorbell
  input: unlock front_door 1

Anything else?

image

pergolafabio commented 1 year ago

Hi, Hikconnect indeed doesnt use ISAPI, but build on SDK also (i believe)

I have added your device 861 as a valid device type in beta 20, will release it soon...

The beta addon has several methods to open door in case one method fails

pergolafabio commented 1 year ago

beta 20 released, can you try?

eumanecs commented 1 year ago

just reinstaled beta 20 release got log 2023-06-17 11:49:30.771 | DEBUG | config:mqtt_config_from_supervisor:36 - Requesting MQTT service configuration to supervisor 2023-06-17 11:49:30.799 | INFO | sdk.utils:loadSDK:44 - Using OS: Linux with architecture: x86_64 loop[2] find 2 mac and 0 ip 2023-06-17 11:49:31.116 | INFO | doorbell:authenticate:70 - Connected to doorbell: Durvis 2023-06-17 11:49:31.117 | INFO | event:init:76 - Setting up event handler: Console stdout 2023-06-17 11:49:31.117 | INFO | mqtt:init:91 - Setting up event handler: MQTT Traceback (most recent call last): File "/app/main.py", line 91, in asyncio.run(main()) File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete return future.result() File "/app/main.py", line 52, in main mqtt = MQTTHandler(config.mqtt, doorbell_registry) File "/app/mqtt.py", line 132, in init num_doors = doorbell.get_num_outputs() File "/app/doorbell.py", line 228, in get_num_outputs raise RuntimeError("Unable to get the number of doors") RuntimeError: Unable to get the number of doors

pergolafabio commented 1 year ago

Its using indeed ISAPI to get the relays, can you configure them manually as described here:

https://github.com/pergolafabio/Hikvision-Addons/blob/main/doorbell_beta/DOCS.md

With output_relays ...

eumanecs commented 1 year ago

Not shure what i am dooing wrong

my configuration look lik this

Loads default set of integrations. Do not remove.

default_config:

Load frontend themes from the themes folder

frontend: themes: !include_dir_merge_named themes

Text-to-speech

tts:

automation: !include automations.yaml script: !include scripts.yaml scene: !include scenes.yaml

configuration.yaml

service: hassio.addon_stdin data: addon: aff2db71_hikvision_doorbell input: unlock front_door 1

and

image

pergolafabio commented 1 year ago

Hi, the config looks correct, but check your mqtt integration, there should be a device be created, with all sensors and relays, you don't need to use that service

eumanecs commented 1 year ago

image

pergolafabio commented 1 year ago

goto settings , integrations, mqtt, should look like this:

image image

pergolafabio commented 1 year ago

remove that service: part from your configuration, thats incorrect, is not needed

pergolafabio commented 1 year ago

You don't need to touch any yaml file, if you use beta addon, all sensors and services will be created automatically as an mqtt device

eumanecs commented 1 year ago

my UI looks diferent i dont have integrations under settings i have removed service part from my configuration image

eumanecs commented 1 year ago

image

pergolafabio commented 1 year ago

offcourse you have :-) , there you will see the integrations, then click on mqtt

image

eumanecs commented 1 year ago

Supper, but it seams that Hikvisin door bell beta stops working image

eumanecs commented 1 year ago

2023-06-17 13:02:36.230 | DEBUG | config:mqtt_config_from_supervisor:36 - Requesting MQTT service configuration to supervisor 2023-06-17 13:02:36.238 | INFO | sdk.utils:loadSDK:44 - Using OS: Linux with architecture: x86_64 loop[2] find 2 mac and 0 ip 2023-06-17 13:02:36.775 | INFO | doorbell:authenticate:70 - Connected to doorbell: Front door 2023-06-17 13:02:36.775 | INFO | event:init:76 - Setting up event handler: Console stdout 2023-06-17 13:02:36.775 | INFO | mqtt:init:91 - Setting up event handler: MQTT 2023-06-17 13:02:37.056 | ERROR | main::96 - Error while listening to events in Front door: Failed to receive data from the device. Error code:9

eumanecs commented 1 year ago

image

By the way i was able to ssh to my Access Control terminal DS-K1T501SF with root login may be i can do some door unlock comand by som sip comand just for info

pergolafabio commented 1 year ago

I need to have a look later for that error 9 ...

There is also an hikconnect integration, using cloud, maybe you can use that one instead of this addon if you only want to open door?

pergolafabio commented 1 year ago

Hmm, seems error 9 , just means, fail to read data from device, can you try maybe rebooting it?

eumanecs commented 1 year ago

Yes, I want only open door, my ultimate gool is to have some curl command or http request to unlock the door and latter to put that request on home bridge to control the door with iPhone with Siri voice using https://github.com/TheRealSimon42/homebridge-http-lock-ultimate

I was using this source: https://github.com/judge/homebridge-hikconnect but seam it's its build from some revers engineering from your home assistant GitHub code, so it dos not exposes my lock. and unfortunately no support for ISAPI for my device DS-K1T501SF

pergolafabio commented 1 year ago

You can use the hikconnect integration in HA, that should work for you, you will find it in HACS

pergolafabio commented 1 year ago

https://github.com/tomasbedrich/home-assistant-hikconnect

eumanecs commented 1 year ago

I have been in contact with tomasbedrich

his replay was

Thanks for the logs. It seems your device does not expose the locks in the payload, the plugins looks for 'lockNum' property and it's not present in your case. I don't have a device like yours so it's pretty complicated to reverse engineer what happens when you click unlock in the application. If you could do that (listen for web traffic while press the button in the application) I could check if I could do something.

eumanecs commented 1 year ago

i started to play with the fiddler the problem is that cant repaly POST comand as the body part of the post all the time is diferent https://apiieu.hik-connect.com/api/device/transmit HTTP/1.1 Host: apiieu.hik-connect.com Content-Type: application/x-www-form-urlencoded; charset=utf-8 Connection: keep-alive Accept: application/json User-Agent: HikConnect/20230606 (iPhone; iOS 16.5; Scale/3.00) Accept-Language: en-LV;q=1, ru-LV;q=0.9, lv-LV;q=0.8 Accept-Encoding: gzip, deflate, br Content-Length: 569

pergolafabio commented 1 year ago

Are you using android or iphone with fiddler?

pergolafabio commented 1 year ago

when i started before with hikconnect, i have an indoor station, i need to send unlock to that device:

i saw in fiddler: (removed my serial number + token)


PUT /v3/devconfig/v1/call/QXXXXXXXX/1/remote/unlock?srcId=1&lockId=0&userType=0 HTTP/1.1
clientType: 55
osVersion: 5.1.1
clientVersion: 4.14.1.0407
netType: WIFI
clientNo: google_play
appChannel: hikvision
customno: 1000002
featureCode: b547bcdd76b2f2c93e0956114a6f8665
sessionId: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
areaId: 111
lang: en-US
appId: HikConnect
Content-Length: 0
Host: apiieu.hik-connect.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.12.1
pergolafabio commented 1 year ago

yes, body part is indeed differen, you need to send the sessionID , its a token

you can create a sensor in HA, to grab the sessionID , here is an example, you can see it all in fiddler, make sure to change the payload, i have it in secret, its actuallt the login part, just copy/past it from fiddler

  - platform: rest
    name: hikconnect_login
    scan_interval: 7200
    resource: "https://apiieu.hik-connect.com/v3/users/login/v2"
    headers:
      customno: 1000002
      appChannel: "hikvision"
      Content-Type: "application/x-www-form-urlencoded"
      featureCode: "b547bcdd76b2f2c93e0956114a6f8665"
      lang: "en-US"
      clientVersion: "4.14.1.0407"
      appId: "HikConnect"
      sessionId: ""
      clientType: 55
      clientNo: "google_play"
      areaId: 111
      netType: "WIFI"
      osVersion: "5.1.1"   
    method: POST
    payload: !secret hikvision_payload
    value_template: "Hikconnect Login"    
    json_attributes:
    - loginSession 

you can see the payload in the "textview" below , its a long huge string

image

eumanecs commented 1 year ago

Iphone 13 mini ios 16.05 i was able to capture all trafick very well. I was not able to logi in to Hikconect app when proxy was activeted, but thet i log in on hikconect app with my credentials and afterwards enabled proxy on my phone and afterward i am able to cappuure all the trafick

pergolafabio commented 1 year ago

yeah, once you have the sensor, you can build actions/scripts, based on the sessionID

pergolafabio commented 1 year ago

for example;, here is a reject command, as you see i use the sessionID from the sensor:

hikconnect_callsignal_3_reject-: "curl -X PUT --header \"sessionId: {{ state_attr('sensor.hikconnect_login', 'loginSession').sessionId }}\" https://apiieu.hik-connect.com/v3/devconfig/v1/call/XXXXXXX/operation?cmdId=3&handler=e6ldym"

just copy paste the command from fiddler

eumanecs commented 1 year ago

i am able to get my faturecode but then i am stuck with what next, idealy if somone can make some plugin were i put my varialbes like customno and featurecode

pergolafabio commented 1 year ago

see above, i copy pasted everything you need

pergolafabio commented 1 year ago

first you need the sensor, thats the most important one, you need the sessionID token, that token is always needed to send your commands, like open door

eumanecs commented 1 year ago

image

pergolafabio commented 1 year ago

yes, with that session id, you can send commands, but remember, the session is is only valid for some hours , its a temp token thats why i created a rest sensor with scan interval 7200 sec

eumanecs commented 1 year ago

My post looks like this image

eumanecs commented 1 year ago

image

eumanecs commented 1 year ago

There I am stuck. What is sensor my serial device number? What is the header label name in fiddler for sensor? And wat is the payload is it session id value? Is the feature code header label name for the token?

What are the essential header label names from fiddler I should use?

pergolafabio commented 1 year ago

You can all see it in fiddler, see my posts above

eumanecs commented 1 year ago

Ok i will investigate that. bay the way, i have rebooted device if you can take a look when you will have a time error 9 - fail to read data from device on home assitant. toady i will not boter you any more, hav a nice weekend.

pergolafabio commented 1 year ago

Ok, will have a look , have a nice weekend!

eumanecs commented 1 year ago

Hello i have managed to create the python scrip to unlock my door the key values for me was: this info is aquered up on login: token voipToken session_id In order to unlock the door i need to login in to Hikconect app with the username and password go to the Access control device DS-K1T501SF in the app and there is door i need to provide extra 6 digit code to punch in order to unlock the door. if somone needs i can sher the Pyton script with masked privad data. pergolafabio let me know if you need somthing from Fiddler Everywhere i have still 5 days free trieal licence.

pergolafabio commented 1 year ago

Hi, no need for it This addon is not based on Hikconnect, but uses the SDK, maybe you can provide the info thomas, who is the owner for the hikconnect integration? he can benefit from it?

Thnx anyway!! appreciated

pergolafabio commented 1 year ago

btw, if you use fiddler classic, its a free version, no trial then

pergolafabio commented 1 year ago

as for error 9 , not sure i can do anything about it :-( Is it an old device you have? maybe a firmware upgrade?

eumanecs commented 1 year ago

Yes its several years old, I have some custom firmware on it have installed Portugies version of it that had special functionality there were folks that helped to unpack the firmware and access audio files we have replaced and then package it back.

pergolafabio commented 1 year ago

ah, maybe some issue on it to retrieve the events by sdk :-(

eumanecs commented 1 year ago

i dont wan to upgarde there were some scripts repackFirmwareH5.py and unpackFirmwareH5 and unpack.sh repack.sh i was using.

pergolafabio commented 1 year ago

ok, maybe you can ask on community forum if someone added the device in the addon, that way we can rule out the issue wirth firmware maybe? gonna close for now, dont think i can do anything now :-(