Hacking the XiaomiFang Wifi camera

“homeassistant” is only the default value for discovery prefix. That means I can use anoter value…
By the ways, I’ve just modified the mqtt.conf for my cameras like this :

############################################################
# edit this file and move it to /system/sdcard/config/mqtt.conf #
############################################################

export LD_LIBRARY_PATH='/thirdlib:/system/lib:/system/sdcard/lib'

# Options for mosquitto_sub & mosquitto_pub
USER=
PASS=
HOST=192.168.1.254
PORT=1883

# Define a location
LOCATION="homeassistant"

# Define device name
DEVICE_NAME="Cam01"

# Define the base topic used by the camera
# send a message to myhome/dafang/set with the payload help for help.
# Results will be placed in myhome/dafang/${command} or topic/dafang/error - so please subscribe topic/dafang/# for testing purposes
TOPIC="$LOCATION/$DEVICE_NAME"

# Define an autodiscovery prefix, if autodiscovery is desired:
AUTODISCOVERY_PREFIX="homeassistant"

# Define additional options for Mosquitto here.
# For example --cafile /system/sdcard/config/DST_Root_CA_X3.pem --tls-version tlsv1
# or use a special id to connect to brokers like azure
MOSQUITTOOPTS="-V mqttv311"

# Add options for mosquitto_pub like -r for retaining messages
MOSQUITTOPUBOPTS=""

# Send a mqtt statusupdate every n seconds
STATUSINTERVAL=30

# Publish and update the light dependant resistor (LDR) status or not. The
# default value if the variable is not set is to send it. This might be useful
# for owners of devices that lack the hardware sensor to avoid publishing
# rubbish.
SENDLDR="true"

Now I have a main topic named homeassistant, but doesn’t change anything. The devices are not discovered.

Location should not be homeassistant
It should be something else
AUTODISCOVERY_PREFIX should be homeassistant.
On MQTT Explorer you should see topics under homeassisant
For example
image

In this example CAMERAS is LOCATION
and homeassistant AUTODISCOVERY_PREFIX

You also need to activate autodiscovery in MQTT integration

1 Like

Hello, Gismo38… Me again !
I fond a solution, there is a problem with the new autdiscover script in Dafang’s hack. Just tried an old one an now everything is OK !
I’ll open an issue in Dafang’s Hack on GitHub…

Thank you very much for your support :+1:

Ok no problem :slight_smile: I dev some part of this hack :stuck_out_tongue:

I found this on Dafang’s Hack on Github
‘Just start with a fresh firmware mod. There was a non functional commit some hours ago, you might have caught that.’

Maybe you are in this case. For me it work well with last version.

Oh, that’s great :+1: I’m happy to use the hack….
Have to check how I can open an Issue (never done it !)… but mqtt_autodiscovery.sh script in the latest update is not working… Using it in ssh session I have errors

image

There is no change since one month on this script.
You can try to get the script from this address https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks/blob/be2cb151c0506f2a90a8d301627d5c5affc116c0/firmware_mod/scripts/mqtt-autodiscovery.sh to compare with yours

Yes I’ve seen that.
I downloaded the script from your link and tried it again with same issue. So I think it never worked with the last script. But if you discovered controls and device earlier you don’t see the problem (HA has kept the older informations). I just have added my cameras a few day’s ago with the new hack and that’s why I have the problem.

Ok I will upgrade mine and have a test.
I will send you the result

Is there a way to fix this?

1586275623: New client connected from 192.168.1.50 as mosqpub|579-WyzeCam+Kan (p2, c1, k60, u’gijsje’).
1586275623: ACL denying access to client with dangerous client id “mosqpub|579-WyzeCam+Kan”
1586275623: Client mosqpub|579-WyzeCam+Kan disconnected.

Never mind found it

Updated two dafang via the web ui of the camera (hack). Once updated I cant access the web ui. No response. But mqtt works and also snapshots. But currentpic.cgi doesnt work. Anyone experienced this?

Well I think the problem is my MQTT password. I’ve set a password starting with the $ character and in the string $COMMAND used in the mqtt-discovery.sh script the password doesn’t appear…
I changed the password to a string not starting with $ character and now everything works well :+1:

So the problem isn’t in the script :slight_smile:

