home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.34k stars 29.88k forks source link

Xiaomi gateway light - can't turn off, no colour #20329

Closed SaturnusDJ closed 5 years ago

SaturnusDJ commented 5 years ago

Running HA 0.85.1 on Docker on RPi 3, with --net-host (pretty default setup basically). Having a Xiaomi 'Mi Hub' lumi v3 gateway with a bunch of sensors attached and they work perfectly with the gateway and with HA, through the use of the LAN access feature, all the time. The config entry is fully manual:

xiaomi_aqara:
  discovery_retry: 10
  interface: LAN_ipaddress_RPi3
  gateways:
    - mac: xxx
      host: LAN_ipaddress_Mi_Hub
      key: yyy

The gateway light and illumination however, don't work as expected at all.

Debug data follows.

Turning on the light via HA:

DEBUG (SyncWorker_12) [xiaomi_gateway] _send_cmd >> b'{"cmd": "write", "sid": "(removed for privacy)", "data": {"rgb": 1694498815, "key": "(removed for privacy)"}}'
DEBUG (SyncWorker_12) [xiaomi_gateway] _send_cmd resp << {'cmd': 'write_ack', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":1694498815,"illumination":1064,"proto_version":"1.1.2"}'}
DEBUG (SyncWorker_12) [xiaomi_gateway] write_ack << {'cmd': 'write_ack', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":1694498815,"illumination":1064,"proto_version":"1.1.2"}'}

Not sure what "key" is. Not the gateway key. 34 characters, a-f, 1-9, maybe more numbers/letters possible.

Switching the light off (that was turned on in HA) via Mi Hub app:

DEBUG (Thread-2) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":0,"illumination":1069}'}
DEBUG (Thread-2) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":0,"illumination":996}'}

Switching the light on via Mi Hub app:

DEBUG (Thread-2) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":1694498815,"illumination":769}'}
DEBUG (Thread-2) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":1694498815,"illumination":769}'}
DEBUG (Thread-2) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":1694498815,"illumination":800}'}

Switching the light off via Mi Hub app:

DEBUG (Thread-2) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":0,"illumination":913}'}
DEBUG (Thread-2) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":0,"illumination":869}'}
DEBUG (Thread-2) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '(removed for privacy)', 'short_id': 0, 'data': '{"rgb":0,"illumination":814}'}

Those MCAST reports always come in groups of 2-3.

Reporting this to my best knowledge and intentions. Please don't close the report with a one-liner etc. In case suggestions are given I will test and report back.

Bug started as topic: https://community.home-assistant.io/t/xiaomi-gateway-light-unavailable-after-exactly-2-5-hours/89397

h4xxxx commented 5 years ago

I seem to be having the very same issue. I have tried 2 setups - hass.io on RPi 3b+ and Windows based on Python virtual env., both with exacly the same symptoms:

What I checked

  1. Gateway ports are open
    
    Starting

Nmap 7.70 ( https://nmap.org ) at 2019-01-28 21:09 Central European Standard Time Nmap scan report for 192.168.88.220 Host is up (0.0020s latency). PORT STATE SERVICE 9898/udp open monkeycom MAC Address: REDACTED (Unknown) Nmap done: 1 IP address (1 host up) scanned in 2.08 seconds



2. The multicast packets, including heartbeat from the gateway, appear to be reaching the machine - example wireshark output
`
"cmd":"heartbeat","model":"gateway","sid":"04cf8c743f08","short_id":"0","token":"EQgoaEYDzNX66AJY","data":"{\"ip\":\"192.168.88.220\
`
Example home assitant log output for gateway state change:
`
2019-01-26 17:14:20 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '04cf8c743f08', 'short_id': 0, 'data': '{"rgb":0,"illumination":386}'}
`

3. Tried generating the gateway key from both ios and android apps - no difference
4. The gateway and the hass.io instance are on the same subnet (gateway is wireless while hass is on lan but multicast packets seem to arrive without issue.

4. Using lumi.gateway.v3. hw_ver=MW300, fw_ver 1.4.1_164

**Things I plan to check**

Connecting hass.io through the same wireless SSID instead of LAN
Connecting hass.io through a different router

Any and all help would be appreciated 
delorean-dev commented 5 years ago

I have the "same" issue, after some hours i have this error: ERROR (SyncWorker_15) [xiaomi_gateway] Got error element in data {"error":"Invalid key"}

if i restart home assistant, its ok no problem, after some hours again error, 8h 12h .. But in Mi home appp, its working fine always.

gorpet commented 5 years ago

Same here, so I presume this is not a isolated case as the same issues (all of them) described very well by @SaturnusDJ happens to all of us.

JoseFerre commented 5 years ago

I have the same incidence.

Light not working fine, the swith moves to turned off a few seconds after I turned on and the light stays on while the swith is off. There is no way to turn it off and no color wheel for the gateway light.

After 2.5 hours more or less, the lights are not available but if I reboot HA service, the light appears available and go back to work 2.5 hours more.

As @SaturnusDJ says, all the time the other sensors work absolutely perefectly.

dbfort commented 5 years ago

Same. The gateway changes its status to unavailable after 2.5 hours (parameter in TIME_TILL_UNAVAILABLE). The switch returns to its original state. At the same time, all reports and heartbeats are visible in the logs.

2019-02-01 23:12:54 DEBUG (SyncWorker_6) [xiaomi_gateway] _send_cmd >> b'{"cmd": "write", "sid": "4cf8c7444ca", "data": {"rgb": 1694465280, "key": "c387601cea4c643a2f6a7f2be18854bd"}}'
2019-02-01 23:12:54 DEBUG (SyncWorker_6) [xiaomi_gateway] _send_cmd resp << {'cmd': 'write_ack', 'model': 'gateway', 'sid': '4cf8c7444ca', 'short_id': 0, 'data': '{"rgb":1694465280,"illumination":398,"proto_version":"1.1.2"}'}
2019-02-01 23:12:54 DEBUG (SyncWorker_6) [xiaomi_gateway] write_ack << {'cmd': 'write_ack', 'model': 'gateway', 'sid': '4cf8c7444ca', 'short_id': 0, 'data': '{"rgb":1694465280,"illumination":398,"proto_version":"1.1.2"}'}
2019-02-01 23:12:54 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '04cf8c7444ca', 'short_id': 0, 'data': '{"rgb":1694465280,"illumination":398}'}
2019-02-01 23:12:55 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '04cf8c7444ca', 'short_id': 0, 'data': '{"rgb":1694465280,"illumination":415}'}
MarcoBischero commented 5 years ago

My gw doens't beome unavailable, but it doesn't report the actual state.

When I turn on the miHubLight switch the light switch on, but the switch in HA remain off 2019-02-02 23:32:40 DEBUG (SyncWorker_1) [xiaomi_gateway] _send_cmd >> b'{"cmd": "write", "sid": "4cf8c913328", "data": {"rgb": 1679622033, "key": "4c399bb98e4792551a52595b99cb3d29"}}' 2019-02-02 23:32:40 DEBUG (SyncWorker_1) [xiaomi_gateway] _send_cmd resp << {'cmd': 'write_ack', 'model': 'gateway', 'sid': '4cf8c913328', 'short_id': 0, 'data': '{"rgb":1679622033,"illumination":281,"proto_version":"1.1.2"}'} 2019-02-02 23:32:40 DEBUG (SyncWorker_1) [xiaomi_gateway] write_ack << {'cmd': 'write_ack', 'model': 'gateway', 'sid': '4cf8c913328', 'short_id': 0, 'data': '{"rgb":1679622033,"illumination":281,"proto_version":"1.1.2"}'} 2019-02-02 23:32:41 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '04cf8c913328', 'short_id': 0, 'data': '{"rgb":1679622033,"illumination":282}'} 2019-02-02 23:32:41 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'gateway', 'sid': '04cf8c913328', 'short_id': 0, 'data': '{"rgb":1679622033,"illumination":290}'}

