I finally got my MIPC camera integrated as a feed in Home Assistant.
In my case, the camera didn’t expose a static URL that I could use as the stream URL. Luckily some genius created a GitHub repository that is able to get a temporary stream URI.
Even better, this repo can be installed by pip install:
python -m pip install mipc_camera_client
Requirements
For this to work, you’ll need the following information:
- Your camera’s IP. (Maybe check your router.)
- The username for your camera.
- This is the password for logging into the camera.
I was able to log into my camera at port 80. Hopefully, once you know the IP address of your camera, you’ll be able to set and test the username and password. Otherwise, try “admin” and “admin”.
The solution
Using go2rtc
To set this up, I add the following to my go2rtc config. (go2rtc can be installed via Add-ons).
streams:
my_stream_name: echo:bash /config/get_dynamic_stream.sh
This configures a dynamic stream called “my_stream_name” and the value is retrieved dynamically by running a “get_dynamic_stream.sh” shell script (that I placed in my ~/config
folder).
The Bash script
Installing the mipc_camera_client package takes time, so this script first assumes the package is installed.
Here’s the get_dynamic_stream.sh
!/bin/bash
# First attempt to run the script
python /config/get_dynamic_stream.py
if [ $? -eq 0 ]; then
# The script succeeded!
exit 0
else
# If the script failed, install the package and try again
python -m pip install mipc_camera_client >/dev/null 2>&1
python /config/get_dynamic_stream.py
fi
If it’s can’t run the application successfully, it installs assumes the package needs to be installed and it installs the package and tries to run the Python script again.
The Python script
get_dynamic_stream.py looks like this:
from mipc_camera_client import MipcCameraClient
my_camera_ip = "" # Replace with your camera's IP. (Maybe check your router.)
my_camera_username = "" # The username for your camera. (This was on a sticker under my camera.)
my_camera_password = "" # (This is the password I used to log into my camera)
c = MipcCameraClient(my_camera_ip)
c.login(my_camera_username, my_camera_password)
uri = "ffmpeg:" + c.get_rtmp_stream()
print(uri)
This is a pretty simple script that simply provides this 3rd-party package with the details it needs to perform its job.
Adding the camera feed to a dashboard
To display the camera feed on a dashboard, I used WebRTC Camera. (The link has details for installing the component in Home Assistant). Specifically, I’m using the webrtc-camera card
.
cards:
- type: custom:webrtc-camera
url: my_stream_name
Specifically, the “URL” here is a documented feature of go2rtc, where it will look up “my_stream_name” in go2rtc config.
Conclusion
Here, we’re configuring a dynamic stream for a MIPC compatible camera using a couple of components:
- The dashboard references a named stream in the go2rtc configuration.
- go2rtc dynamically retrieves the stream URI by executing a bash script.
- the bash script installs the prerequisite python package and runs the python code.
- the python code programmatically retrieves the URI using a 3rd party python package.
Magic!
Good luck!