Hacking the XiaomiFang Wifi camera

I thought I’d update you all on improvements to the custom firmware on the Dafang, there is now an MQTT function allowing the control of the camera, much better than my “hacked” shell files.
You’ll need to edit the file firmware_mod\config\mqtt.dist to add your MQTT settings, then rename the file into mqtt
Once done, browse to your camera IP address and click on manage scripts
Then start and enable scripts for both mqtt-control and mqtt-status:
image

Now head over to your config file in HA and add these:

- platform: mqtt
  name: "DaFang Living Room RTSP"
  state_topic: "LivingRoom/DaFang/status"
  value_template: "{% if value_json[\"RTSP-Server\"] == 'running' %}h264_noseg_start{% else %}rtsp_stop{% endif %}"
  command_topic: "LivingRoom/DaFang/set"
  payload_on: "h264_noseg_start"
  payload_off: "rtsp_stop"
  optimistic: false

- platform: mqtt
  name: "Dafang Living Room Blue LED"
  state_topic: "LivingRoom/DaFang/status"
  value_template: "{% if value_json.LEDs.Blue == 'on' %}blue_led_on{% else %}blue_led_off{% endif %}"
  command_topic: "LivingRoom/DaFang/set"
  payload_on: "blue_led_on"
  payload_off: "blue_led_off"
  optimistic: false

- platform: mqtt
  name: "Dafang Living Room Yellow LED"
  state_topic: "LivingRoom/DaFang/status"
  value_template: "{% if value_json.LEDs.Yellow == 'on' %}yellow_led_on{% else %}yellow_led_off{% endif %}"
  command_topic: "LivingRoom/DaFang/set"
  payload_on: "yellow_led_on"
  payload_off: "yellow_led_off"
  optimistic: false

I’ve not added the IR-Cut or IR-LED, but the process is the same
and the same can be done to control the motor. Technically there would be no need for state_topic nor value_template, but then the switch would be rendered differently (the flash icon set) and would not revert back to off once pressed, though that could always be changed with an automation…
so here is the code I’m using for the down command:

- platform: mqtt
  name: "Dafang Kitchen Down"
  state_topic: "Kitchen/DaFang/status"
  command_topic: "Kitchen/DaFang/set"
  payload_on: "motor_down"
  optimistic: false

Note it takes about 1 sec for the switch to go back to off, so maybe an automation is needed for impatient users :slight_smile:
alternatively, removing the state_topic and value_template will show flash icons for controls, so it’s technically possible to use just 1 switch to control both directions: off = left, on = right. Only thing is I don’t know if if it is possible to change these icons, but here’s how I’ve down it:

- platform: mqtt
 name: "Dafang Living Room Left Right"
 command_topic: "LivingRoom/DaFang/set"
 payload_off: "motor_left"
 payload_on: "motor_right"
 optimistic: false

Here’s the result:
image

10 Likes