Xiaomi Gateway Integration

Nice. I’m planning to add the gateway itself as a light. Should be easy

3 Likes

looking forward to test this feature.

@rave I think it’s been asked before, but whilst looking at light integration with the gateway, any chance of the Alarm feature?

Yeah. I’ll look at it at the same time

2 Likes

You’re the man.

1 Like

I have tried alarm functions some minutes ago - it doesn’t report about alarm state change (only reporting on rgb light change while arming light flashes and I think it will report flashes on alarm trigger), but we can’t turn it on\off and ask for alarm state.

P.S. No reports about radio state change too.

1 Like

Too bad. thanks for testing it

looks like i’m not the only one to have sensor stuck on “on” then. I noticed at the moment if sensors are stuck and I restart HA, they are then in the “on” state on the home card, but if I go into history, they are in the “unknown” state, until they get triggered to the on state, then it will function as normal.

Today one of the motion sensor is not aligning with the xiaomi app, HA shows it as turning on and off during the day but timestamp wise it’s all over the place. Feels like the off signal never made it to HA so HA was showing “on” and xiaomi was showing off. However this doesn’t explain why magically my sensors all went into “on” last time

it would be great to use the light sensor … an important sensor for other functions in HA, possible?

I have similar problem when having two interfaces, so I wrote a small patch that allows you to specify which interface to listen on for communication from the hub. It’s an open pull request now, so once it’s accepted give it a try by adding in your config the ip address of your WiFi

xiaomi:
  key: aaaaaaaaaaaaaaaaaa    
  interface: 192.168.xxx.yyy

i made some tests…

when the sensor trigger the output is:

{"cmd":"report","model":"motion","sid":"xxxxxx","short_id":22951,"data":"{\"status\":\"motion\"}"}

after 1 minute the sensor is avaible again for a new reading so i think must be set to off manually…

after 2 minutes without movement this is the output

{"cmd":"report","model":"motion","sid":"xxxxxx","short_id":22951,"data":"{\"no_motion\":\"120\"}"}

@rave i’ll hope this is helpful… :slight_smile:

1 Like

Having the same issue on the motion sensor, sticks at ‘on’ state.
I’m running the latest firmware version (from yesterday), never got it running on the previous version so cannot compare.

@rave is there anything I can do to help you on this one?

Edit: how do you guys sniff the network traffic? I don’t see anything relevant in WireShark.
I’m running HaSS on a Raspberry PI and sniffing from a Windows machine. Should I see the hub broadcast any status updates on sensors?

Edit2:
Patched this module to printout any messages received.
Can conform the motion sensor sends a message no_motion after 2 min:
{‘data’: ‘{“no_motion”:“120”}’, ‘cmd’: ‘report’, ‘sid’: ‘xx’, ‘model’: ‘motion’, ‘short_id’: 61771}

Hi, great work! It looks very good, but i can’t make it work in my pc. I think maybe its a Multicast Network configuration problem but i can’t figure out how to fix it.
In the session’s log i have this:
17-01-12 20:51:14 homeassistant.bootstrap: Error during setup of component xiaomi
Traceback (most recent call last):
File “C:\Users\gustavo\AppData\Roaming.homeassistant\custom_components\xiaomi.py”, line 163, in _send_socket
data, addr = socket.recvfrom(1024)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\xxx\AppData\Local\Programs\Python\Python35\lib\site-packages\homeassistant\bootstrap.py”, line 151, in _async_setup_component
None, component.setup, hass, config)
File “C:\Users\xxx\AppData\Local\Programs\Python\Python35\lib\asyncio\futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “C:\Users\xxx\AppData\Local\Programs\Python\Python35\lib\asyncio\tasks.py”, line 304, in _wakeup
future.result()
File “C:\Users\xxx\AppData\Local\Programs\Python\Python35\lib\asyncio\futures.py”, line 293, in result
raise self._exception
File “C:\Users\xxx\AppData\Local\Programs\Python\Python35\lib\concurrent\futures\thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)
File “C:\Users\xxx\AppData\Roaming.homeassistant\custom_components\xiaomi.py”, line 43, in setup
XIAOMI_HUB = XiaomiHub(key)
File “C:\Users\xxx\AppData\Roaming.homeassistant\custom_components\xiaomi.py”, line 90, in init
data = self._send_socket(’{“cmd”:“whois”}’, “iam”, self.MULTICAST_ADDRESS, self.GATEWAY_DISCOVERY_PORT)
File “C:\Users\xxx\AppData\Roaming.homeassistant\custom_components\xiaomi.py”, line 172, in _send_socket
except socket.timeout:
TypeError: catching classes that do not inherit from BaseException is not allowed

Any help?
Thanks

Thanks for testing guys. So i think what needs to be done is to manually turn off the motion after one minute if no new messages come from the gateway.

Btw, the code for multi gateway is almost done

2 Likes

Socket timeout means it couldn’t find the gateway after 10 seconds. Make sure you have enabled developer mode on your gateway

I had timeout errors before too. I end up just restarting HA and it works again. also you can try what I did and change
sock.bind((self.MULTICAST_ADDRESS, self.MULTICAST_PORT)) to
sock.bind((“MyIPAddress”, self.MULTICAST_PORT))
make sure you double quote

I have two NICs hence it is needed. Also check your antivirus/firewall since you are running it on windows like me.

@rave I think I found the problem with the hanging motion sensors :slight_smile:

In components/binary_sensor/xiaomi.py
There is this code that checks if the data contains the value ‘status’:

    def parse_data(self, data):
        if not self._data_key in data:
            return False 

If motion is detected, there is a ‘status’ key in data.
{'short_id': 61771, 'sid': 'xx', 'cmd': 'report', 'model': 'motion', 'data': '{"status":"motion"}'}

But the no_motion event doens’t contain ‘status’, its marked as ‘no_motion’.
{'short_id': 61771, 'sid': 'xx', 'cmd': 'report', 'model': 'motion', 'data': '{"no_motion":"120"}'}

In that case the parse_data method return false instantly instead of further processing.
I’m going to bed now, in my time-zone its 02:45 now. Maybe I can submit a pull request for this tomorrow :slight_smile:

Edit:

The door sensor does send a status while in opening or closing state.
{'status': 'open'} and {'status': 'close'}

2 Likes

Yes, i’ve checked and it’s enabled.

I’ve disabled the firewall and antivirus, also i’ve changed the code with your suggestion but still logging the same error message.
I’ll try to run HA from another pc.
Thanks

Hello,

First of all, thank you everyone for the great work and specially to @rave for the code sharing.
I successfully integrated my gateway with motion sensors, cube and temperature and humidity sensors.
Everything was working fine until yesterday that I updated the gateway’s FW.
Now, as @jmvermeulen says, the sensors get stuck:

  • Humidity reporting 0%

  • Temperature reporting 100°C

  • Motion sensors always in ON state

After manually pressing the temperature and humidity sensors button, it seems they start reporting normally, but the motion sensors some of them are still stuck in ON state. the strange thing is always one of them (I have 5) is always working good, but the rest are unstable.
As an example:

This sensor last night was fluctuating from ON to OFF all night, when it should have been always in OFF state.
The one the is working good was stable all night, I wonder why the behaviour on them are different…
Let me know if I can help with anything!

Thank you!
Best regards,
Santi