user2684 / imou_life

Home Assistant custom component for controlling Imou devices
MIT License
115 stars 18 forks source link

Motion sensor & doorbell button sensor support for Imou doorbells (DB11/DB60/DB61) #35

Open jrb80 opened 1 year ago

jrb80 commented 1 year ago

I am requesting support for the Imou/Dahua doorbell products (DB11/DB60/DB61) to provide motion detection and doorbell button binary sensors. The recent binary sensor enhancement for cameras is not working for doorbells. I think this is because it uses a different event classification mobileDetect whereas cameras are using videoMotion events. Ideally I want to know when someone actually presses the doorbell button, probably more important than the motion detection event. This should be possible through the callBellEvent

Example JSON webhook response for mobileDetect event

  json:
    deviceType: DB
    msgType: mobileDetect
    cname: DB11
    labelType: motionAlarm
    remark: ''
    zerotime: 1669025671
    type: 1030

Example JSON webhook response for callBellEvent

  json:
    deviceType: DB
    msgType: callBellEvent
    cname: DB11
    labelType: callAlarm
    remark: ' '
    zerotime: 1669025675
    type: 1028

Extract from log file

- Device ID: xxxx
Name: Doorbell
Catalog: Doorbell
Model: DB11
Firmware: Eng_N_V1.001.176S000.1.R.20220719
Online: yes
Capabilities:
- Support access to wireless local area network (WLAN)
- Device end loopback RTSP requires authentication (Auth)
- Support Dahua P2P Service (DHP2P)
- Support Huashi WeChat Stream Encryption (HSEncrypt)
- Support for cloud storage of China Vision WeChat platform (CloudStorage)
- Support device local storage, such as SD card or hard disk (LocalStorage)
- The device supports playback based on the file name (PlaybackByFilename)
- Support streaming media forwarding capability (MT)
- The device supports CustomKey (CustomKey) (CK)
- The device has remote debugging capabilities and supports log extraction (RemoteDebug) (RD)
- Support RD capability, support data burying point control, support debugging log upload (RDV2)
- Cloud Upgrade (XUpgrade)
- Device supports time synchronization (TimeSync)
- Support user name password information verification and password modification (ModifyPassword)
- Support device local storage enable switch (LocalStorageEnable)
- Real-time streaming supports private protocol streaming, supports dynamic port modification (RTSV2)
- The playback stream supports private protocol streaming and supports dynamic port modification (PBSV2)
- Voice intercom streaming supports private protocol streaming, supports dynamic port modification (TSV2)
- Support 0x95 extension header encryption (ESV1)
- The device supports daily summer time (DaySummerTime)
- The device supports weekly daylight saving time (WeekSummerTime)
- Support time format setting (TimeFormat)
- Doorbell volume setting (RingAlarmSound)
- Device Reset (DevReset)
- The device supports restarting (Reboot)
- The device supports No encrypt capture (NEC)
- CCD
- Support remote linkage (RemoteControl)
- Support Panorama (Panorama)
- Support screen flip (FrameReverse)
- AECV3
- motion-detect-window supports motion detection window settings (MDW)
- motion-detect-sensitive supports motion detection sensitivity setting (MDS)
- Mobile Detection (Merged with PIR) (MobileDetect)
- Support voice intercom (AudioTalk)
- Push notifications (pushNotifications)
Switches:
- Local storage (localStorageEnable): None {}
- Mobile detection (mobileDetect): None {}
- Push notifications (pushNotifications): None {}
Sensors:
- Storage used (storageUsed): None {}
- Callback url (callbackUrl): None {}
Binary Sensors:
- Online (online): None {}
Select:
Buttons:
- Restart device (restartDevice) {}
- Refresh all data (refreshData) {}
- Refresh alarm (refreshAlarm) {}
2022-11-21 09:43:08.702 DEBUG (MainThread) [custom_components.imou_life] Initialized coordinator. Scan internal 900 seconds
2022-11-21 09:43:08.702 DEBUG (MainThread) [imouapi] [Doorbell] update requested
user2684 commented 1 year ago

Hi, the point is if this event, regardless of the way it is sent to the callback url, is also registered as a message alarm because the current flow is:

Motion detected by device -> message sent to webhook -> automation trigger refresh of HA sensor -> HA sensor pulls the latest message alarm from the API -> if the timestamp is new, motion is detected

To test this, try running something like (preferably outside HA):

pip3 install imouapi
python3 -m imouapi.cli --app-id <your_app_id> --app-secret <your_app_secret> api_getAlarmMessage <device_id>

And check if the alarm is there. Thanks

kkwestt commented 1 year ago

No video in my HA. (I use DB60) in console (pip3 install imouapi) i get hls stream and watch it over VLC.

kkwestt commented 1 year ago

now through api i get access to ports with no ansver: "httpPort": "80", "privatePort": "37777", "rtspPort": "554",

