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.16k stars 29.85k forks source link

It seems that there are sth wrong with the xiaomi_aqara api about light #15005

Closed xcray closed 6 years ago

xcray commented 6 years ago

Home Assistant release with the issue:

0.71.0

Last working Home Assistant release (if known):

Operating environment (Hass.io/Docker/Windows/etc.):

Ubuntu Component/platform:

https://www.home-assistant.io/components/light.xiaomi_aqara/ https://www.home-assistant.io/components/xiaomi_aqara/

Description of problem:

As default, HA will discover and configure xiaomi_light automatically. But it seems that there are something wrong within the xiaomi_aqara LAN API. If I'd toggled on/off the gateway light, it's brightness/color will be reset to 100/red, and then it will not work properly in the official Mijia App. Even one of mijia app's automation excused successfully with the light turned on, it doesn't glitter at all. Until I'd modified the brightness/color in Mijia App, it will not recover to work. btw, I don't think this is a bug of HA, but of the official LAN API.

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

xiaomi_aqara:
  gateways:
    - key: xxxxxxxxxxxx

Traceback (if applicable):

Additional information: Un ugly hack is to disable the light in HA. If just set "disabled_by" in entity_registry.yaml (as metioned by HA here, this is a quite experimental feature) , HA will throw many errors saying:

2018-06-16 22:56:18 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 199, in async_update_ha_state
    "No entity id specified for entity {}".format(self.name))
homeassistant.exceptions.NoEntitySpecifiedError: No entity id specified for entity Gateway Light_34ce0091db83
2018-06-16 22:56:20 ERROR (MainThread) [homeassistant.core] Timer got out of sync. Resetting

So, I'd delete the word 'light' in the line 142 of lib/python3.6/site-packages/homeassistant/components/xiaomi_aqara.py. I know this is not the correct choice, but I don't know a better one just now.

Maybe future code of the xiaomi_aqara component/platform could handle this issue with more adaptability?

syssi commented 6 years ago

Which hardware version of the gateway do you own? Which firmware version do you use and could you provide some steps to reproduce the issue (initial brightness, color and state).

xcray commented 6 years ago

@syssi Thanks for your comment! hw_ver: MW300, model: lumi.gateway.v3, with the newest firmware: fw_ver 1.4.1_155. The wifi_fw_ver is SD878x-14.76.36.p84-702.1.0-WM, also the newest. steps to reproduce: 1, fresh default configurations for xiaomi_gateway integration (I also configured home-assistant-homebridge). 2, turn on and then off the gateway light using HA frontend, or Home App of iOS. 3, go back to Mijia App, click on the light, it will not glitter. 4, modify the brightness/color in Mijia App, then it recovers.

It seems that the brightness had been set to an illegal value after turning on & off via HA, and the color had been set to RED.

syssi commented 6 years ago

Please enable the debug output by

# configuration.yaml
logger:
  default: warn
  logs:
    xiaomi_gateway: debug
    homeassistant.components.xiaomi_aqara: debug
    homeassistant.components.light.xiaomi_aqara: debug

restart HA, set the brightness of the gateway light to 50%. Toggle the light some times to reproduce the issue and provide the home-assistant.log.

syssi commented 6 years ago

I could reproduce the bug (same firmware & device). It's a bug of the MiHome app / Xiaomi Cloud and cannot be improved here. Home Assistant is always aware of the correct state of the light. The MiHome app gets out of sync some times.

xcray commented 6 years ago

@syssi Thanks for your comments, I totally agree with you that it's a bug or defect of the gateway or it's interface or the app/cloud. It should have an API with a simple command just turn the light on/off, and another command to modify brightness/color and stored inside the GW. I'm not sure if we modify the default brightness/color in HA or PyXiaomiGateway to a lower value/yellow could make help.

syssi commented 6 years ago

In my case the color isn't the issue. Try to do something like this:

  1. Turn the light on by the MiHome app
  2. Change to color to red by the MiHome app
  3. Turn off the light by Home Assistant (The MiHome gets out of sync now)
  4. Turn on the light by HA
  5. Change the color to blue by HA (State and color is out of sync now at the MiHome app)
  6. Try to turn off the light by the MiHome App
  7. Try harder by MiHome app ;-)
  8. Sometimes the color indicator jumps to blue now
  9. Change the brightness (MiHome app) to get back the control and state about the light

Do you have a feeling for the issue?

Please try to identify edge cases. I'm aware of problems if the brightness is very low. In this case the light cannot turned on sometimes. Are there issues with some colors, too?

xcray commented 6 years ago

In my understanding, the LAN API command to control the light is just write RGB value to the GW, RGB=0xFF000000 means turn off while 0xFFFFFFFF means the maximum brightness and white color. Am I right? It seems that HA (& PyXiaomiGateway?) use 0xFFFFFFFF for the first time turning it on, which could cause this issue (I guessed). In my case: 1, I am using a motion sensor to adopt the gw light as a night-light, normally with a brightness of about 5% and orange color. 2, After HA configured, I use HA to turn the light on, it seems to have the maximum brightness and white color, so I guess HA/PyXiaomiGateway use the value of 0xFFFFFFFF. 3, Then I could found that the night-light will be out-of-service, but inside the log of Mijia App, it looks very well, will the automation executed successfully while I move around the motion sensor. 4, Then, in the Mijia App, I try to turn the light on, the icon of bulb indicated it's on, but in fact it's not. And the brightness indication in Mijia App is on the most of right (maximum brightness), color indication is on the bottom of left, which means white. 5, I adjust the brightness now, to any level lower than maximum, the color will jump to red, and then Mijia got back the control.

It seems that Mijia App store lasts glittered RGB value (not 0xFF000000) on the cloud (so different mobiles could sync each-other).

xcray commented 6 years ago

@syssi I'd just taken an experiment:

  1. I'd enabled HA's function of gw light and the logger just as you required.
  2. Once I'd control the light via HA, regardless of any brightness/color value, the Mijia App will lost the control of the light (not completely, the bulb icon could response to being clicked, the indications of brightness and color jumped to maximum/white, the light on the gw just can't glitter).
  3. Adjust the brightness in Mijia, the color jumped to red, then Mijia got the full control. HA could sync the state/brightness/rgb properly.
  4. If I tried control the light in HA, without adjusting the RGB, the bug appeared again. This is the log, I believe you can get the same result on your HA host. home-assistant.log

So, to keep the function of night-light available, I have to disable the light component in "lib/python3.6/site-packages/homeassistant/components/xiaomi_aqara.py".

syssi commented 6 years ago

You could hide the entity by "customize". This should work out of the box.

xcray commented 6 years ago

@syssi Maybe miio interface is a better choice to control gw's light, would u pls consider this?

xcray commented 6 years ago

@syssi It's a regret that hiding in customize can't help to this issue. I'd tried hide it and never tough it in HA, the MiHome (Mijia) App also could lost control of it. So, I have to modify the source code.