Hello, my Dafang cameras are now working in HA and I’d like to use automations to enable motion detection when no one is home and to disable it when someone comes home.
So I wrote this automation :

  • id: mode_presence
    alias: Mode présence
    trigger:
    platform: state
    entity_id: group.Famille
    from: ‘not_home’
    to: ‘home’
    action:
    #Extinction détection mouvement

    • service: camera.disable_motion_detection
      entity_id: camera.cam01
    • service: camera.disable_motion_detection
      entity_id: camera.cam02
      #Information présence
    • service: notify.telegram
      data:
      message: ‘Mode présence activé’
  • id: mode_absence
    alias: Mode absence
    trigger:
    platform: state
    entity_id: group.Famille
    from: ‘home’
    to: ‘not_home’
    action:
    #Démarrage détection mouvement

    • service: camera.enable_motion_detection
      entity_id: camera.cam01
    • service: camera.enable_motion_detection
      entity_id: camera.cam02
      #Information présence
    • service: notify.telegram
      data:
      message: ‘Mode absence activé’

It seems that the motion_detection services are not working. In my homeassistant log file I have those errors

Traceback (most recent call last):
File “/usr/lib/python3.7/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/camera/init.py”, line 433, in disable_motion_detection
raise NotImplementedError()
NotImplementedError
2020-04-10 16:03:32 ERROR (MainThread) [homeassistant.core] Error doing job: Future exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.7/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/camera/init.py”, line 433, in disable_motion_detection
raise NotImplementedError()
NotImplementedError
2020-04-10 16:10:48 ERROR (MainThread) [homeassistant.core] Error doing job: Future exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.7/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/camera/init.py”, line 424, in enable_motion_detection
raise NotImplementedError()
NotImplementedError

Can someone explain me why I have those errors ? Should I use MQTT messages to set the motion detection ON or OFF ?

The Telegram notification works fine.

group.Famille is incorrect. All entity ids are lower case

Thanks Nickrout, this wasn’t the problem I think but was a mistake.
I’ve change the automation ad this works

  • id: mode_presence
    alias: Mode présence
    trigger:
    platform: state
    entity_id: group.famille
    from: ‘not_home’
    to: ‘home’
    action:
    #Extinction détection mouvement
    • service: switch.turn_off
      data:
      entity_id: switch.cam01_motion_detection
    • service: switch.turn_off
      data:
      entity_id: switch.cam02_motion_detection
      #Information présence
    • service: notify.telegram
      data:
      message: ‘Mode présence activé’

Please post your code properly. See How to help us help you - or How to ask a good question

I get following error in HA logs from long time but I ignored it. Now decided to fix and I ran into this thread. I thought of applying fix (removing the icon info) but I didn’t find icon reference in motion sensor section in mqtt-autodiscovery.sh file.

@nickrout Any ideas to fix this issue?

# Motion sensor 
/system/sdcard/bin/mosquitto_pub.bin -h "$HOST" -p "$PORT" -u "$USER" -P "$PASS" -t "$AUTODISCOVERY_PREFIX/binary_sensor/$DEVICE_NAME/motion/config" ${MOSQUITTOPUBOPTS} ${MOSQUITTOOPTS} "{\"name\": \"$DEVICE_NAME motion sensor\", \"unique_id\": \"$MAC_SIMPLE-motion-sensor\", \"device\": {\"identifiers\": \"$MAC_SIMPLE\", \"connections\": [[\"mac\", \"$MAC\"]], \"manufacturer\": \"$MANUFACTURER\", \"model\": \"$MODEL\", \"name\": \"$MANUFACTURER $MODEL\", \"sw_version\": \"$VER\"}, \"state_topic\": \"$TOPIC/motion\", \"device_class\": \"motion\"}"

HA error:

2020-04-12 19:30:30 ERROR (MainThread) [homeassistant.util.logging] Exception in async_discover when dispatching 'mqtt_discovery_new_binary_sensor_mqtt': ({'name': 'garage-camera motion sensor', 'unique_id': 'a4da222fd641-motion-sensor', 'icon': 'mdi:run', 'state_topic': 'home/garage-camera/motion', 'device_class': 'motion', 'platform': 'mqtt'},)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/binary_sensor.py", line 84, in async_discover
    config = PLATFORM_SCHEMA(discovery_payload)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: extra keys not allowed @ data['icon']

Hey, I know this is an old post but has any work been done on utilising the speaker and mic?
I was going to use it as an intercom with my Xiaomi ZigBee button at the front door.

Unfortunately no.
Just as an update. The camera has been ticking fine, the only problem I had so far was a dead SD card. This was an easy fix by replacing the SD and following the instructions again as per my original post

1 Like