400 Bad Request with Onvif

Hello there!

I’m trying to configure my IP cameras and the least I can say is that I’m having a bad time. :smiley: Until now I was using Synology Surveillance Station but since I’m currently building a HASS setup I though it would be a good thing to ditch SS and its absurd licensing model.

One of my IP cameras, however, won’t cooperate much. An Escam QD300 Brick. I first set it up with ffmpeg but the stream is really random and laggy, when it won’t show the typical “Image won’t load” on the HASS UI. So I wanted to give Onvif a try since my camera is supposed to be compatible (we’re never sure with these chinese clones with random firmwares). My camera.yaml:

# Camera du jardin
- platform: onvif
  host: garden.bigoudi.lan
  port: 8899
  username: hass
  password: ssah

The log file:

2017-07-23 17:55:35 ERROR (MainThread) [homeassistant.components.camera] Error while setting up platform onvif
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/deps/suds/transport/http.py", line 78, in send
    fp = self.u2open(u2request)
  File "/home/homeassistant/.homeassistant/deps/suds/transport/http.py", line 119, in u2open
    return url.open(u2request, timeout=tm)
  File "/usr/lib/python3.5/urllib/request.py", line 472, in open
    response = meth(req, response)
  File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.5/urllib/request.py", line 510, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 590, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: **HTTP Error 400: Bad Request**

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/deps/suds/client.py", line 652, in send
    reply = transport.send(request)
  File "/home/homeassistant/.homeassistant/deps/suds/transport/http.py", line 178, in send
    return HttpTransport.send(self, request)
  File "/home/homeassistant/.homeassistant/deps/suds/transport/http.py", line 86, in send
    raise TransportError(e.msg, e.code, e.fp)
suds.transport.TransportError: **Bad Request**

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/deps/onvif/client.py", line 34, in wrapped
    return func(*args, **kwargs)
  File "/home/homeassistant/.homeassistant/deps/onvif/client.py", line 201, in wrapped
    return call(params, callback)
  File "/home/homeassistant/.homeassistant/deps/onvif/client.py", line 191, in call
    ret = func(**params)
  File "/home/homeassistant/.homeassistant/deps/suds/client.py", line 559, in __call__
    return client.invoke(args, kwargs)
  File "/home/homeassistant/.homeassistant/deps/suds/client.py", line 618, in invoke
    result = self.send(soapenv)
  File "/home/homeassistant/.homeassistant/deps/suds/client.py", line 664, in send
    result = self.failed(binding, e)
  File "/home/homeassistant/.homeassistant/deps/suds/client.py", line 726, in failed
    raise Exception((status, reason))
**Exception: (400, 'Bad Request')**

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py", line 164, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=self.hass.loop)
  File "/usr/lib/python3.5/asyncio/tasks.py", line 400, in wait_for
    return fut.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/usr/lib/python3.5/asyncio/coroutines.py", line 210, in coro
    res = func(*args, **kw)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/camera/onvif.py", line 48, in async_setup_platform
    async_add_devices([ONVIFCamera(hass, config)])
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/camera/onvif.py", line 68, in __init__
    self._input = media.GetStreamUri().Uri
  File "/home/homeassistant/.homeassistant/deps/onvif/client.py", line 36, in wrapped
    raise ONVIFError(err)
**onvif.exceptions.ONVIFError: Unknown error: (400, 'Bad Request')**

Bad requests. Bad requests everywhere. And I can’t find the reason. I found a recent similar topic right there but the given solutions (especially the custom component given by Matt) aren’t working. Is there something I’m missing?

Thanks for your help! :slight_smile:

Have you ensured that the FFmpeg hub component is installed?
ffmpeg hub
I’ve also written a fixed component for non-virtual env installs
Heres how to use the custom_component

Please create a custom_components folder in the home-assistant config folder (The folder were configuration.yaml is) and then a camera folder under the custom_components folder.
Then add the following file in to the camera folder as onvif.py
github gist onvif.py

Then restart Home-assistant

Hi Matt!

FFMpeg is correctly installed, I’m able to connect to a RSTP stream with it. I also tried your component with no more luck.

For now I set the cameras with Surveillance Station from Synology and using the according component but if I could get rid of SS it’d be way better.

Regards,
Chris.

Ok, you say FFMpeg is installed.

Do you have a configuration section in configuration.yaml like this?

ffmpeg:
  ffmpeg_bin: /usr/bin/ffmpeg
  run_test: true

What camera are you using? does the rtsp stream use authentication? e.g. rtsp://user:[email protected]/onvif1?

I am having the same problem , and the ffmpeg is defined correctly.

Please try this custom component. Onvif.py
And enable debug logging as shown in this post onvif debug

You should then see some debug entries in the log. Please post these.

Hi can you provide a bit more detail as I am new to home assistant.

Thanks

To add debugging for the onvif component.
Add the following to configuration.yaml.

logger:
  logs:
    homeassistant.components.camera: debug
    custom_components.camera.onvif: debug

You should then see the steam URL in the log. That should be a rtsp URL.

OK this is what I got.192.168.1.68_20170728103408.xml (628.5 KB)

Thanks

I have the very same error message - any update on this?

I have a pull request to implement some fixes and error handling. Just waiting for it to be merged.

