core: Xiaomi gateway light - can't turn off, no colour

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.

  • The switch of the gateway light in HA will move to turned off seconds after being turned on in HA, while the light itself actually did turn on and stays on.
  • No color wheel for the gateway light.
  • After 2,5 hours, HA gives up and with that also the illumination sensor values of the gateway are not processed anymore.
  • Remember: all the time the other sensors work absolutely perfectly with both the Mi Hub and HA.

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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 48 (11 by maintainers)

Most upvoted comments

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.

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.

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:

  • a factory reset of the gateway (by removing it from the Mi Home app and another time by holding down the button on the gateway)
  • Toggling the LAN mode
  • regenerating the LAN key on Android (5 times).

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,”}}”.