I have 2 of these Xiaomi DaFang cameras and I’ve integrated them with HA.
Note that you will need ffmpeg to get this working.
I personally use motion in the middle to perform motion detection / recording and streaming (I use that streamed feed in HA). Plus for some reason the quality is better than HA directly pulling the RTSP feed.
- Get the modified firmware from https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks
- Once installed and setup as per the instructions, start the RTSP sever (use the MJPEG RTSP)
- If connecting from HA directly, use this config:
camera:
- platform: ffmpeg
name: Cam
input: -rtsp_transport tcp -i rtsp://192.168.x.x:8554/unicast
if using motion in the middle, your config should look like this:
- platform: mjpeg
mjpeg_url: http://192.168.x.x:808x
name: Cam
in addition you can control some functions of the camera directly from HA like the blue/yellow LED, IR Filter and IR LED, X and y motor movements etc. The way I achieved this is with a combination of scrape sensors and template switches.
For the scrape sensors I had to create a few web pages to return statuses. I can’t attach them here so I’m posting one example for the blue LED:
#!/bin/sh
echo "Content-type: text/html"
echo ""
getgpio(){
GPIOPIN=$1
cat /sys/class/gpio/gpio$GPIOPIN/value
}
cat << EOF
<!DOCTYPE html>
<html>
<head>
<title>Fang Hacks Blue LED Status</title>
</style>
</head>
<body>
EOF
# status of 0 means on, status of 1 means off
getgpio 39
cat << EOF
</body>
</html>
EOF
Save the file as blue.cgi on the SD card under www/cgi-bin
For other functions, simply replace the GPIO number (39 here) with the one matching the function that you need:
Blue LED: 39
Yellow LED: 38
IR Cut: 26
IR LED: 49
Then you can create scrape sensors, here is the example for the blue LED:
- platform: scrape
resource: http://192.168.x.x/cgi-bin/blue.cgi
name: dafang_blue_led_status_scrape
select: "body"
Create 2 simple shell commands to turn the LED on or off:
dafang_blue_led_on: wget http://192.168.x.x/cgi-bin/action.cgi?cmd=blue_led_on
dafang_blue_led_off: wget http://192.168.x.x/cgi-bin/action.cgi?cmd=blue_led_off
Then create a template switch:
- platform: template
switches:
dafang_blue_led:
friendly_name: Dafang Blue LED
value_template: "{{ is_state('sensor.dafang_blue_led_status_scrape', '\n0\n') }}"
turn_on:
service: shell_command.dafang_blue_led_on
turn_off:
service: shell_command.dafang_blue_led_off
Here is the result:
Hope this helps