and the only way to report the correct status is to change the name of the light (light.gateway_xxx)

If I change the status from the MI App, the status doesn't reflect on HA. I think I have the same issue

Latest version of HA and mi gateway lumi.v3 no sensors attached. Hassio on Raspberry

Could be something related to multicast? I've a fritzbox. Let me know if you need other checks

peppekerstens commented 5 years ago

HA: hassio v0.84.6

I have been at this exact same problem about two weeks now. Reading numerous post about this and related issues.

Hardware & versions

I have 3 xiaomi gateways. All of them the 'new' model (round letters/info on the back). But 2 different hardware series/runs (or so it seems). This difference is clear in/as the first 6 digits of the MAC address are different.

Of these 2 types:

Network & app & ports etc etc

All Xiaomi gateways are on the same network and connected via the same network devices as the HA setup is. As one of the gateways remains available; i claim that this rules out any network related issues (in my case/setup).

All of the gateways are registered and present/available in the Mi Home app. As i understand, communication via this app is based on different ports/protocols as the HA extension.

Little history and probable cause

My OLD gateway was the first i bought, deciding whether to invest more in a Xiaomi/HA setup. After successful automating by a 100% copy of https://www.home-assistant.io/components/binary_sensor.xiaomi_aqara/#motion, i decided to buy the NEW gateways (as well as more sensors).