but without lucky :) all i need is RTSP stream for local NVR

user2684 commented 1 year ago

@kkwestt any error in the logs when opening up the stream? Any chance to enable debug logging? @jrb80 is video stream working for you? Thanks

kkwestt commented 1 year ago
Log Details (ERROR)
This error originated from a custom integration.
Logger: custom_components.imou_life
Source: custom_components/imou_life/coordinator.py:44
Integration: Imou Life ([documentation](https://github.com/user2684/imou_life), [issues](https://github.com/user2684/imou_life/issues))
First occurred: 21 February 2023 at 13:32:06 (977 occurrences)
Last logged: 12:22:40

ConnectionFailed: Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 181, in _async_call_api response = await self._session.request("POST", url, json=body, timeout=self._timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 559, in _request await resp.start(conn) File "/usr/local/lib/python3.10/site-packages/aiohttp/client_reqrep.py", line 893, in start with self._timer: File "/usr/local/lib/python3.10/site-packages/aiohttp/helpers.py", line 721, in __exit__ raise asyncio.TimeoutError from None asyncio.exceptions.TimeoutError The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/config/custom_components/imou_life/coordinator.py", line 42, in _async_update_data return await self.device.async_get_data() File "/usr/local/lib/python3.10/site-packages/imouapi/device.py", line 414, in async_get_data await sensor_instance.async_update() File "/usr/local/lib/python3.10/site-packages/imouapi/device_entity.py", line 285, in async_update if not await self._async_is_ready(): File "/usr/local/lib/python3.10/site-packages/imouapi/device_entity.py", line 86, in _async_is_ready awake = await self._device_instance.async_wakeup() File "/usr/local/lib/python3.10/site-packages/imouapi/device.py", line 384, in async_wakeup await self._api_client.async_api_setDeviceCameraStatus(self._device_id, "closeDormant", True) File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 359, in async_api_setDeviceCameraStatus return await self._async_call_api(api, payload) File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 183, in _async_call_api raise ConnectionFailed(f"{exception}") from exception imouapi.exceptions.ConnectionFailed
ConnectionFailed: Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 181, in _async_call_api response = await self._session.request("POST", url, json=body, timeout=self._timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 559, in _request await resp.start(conn) File "/usr/local/lib/python3.10/site-packages/aiohttp/client_reqrep.py", line 893, in start with self._timer: File "/usr/local/lib/python3.10/site-packages/aiohttp/helpers.py", line 721, in __exit__ raise asyncio.TimeoutError from None asyncio.exceptions.TimeoutError The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/config/custom_components/imou_life/coordinator.py", line 42, in _async_update_data return await self.device.async_get_data() File "/usr/local/lib/python3.10/site-packages/imouapi/device.py", line 414, in async_get_data await sensor_instance.async_update() File "/usr/local/lib/python3.10/site-packages/imouapi/device_entity.py", line 290, in async_update data = await self.api_client.async_api_getMessageCallback() File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 415, in async_api_getMessageCallback return await self._async_call_api(api, payload) File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 183, in _async_call_api raise ConnectionFailed(f"{exception}") from exception imouapi.exceptions.ConnectionFailed
APIError: OP1001: Operation failed. Traceback (most recent call last): File "/config/custom_components/imou_life/coordinator.py", line 42, in _async_update_data return await self.device.async_get_data() File "/usr/local/lib/python3.10/site-packages/imouapi/device.py", line 414, in async_get_data await sensor_instance.async_update() File "/usr/local/lib/python3.10/site-packages/imouapi/device_entity.py", line 285, in async_update if not await self._async_is_ready(): File "/usr/local/lib/python3.10/site-packages/imouapi/device_entity.py", line 86, in _async_is_ready awake = await self._device_instance.async_wakeup() File "/usr/local/lib/python3.10/site-packages/imouapi/device.py", line 384, in async_wakeup await self._api_client.async_api_setDeviceCameraStatus(self._device_id, "closeDormant", True) File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 359, in async_api_setDeviceCameraStatus return await self._async_call_api(api, payload) File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 216, in _async_call_api raise APIError(error_message) imouapi.exceptions.APIError: OP1001: Operation failed.
ConnectionFailed: Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 181, in _async_call_api response = await self._session.request("POST", url, json=body, timeout=self._timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 559, in _request await resp.start(conn) File "/usr/local/lib/python3.10/site-packages/aiohttp/client_reqrep.py", line 893, in start with self._timer: File "/usr/local/lib/python3.10/site-packages/aiohttp/helpers.py", line 721, in __exit__ raise asyncio.TimeoutError from None asyncio.exceptions.TimeoutError The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/config/custom_components/imou_life/coordinator.py", line 42, in _async_update_data return await self.device.async_get_data() File "/usr/local/lib/python3.10/site-packages/imouapi/device.py", line 414, in async_get_data await sensor_instance.async_update() File "/usr/local/lib/python3.10/site-packages/imouapi/device_entity.py", line 294, in async_update data = await self.api_client.async_api_getDeviceCameraStatus(self._device_id, self._name) File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 343, in async_api_getDeviceCameraStatus return await self._async_call_api(api, payload) File "/usr/local/lib/python3.10/site-packages/imouapi/api.py", line 183, in _async_call_api raise ConnectionFailed(f"{exception}") from exception imouapi.exceptions.ConnectionFailed
InvalidResponse: electricitys not found in {'electricitys': [{'type': 'batteryAdapter', 'litElec': '100'}]} Traceback (most recent call last): File "/config/custom_components/imou_life/coordinator.py", line 42, in _async_update_data return await self.device.async_get_data() File "/usr/local/lib/python3.10/site-packages/imouapi/device.py", line 414, in async_get_data await sensor_instance.async_update() File "/usr/local/lib/python3.10/site-packages/imouapi/device_entity.py", line 168, in async_update raise InvalidResponse(f"electricitys not found in {data}") imouapi.exceptions.InvalidResponse: electricitys not found in {'electricitys': [{'type': 'batteryAdapter', 'litElec': '100'}]}
Logger: aiohttp.server
Source: custom_components/imou_life/camera.py:98
Integration: Imou Life ([documentation](https://github.com/user2684/imou_life), [issues](https://github.com/user2684/imou_life/issues))
First occurred: 21 February 2023 at 20:07:31 (423 occurrences)
Last logged: 12:23:06

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/imouapi/device_entity.py", line 578, in async_get_image
    raise InvalidResponse(f"status code {response.status}")
imouapi.exceptions.InvalidResponse: status code 404

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

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
    resp = await request_handler(request)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 80, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 145, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/camera/__init__.py", line 724, in get
    return await self.handle(request, camera)
  File "/usr/src/homeassistant/homeassistant/components/camera/__init__.py", line 742, in handle
    image = await _async_get_image(
  File "/usr/src/homeassistant/homeassistant/components/camera/__init__.py", line 170, in _async_get_image
    if image_bytes := await camera.async_camera_image(
  File "/config/custom_components/imou_life/camera.py", line 98, in async_camera_image
    return await self.sensor_instance.async_get_image()
  File "/usr/local/lib/python3.10/site-packages/imouapi/device_entity.py", line 581, in async_get_image
    raise InvalidResponse(f"unable to retrieve image from {url}: {exception}") from exception
imouapi.exceptions.InvalidResponse: unable to retrieve image from https://ali-pano-fk.oss-eu-central-1.aliyuncs.com/snappic/
jrb80 commented 1 year ago

@jrb80 is video stream working for you? Thanks

Cloud video streaming does not work with my Imou doorbell and therefore I am using a generic camera integration using RSTP, e.g. rtsp://192.168.1.108:554/cam/realmonitor?channel=1&subtype=0

I use a webhook trigger using the Imou cloud integration to receive motion/call notifications and then create local snapshots but I found it not to be 100% reliable.

kkwestt commented 1 year ago

imou DB60 inside

IMG_9520 IMG_9521 IMG_9522 IMG_9523 IMG_9524

user2684 commented 1 year ago

Thanks guys. It behaves slightly different from an imou webcam. But enough to create troubles. I see the image snapshot service is not available there. The ConnectionFailed looks like network connectivity issue instead while the InvalidResponse should only impact the "battery" sensor so I'd not be worries about that. All of this would require a step by step debugging and troubleshooting any API call and response which is really difficult without having the device. I'll keep on eye open when there will be a decent discounted price to buy one to run some additional tests but I will still keep this as a roadmap item

jrb80 commented 1 year ago

I since upgraded my DB11 to a DB61i, better image quality and other improvements. But the main difference is that the DB61 supports local API and therefore I am using the Dahua custom integration with HA. It also supports Imou cloud but why bother if you can do everything local. The Dahua integration exposes the call button and other sensors, i.e motion and direct video streaming rather than cloud. I did read on some forums that Dahua disabled the local API on newer firmwares, so results may vary.

BlackRapsbeere commented 1 year ago

Can you tell which firmware is installed in a working setup? Do you know if one can download the firmware files somewhere and if you can downgrade? Thanks in advance!

jrb80 commented 1 year ago

Can you tell which firmware is installed in a working setup? Do you know if one can download the firmware files somewhere and if you can downgrade? Thanks in advance!

See here

mgolaszewski93 commented 3 months ago

Please add full support for DB11.

jrb80 commented 3 months ago

The DB11 does not support the Imou webhooks particularly well.

My advice is to upgrade to the DB61 and use local Dahua API.

mgolaszewski93 commented 3 months ago

Software update or hardware?