Same error here. Is there any news?

I also get the 400 Bad request error. I added the ffmpeg component and the onvif.py file to the custom_components.

Error decoding json invalid character 'F' looking for beginning of value:   File "/usr/lib/python3.6/site-packages/suds/transport/http.py", line 86, in send
    raise TransportError(e.msg, e.code, e.fp)
suds.transport.TransportError: Bad Request
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 34, in wrapped
    return func(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 201, in wrapped
    return call(params, callback)
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 191, in call
    ret = func(**params)
  File "/usr/lib/python3.6/site-packages/suds/client.py", line 559, in __call__
    return client.invoke(args, kwargs)
  File "/usr/lib/python3.6/site-packages/suds/client.py", line 618, in invoke
    result = self.send(soapenv)
  File "/usr/lib/python3.6/site-packages/suds/client.py", line 664, in send
    result = self.failed(binding, e)
  File "/usr/lib/python3.6/site-packages/suds/client.py", line 726, in failed
    raise Exception((status, reason))
Exception: (400, 'Bad Request')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/entity_component.py", line 171, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=self.hass.loop)
  File "/usr/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/lib/python3.6/asyncio/futures.py", line 245, in result
    raise self._exception
  File "/usr/lib/python3.6/asyncio/tasks.py", line 180, in _step
    result = coro.send(None)
  File "/usr/lib/python3.6/asyncio/coroutines.py", line 210, in coro
    res = func(*args, **kw)
  File "/config/custom_components/camera/onvif.py", line 51, in async_setup_platform
    async_add_devices([ONVIFCamera(hass, config)])
  File "/config/custom_components/camera/onvif.py", line 72, in __init__
    self._input = media.GetStreamUri().Uri
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 36, in wrapped
    raise ONVIFError(err)
onvif.exceptions.ONVIFError: Unknown error: (400, 'Bad Request')

Not sure what to do next…

Hello,
I’m trying to connect to an onvif camera.
I already have been able to use it with the “onvifier” app on Android which also gave me the right ports to connect.
However when I try to connect it with hassio (running hassio.io on a raspi3) I receive a bad request.
I have take a look to the onvi-python3 code library to try to understand what is happening and it is crashing when is retrieving the management capabilies.

This is the error from hassio.io:

Error while setting up platform onvif
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/suds/transport/http.py", line 78, in send
    fp = self.u2open(u2request)
  File "/usr/lib/python3.6/site-packages/suds/transport/http.py", line 119, in u2open
    return url.open(u2request, timeout=tm)
  File "/usr/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.6/urllib/request.py", line 570, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/suds/client.py", line 652, in send
    reply = transport.send(request)
  File "/usr/lib/python3.6/site-packages/suds/transport/http.py", line 178, in send
    return HttpTransport.send(self, request)
  File "/usr/lib/python3.6/site-packages/suds/transport/http.py", line 86, in send
    raise TransportError(e.msg, e.code, e.fp)
suds.transport.TransportError: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 34, in wrapped
    return func(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 201, in wrapped
    return call(params, callback)
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 191, in call
    ret = func(**params)
  File "/usr/lib/python3.6/site-packages/suds/client.py", line 559, in __call__
    return client.invoke(args, kwargs)
  File "/usr/lib/python3.6/site-packages/suds/client.py", line 618, in invoke
    result = self.send(soapenv)
  File "/usr/lib/python3.6/site-packages/suds/client.py", line 664, in send
    result = self.failed(binding, e)
  File "/usr/lib/python3.6/site-packages/suds/client.py", line 726, in failed
    raise Exception((status, reason))
Exception: (400, 'Bad Request')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 84, in async_setup
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/lib/python3.6/asyncio/futures.py", line 245, in result
    raise self._exception
  File "/usr/lib/python3.6/asyncio/tasks.py", line 180, in _step
    result = coro.send(None)
  File "/usr/lib/python3.6/asyncio/coroutines.py", line 210, in coro
    res = func(*args, **kw)
  File "/config/custom_components/camera/onvif.py", line 47, in async_setup_platform
    async_add_devices([ONVIFCameraHASS(hass, config)])
  File "/config/custom_components/camera/onvif.py", line 67, in __init__
    config.get(CONF_USERNAME), config.get(CONF_PASSWORD), adjust_time=True
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 267, in __init__
    self.update_xaddrs()
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 283, in update_xaddrs
    capabilities = self.devicemgmt.GetCapabilities({'Category': 'All'})
  File "/usr/lib/python3.6/site-packages/onvif/client.py", line 36, in wrapped
    raise ONVIFError(err)
onvif.exceptions.ONVIFError: Unknown error: (400, 'Bad Request')

I have it configured it the following way:

ffmpeg:
  ffmpeg_bin: /usr/bin/ffmpeg
  run_test: true

camera:
  - platform: onvif
    host: 192.168.1.226
    username: "admin"
    password: "123456"
    port: 80
    name: camera

I don’t know what can I do more. Any suggestion?
Thank you in advance
Regards
Gabriel

Don’t want to hijack this topic, but I am working on a fix since I am experiencing this myself with a Dahua camera.

You can follow the process here:

https://github.com/home-assistant/home-assistant/issues/20668