When they arrived, i upgraded both the firmware of the OLD as well as the NEW as well as HA itself (from 0.80-ish). This was (of course) a mistake as i can not pinpoint the root-cause of current behavior. Silly me, should have known better...

But it strikes me that @SaturnusDJ is using older Xiaomi firmware (see: https://community.home-assistant.io/t/xiaomi-gateway-light-unavailable-after-exactly-2-5-hours/89397), and still has the exact same behavior.

Current setup

As this is an ongoing investigation:

  1. the OLD gateway is running as initially configured for a few weeks now, running the 'motion' automation as described earlier.
  2. one NEW gateway is disconnected/excluded from HA completely, and only connected to Mi Home app. Using the 'lighting' build-in automation of Mi Home, it runs the 'night light' mode (only).
  3. one NEW gateway is configured/present in HA. Running no automations. Having Xiaomi PIR's and a smoke detector connected. This one is modded for a direct UART connect as described in https://community.openhab.org/t/solved-openhab2-xiaomi-mi-gateway-does-not-respond/52963/24

I logged several hours of serial output yesterday with 3; finding no indication whatsoever as why communication with this gateway fails. That being said; i do not know what to look for.

When I add/enable automation on 3 (exact same as 1), it runs just fine...until the unit becomes unavailable. So it is possible to adjust the color (mine; yellow) and intensity just fine grammatically. The HA interface issues mentioned by @SaturnusDJ remain...

It also strikes me that all gateways seem to reboot now and then (not responding, blue blinking 3 times and returning to normal responding after a few minutes). But frequency seems erratic and does not correspond with the issue at hand; after 2,5 hours loosing the availability status - the OLD gateway remains available in HA, despite of this reboot/stability issue. This was also 1 of my reason for above test setup; determine of HA connection was the cause of this instability. Not so; number 2 also reboots...

As others said; in case suggestions are given I will test and report back.

SaturnusDJ commented 5 years ago

Thanks for your posts guys. Clearly something going on.

I started using HA/Xiaomi Aqara after the gateway light/sensor stopped working, so I have no idea of how the interaction was going at the time it still worked. Anyone having old communication data is welcome to post, so we can investigate possible differences.

Concerning my versions... Since 2 days I am running HA 0.86.4 and for about 2 weeks Mi Hub firmware 1.4.1_164.0158. Situation with the gateway light/sensor is unchanged.

MarcoBischero commented 5 years ago

I’m suffering the 2.5h issue too!!

Other evidence that maybe can help:

Also After 2.5h when the gateway become unavailable if I change the name of the light, the gateway start working again, but always with the same problem of not registering the state after switch on/off the light

peppekerstens commented 5 years ago

related issue: https://github.com/home-assistant/home-assistant/issues/20383

Waexu commented 5 years ago

Same for me. Tried domoticz - everything works fine, so the problem is definitely with HA.

tehstevo commented 5 years ago

I'm also having this issue. HA 0.84.6 - Docker Hassio on a NUC Hub Firmware 1.4.1_150.0143

westfa commented 5 years ago

Also having the the issue whereby the gateway becomes unavailable, but the attached sensors keep functioning.

Issue: I can also turn on the gateway light via HA, but I cannot turn it off again via HA. Also after a second or so, the switch in HA reverts back to the off state. When I turn on the light via the Mi home app, the state in HA doesn't change. This is on the NEW version (round text on the back) of the gateway. The firmware version is 1.4.1_164.0158. I have another gateway (with square text) and that one works fine (f/w 1.4.1_161.0143). I read on a forum (can't remember which one) that if you have the NEW gateway and you enable LAN access before upgrading to the latest firmware and after it's enabled upgrade the firmware, everything works as expected. Unfortunately, I don't have another gateway to confirm that.

