ONVIF camera

I’ve got 2 sricams, how have you configured them in homeassistant.
On mine I found that the component works even if you don’t specify a user or password. So all you need to do is specify the IP.
Are you using the released version or the custom_component I wrote yesterday.

Matt

Not yet, where i have to create the folder? In /home/homassistant or /home/homeassistant/.homeassistant ?

In the config file i have only
platform: onvif
host: xxx.xxx.xxx.xxx

Just tried both, bit always get “error loading image” in the front-end. Do I have to install any other components or library?

Yes you need to make sure ffmpeg is setup. See ffmpeg hub

The custom_components folder needs to be in the same folder as the configuration.yaml

Ok matt…
now all is working perfectly!

Installed the ffmpeg and put into the config, then only with the ip i can see the image from cam.

Thanks for all!

I’ve tried adding your custom component which has solved the wsdl issue. However I am now getting an issue with the stream closing unexpectedly.

I am using the official docker image at 0.48.1 and have ffmpeg setup.

2017-07-23 19:25:55 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 422, in start
    resp = yield from self._request_handler(request)
  File "/usr/local/lib/python3.6/site-packages/aiohttp/web.py", line 306, in _handle
    resp = yield from handler(request)
  File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 213, in coro
    res = yield from res
  File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 213, in coro
    res = yield from res
  File "/usr/src/app/homeassistant/components/http/ban.py", line 58, in ban_middleware_handler
    return (yield from handler(request))
  File "/usr/src/app/homeassistant/components/http/__init__.py", line 424, in handle
    result = yield from result
  File "/usr/src/app/homeassistant/components/camera/__init__.py", line 338, in get
    response = yield from self.handle(request, camera)
  File "/usr/src/app/homeassistant/components/camera/__init__.py", line 376, in handle
    yield from camera.handle_async_mjpeg_stream(request)
  File "/config/custom_components/camera/onvif.py", line 99, in handle_async_mjpeg_stream
    yield from stream.close()
  File "/usr/local/lib/python3.6/site-packages/haffmpeg/core.py", line 131, in close
    yield from self._proc.communicate(input=b'q')
  File "/usr/local/lib/python3.6/asyncio/subprocess.py", line 195, in communicate
    loop=self._loop)
  File "/usr/local/lib/python3.6/asyncio/futures.py", line 331, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 244, in _wakeup
    future.result()
  File "/usr/local/lib/python3.6/asyncio/futures.py", line 244, in result
    raise self._exception
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 179, in _step
    result = coro.send(None)
  File "/usr/local/lib/python3.6/asyncio/subprocess.py", line 143, in _feed_stdin
    self.stdin.write(input)
  File "/usr/local/lib/python3.6/asyncio/streams.py", line 294, in write
    self._transport.write(data)
  File "uvloop/handles/stream.pyx", line 632, in uvloop.loop.UVStream.write (uvloop/loop.c:74612)
  File "uvloop/handles/handle.pyx", line 150, in uvloop.loop.UVHandle._ensure_alive (uvloop/loop.c:54917)
RuntimeError: unable to perform operation on <WriteUnixTransport closed=True reading=False 0x7f2fc0f8dbd8>; the handler is closed

What camera are you using? Do you get a picture at all?

It’s a Digoo DG-M1Q. I can get a picture using rstp but not onvif.

Try adding debug logging for the component.

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.

Not sure that logging config is correct. I get this error.

2017-07-23 21:20:22 ERROR (MainThread) [homeassistant.config] Invalid config for [logger]: [homeassistant.components.camera] is an invalid option for [logger]. Check: logger->logger->homeassistant.components.camera. (See /config/configuration.yaml, line 133). Please check the docs at https://home-assistant.io/components/logger/
2017-07-23 21:20:22 ERROR (MainThread) [homeassistant.setup] Setup failed for logger: Invalid config.

I’ve edited the post above. I had missed the logs. Here is what I have in my configuration. Logger

It looks like it gets the correct URL for rtsp from the onvif service. However it doesn’t include the username and password. If I manually add an ffmpeg component with the username and password included in the url it works.

2017-07-24 07:05:54 DEBUG (MainThread) [custom_components.camera.onvif] ONVIF Camera Using the following URL for Thumper Cam: rtsp://192.168.0.57:554/onvif1

Working url rtsp://username:[email protected]:554/onvif1.

That’ll be the issue. I’ll have a look at the code tonight and update once i’ve got a fix. I’ll need to test that it doesn’t break existing code.

I’ve made another change to allow RTSP auth
here is the custom_component (https://gist.githubusercontent.com/matt2005/c9bd5972219652ee3b27584f8951662e/raw/7162a7319fb78ecaafd1c84300bf44c04ed89b6f/onvif.py)

You will need to add STREAM_AUTH in you config if your camera requires RTSP authentication.

1 Like

No luck here, it works with Onvifier on my phone though:

Network Video Transmitter (NVT) name: IPCAM NVT type: ONVIF Model: C6F0SgZ0N0P0L0 Address/URL: 192.168.1.111:8080 Media Profile: MainProfile Resolution: 1920x1088 Encoding: H264 H.264: Main Profile Level: 4.2 Transport protocol: RTP/RTSP/TCP RTP packets received: 2056 RTP packets lost: 0 Frame rate (fps): 14.4 Audio: G.711 A-Law ONVIF port: 8080 RTSP port: 554 Data Rate (Mbit/s): 0.950 1501187908110:1501187908110

What error are you seeing? Have you added the debug logging?

Yeah, installed the custom component and enabled logging. It looks like my camera’s rtsp port is 554 if that’s any use. It looks like it’s not even getting to the point where it receives the stream url

2017-07-29 11:09:47 INFO (MainThread) [homeassistant.components.camera] Setting up camera.onvif
2017-07-29 11:09:53 ERROR (MainThread) [homeassistant.components.camera] Error while setting up platform onvif
Traceback (most recent call last):
  File "/config/deps/suds/transport/http.py", line 78, in send
    fp = self.u2open(u2request)
  File "/config/deps/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 "/config/deps/suds/client.py", line 652, in send
    reply = transport.send(request)
  File "/config/deps/suds/transport/http.py", line 178, in send
    return HttpTransport.send(self, request)
  File "/config/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 "/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 "/config/deps/suds/client.py", line 559, in __call__
    return client.invoke(args, kwargs)
  File "/config/deps/suds/client.py", line 618, in invoke
    result = self.send(soapenv)
  File "/config/deps/suds/client.py", line 664, in send
    result = self.failed(binding, e)
  File "/config/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 "/usr/lib/python3.6/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.6/asyncio/tasks.py", line 352, in wait_for
    return fut.result()
  File "/usr/lib/python3.6/asyncio/futures.py", line 244, in result
    raise self._exception
  File "/usr/lib/python3.6/asyncio/tasks.py", line 179, 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')

I’ve seen the error 400 when the credentials are incorrect. Have you configured them in the configuration?

Yeah, here’s my configuration and some screenshots from the camera’s control website. Thanks for the help by the way.

What are you seeing in the debug logs?