Troubleshooting: Reading a post on another forum (https://community.openhab.org/t/solved-openhab2-xiaomi-mi-gateway-does-not-respond/52963/113), I confirmed that udp port 9898 was closed. On my "old" working gateway, the port was open. I opened up the case and soldered the rx, tx and gnd wires to connect the gateway via serial interface. Following the instructions, I managed to open port 9898. Unfortunately, this did not fix the issue. The behavior is slightly different now though. I can still only turn on the light, but now the switch stays in the 'on' position in HA. After 2,5 hours the gateway still becomes unavailable again. After this, I tried:

Additional info: When still connected to the serial interface via Putty, I noticed that when I turn the gateway light on via HA, it registers the command ({“id”:52,“method”:“props”,“params”:{“rgb”:1694498815}}. But when I turn it off, no command registers in the terminal window. When I turn it on via the Mi Home app, it displays a different message “{“id”:56,“method”:“props”,“params”:{“light”:“on”,“from.light”:“4,”}}” When I turn it off in the MiHome app, it shows “{“id”:54,“method”:“props”,“params”:{“light”:“off”,“from.light”:“4,”}}”.

SaturnusDJ commented 5 years ago

@Waexu @westfa Is it possible to find out which data (tcpdump?) is sent and interpreted in the cases when the gateway light/illumination is working correctly?

I immediately enabled LAN access on my initial firmware (1.4.1_150.0143) but the light and illumination never worked as expected in HA.

advokatb commented 5 years ago

Same problem for me. It appeared after update to 0.86.x

andriej commented 5 years ago

Guys, you might be laughing and I'm still investigating if it's true, but I have 3 gateways (see #20383) and I've recently moved do hass.io in docker. First of all what has started was problems with gateways. What I did is to REMOVE host:, leaving only KEY and mac address.

Had no issues since 2 days.

Waexu commented 5 years ago

Guys, you might be laughing and I'm still investigating if it's true, but I have 3 gateways (see #20383) and I've recently moved do hass.io in docker. First of all what has started was problems with gateways. What I did is to REMOVE host:, leaving only KEY and mac address.

Had no issues since 2 days.

Didn't help for me

mwrobel80 commented 5 years ago

Hi,

I'm using 2 gateways and one have a very similiar problem. I can change the state of the gateway light only once, after one change I can switch the light "on" and "off" but nothing is happening. Additionaly when I'm using service "light.turn_on" or "light.turn_off" I can turn the light on and off without a problem. This is how it looks like https://www.youtube.com/watch?v=N__FQoCGhMI

MarcoBischero commented 5 years ago

My d/w sensors finally arrived.

While the gateway is “unavailable” on HA, status from sensors still arrive.

The problem is restricted to gateway light/lumen sensor.

BR

andriej commented 5 years ago

I have the same problem (no on/off on gateway light), although I have two chances of failure. sometimes when gateways go down, movement and door sensors are also out or they work even when I can't change gateway light mode.

stsergo commented 5 years ago

I've posted fix here at community post.

I've checked it on 1.4.1_164.0158 firmware and HA 0.87.0. Should work on 0.87.1 My HA installation online 2 days+ and gateway light and sensor works as expected.

Don't forget to backup files! Right now I have no other xiaomi sensors, so can't check. But I hope they should work as usual. Could someone check?

  1. Enable polling.

Find this code in the xiaomi_aquara component (xiaomi_aquara.py) In my venv it's located here at /opt/homeassistant/lib/python3.6/site-packages/homeassistant/components/

    @property
    def should_poll(self):
        """Return the polling state. No polling needed."""
        return False

And change False to True

    @property
    def should_poll(self):
        """Return the polling state. No polling needed."""
        return True
  1. Illumination sensor still does not change the value, no matter we turn light or not. Let's add update def for sensor. File is nearby: ./sensor/xiaomi_aquara.py
    def update(self):
        """Get data from hub."""
        _LOGGER.debug("Update data from hub: %s", self._name)
        self._get_from_hub(self._sid)
  1. Next code is for light ./light/xiaomi_aquara.py

This might not needed, but it's quick and dirty :)

Add this to turn_on and turn_off defs:

self.schedule_update_ha_state()

Result should be like this:

    def turn_on(self, **kwargs):
        """Turn the light on."""
        if ATTR_HS_COLOR in kwargs:
            self._hs = kwargs[ATTR_HS_COLOR]

        if ATTR_BRIGHTNESS in kwargs:
            self._brightness = int(100 * kwargs[ATTR_BRIGHTNESS] / 255)

        rgb = color_util.color_hs_to_RGB(*self._hs)
        rgba = (self._brightness,) + rgb
        rgbhex = binascii.hexlify(struct.pack('BBBB', *rgba)).decode("ASCII")
        rgbhex = int(rgbhex, 16)

        if self._write_to_hub(self._sid, **{self._data_key: rgbhex}):
            self._state = True
            self.schedule_update_ha_state()

    def turn_off(self, **kwargs):
        """Turn the light off."""
        if self._write_to_hub(self._sid, **{self._data_key: 0}):
            self._state = False
            self.schedule_update_ha_state()
  1. Restart HA and wait 2.5 hours :)
  2. Check traffic by ngrep. You should see read and read_ack every 30 sec.
westfa commented 5 years ago

@stsergo: is this without opening port 9898? And does the 'old' version of the gateway still work?

stsergo commented 5 years ago

@westfa Yep. Gateway just out of box:

nmap -sUT -p 9898 lumi.home.lan|grep 9898
9898/tcp closed monkeycom
9898/udp open   unknown

I don't know about old version, i have only one :(

MarcoBischero commented 5 years ago

Hi guys, Good to know!

I run HA in Hass.io on raspberry, how can I fix this? Custom_components?

Il giorno gio 14 feb 2019 alle 20:59 stsergo notifications@github.com ha scritto:

@westfa https://github.com/westfa Yep. Gateway just out of box:

9898/tcp closed monkeycom 9898/udp open unknown```

I don't know about old version, i have only one :(

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/home-assistant/home-assistant/issues/20329#issuecomment-463771086, or mute the thread https://github.com/notifications/unsubscribe-auth/AMkAkhSCiObTlsaRJZ2rfk-0mDBd4XoTks5vNcAtgaJpZM4aNpMv .

andriej commented 5 years ago

For me:


9898/tcp closed monkeycom
9898/udp open   monkeycom
# nmap -sUT -p 9898 192.168.x.x2 |grep 9898
9898/tcp closed monkeycom
9898/udp open   monkeycom
# nmap -sUT -p 9898 192.168.x.x3 |grep 9898
9898/tcp closed monkeycom
9898/udp open   monkeycom
andriej commented 5 years ago

@stsergo - what's exactly that it suppose to fix? why it's not yet PR-ed to repo? :-)

stsergo commented 5 years ago

@andriej

what's exactly that it suppose to fix?

why it's not yet PR-ed to repo? :-)

Just don't know how :( Also fix might broke something, and I doesn't have other sensors and other versions of gateways for check

andriej commented 5 years ago

I'd test it out, just can't find files as I use hass.io in docker.

dbfort commented 5 years ago

@stsergo great job. it works great. Thank you very much.

Я бы проверил это, просто не могу найти файлы, так как я использую hass.io в докере.

/usr/src/app/homeassistant/components/xiaomi_aqara.py /usr/src/app/homeassistant/components/sensor/xiaomi_aqara.py /usr/src/app/homeassistant/components/light/xiaomi_aqara.py

andriej commented 5 years ago

/usr/src/app/homeassistant/components/xiaomi_aqara.py /usr/src/app/homeassistant/components/sensor/xiaomi_aqara.py /usr/src/app/homeassistant/components/light/xiaomi_aqara.py

No paths inside docker :-/ I log in through portainer and there's no path /usr/src/app

Nvm, switched back to venv just to test it out.

andriej commented 5 years ago

So far not even one problem for 24h with 3 gateways!

MarcoBischero commented 5 years ago

How do you edited the xiaomi component in Hassio?

Il giorno sab 16 feb 2019 alle 01:10 Andrzej notifications@github.com ha scritto:

So far not even one problem for 24h with 3 gateways!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/home-assistant/home-assistant/issues/20329#issuecomment-464256550, or mute the thread https://github.com/notifications/unsubscribe-auth/AMkAks0Zd6R9Ys0QGvmBA0gCZkuo8lHPks5vN0xhgaJpZM4aNpMv .

andriej commented 5 years ago

I moved from Hass.io to venv back just to test this. Will make PR as it seem to help a lot (so far no problems still) so everyone can see difference since next version it will be merged.

andriej commented 5 years ago

@stsergo or it would be better if you do it. :-) as it's really your fix

stsergo commented 5 years ago

Oh, nice!

Ok, I'll try to do PR

andriej commented 5 years ago

After next restarts they became unavailable again, but will investigate further. EDIT: My problem seems to be connected with something different (network?), fix seems to work well. :-)

mwrobel80 commented 5 years ago

@stsergo , thank you. Your fix is working very well in my env. I'm using hassio in docker so additionaly I had to commit all the changes to docker image after update.

zatarra77 commented 5 years ago

Hello, only to report that I have the same issue. Should I reach the components to modify on hassio? how? Ssh?

mwrobel80 commented 5 years ago

@zatarra77 , yes you should ssh and modify the xiaomi_aqara.py files according to fix provided by @stsergo

SaturnusDJ commented 5 years ago

@stsergo Thanks for the effort to solve the problem here and in the PR. Since the gateway light/illumination is not 'mission critical' here, I am a bit laid back, but nice to see this problem is getting picked up by you and others.

sonartribe commented 5 years ago

I have a theory...One thing I have noticed, and including myself, is that the people who appear to have most problems all have MAC address of the gateway starting 04:XX:XX:XX:XX:XX.

When I get updates from a sensor, I can see in the debug these appearing as a PUSH and the sensor gets updated.

2019-02-24 20:24:33 DEBUG (Thread-23) [xiaomi_gateway] MCAST (heartbeat) << {'model': 'sensor_ht', 'sid': '158dXXXXXXXXXX', 'cmd': 'heartbeat', 'short_id': 63981, 'data': '{"voltage":2985,"
temperature":"1845","humidity":"5269"}'}
2019-02-24 20:24:33 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Humidity_158dXXXXXXXXXX: 52.7>: {'voltage': 2985, 'temperature': '1845', 'humidity': '5269'}
2019-02-24 20:24:33 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Temperature_158dXXXXXXXXXX: 18.4>: {'voltage': 2985, 'temperature': '1845', 'humidity': '5269'
}

However when I get an illumination update from the gateway I don't get the corresponding PUSH debug message.

2019-02-24 20:20:27 DEBUG (Thread-23) [xiaomi_gateway] MCAST (report) << {'model': 'gateway', 'sid': '04XXXXXXXXXX', 'cmd': 'report', 'short_id': 0, 'data': '{"rgb":1694434079,"illumination
":336}'}

One thing I notice that the entity_id that home assistant set up, is

sensor.illumination_4XXXXXXXXXX and light.gateway_light_4XXXXXXXXXX

thus missing the leading zero.

Could this be causing a mismatch between the SID being broadcast and the HA entitiy?

sonartribe commented 5 years ago

Okay. Bit more digging, looks like during the discovery phase, the gateway drops this 0 in its response as seen in this nmap trace...

U 192.168.1.5:33094 -> 192.168.1.71:9898
  {"cmd":"read","sid":"04xxxxxxxxxx"}                                                                                                              
#
U 192.168.1.71:9898 -> 192.168.1.5:33094
  {"cmd":"read_ack","model":"gateway","sid":"4xxxxxxxxxx","short_id":0,"data":"{\"rgb\":1694498815,\"illumination\":520,\"proto_version\":\"1.1.2\"
  }"}                                                                                                                                              
#

To work around this issue, I modified the xiaomi_gateway init.py in /srv/homeassistant/lib/python3.5/site-packages/xiaomi_gateway on my system. In the following snippet I changed

"sid": resp["sid"], to "sid": resp["sid"].rjust(12, '0'),

to pad an extra 0 back to the start of the SID.

            for device_type in device_types:
                if model in device_types[device_type]:
                    supported = True
                    xiaomi_device = {
                        "model": model,
                        "proto": self.proto,
                        "sid": resp["sid"].rjust(12, '0'),
                        "short_id": resp["short_id"] if "short_id" in resp else 0,
                        "data": _list2map(_get_value(resp)),
                        "raw_data": resp}
                    self.devices[device_type].append(xiaomi_device)

now I see the PUSH messages, which according to the code will also reset the last seen item and hence stop the timeouts.

2019-02-24 21:52:09 DEBUG (Thread-23) [xiaomi_gateway] MCAST (report) << {'sid': '04xxxxxxxxxx', 'model': 'gateway', 'cmd': 'report', 'data': '{"rgb":0,"illumination":503}', 'short_id': 0}
2019-02-24 21:52:09 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Illumination_04xxxxxxxxxx: 223>: {'rgb': 0, 'illumination': 503}
2019-02-24 21:52:09 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Gateway Light_04xxxxxxxxxx: on>: {'rgb': 0, 'illumination': 503}

Can somebody who know what they're doing add this into the codebase.

SaturnusDJ commented 5 years ago

@sonartribe Work for me too. Nice work. All of the mentioned problems are solved, except that 'sometimes' (still investigating) the button on the gateway does not work to turn on the light. After turning it on with HA (and maybe Mi Home app too) the button works again to turn off/on for a while.

syssi commented 5 years ago

If the MAC address of your Xiaomi Gateway starts with '04' please update HA to 0.88.2 because of: https://github.com/home-assistant/home-assistant/pull/21453 Some people here are affected. Some may be not.

JoseFerre commented 5 years ago

@syssi Work for me. Thanks!!

scott-ip1 commented 5 years ago

Working for me as well. Light are good and report status properly, and I'm past the 2.5 hour mark without them losing connection.

peppekerstens commented 5 years ago

updated to hassio 0.90 (and also one of my Xiaomi gateways is on latest firmware):

  1. no more failures after 2,5hours
  2. HA interface/status responds properly
  3. did/could NOT check color issue(s)

1 and 2 seems properly fixed now, somebody confirm 3? i do/did not have that issue...

SaturnusDJ commented 5 years ago

Yes, all fixed since 0.88.2. Button on Mi Hub not always working here. Probably something else. For new and other problems, create a new topic.