core: New Tuya device not shown - due to brightness / color conflict
Home Assistant release with the issue:
0.101.3
Last working Home Assistant release (if known):
Operating environment (Hass.io/Docker/Windows/etc.):
manual install on raspberry-pi4/buster Integration:
https://www.home-assistant.io/integrations/tuya/
Description of problem:
Problem-relevant configuration.yaml
entries and (fill out even if it seems unimportant):
tuya:
username: xxxxxxx
password: xxxxxxx
country_code: 49
platform: smart_life
Traceback (if applicable):
Additional information: OK, please be kind - I’m new to all this (HA, python, git,…), did a lot of coding (e.g. perl), but never considered myself a developper nor do I know a lot about collaboration - hoping to get this right: I have a new tuya device (LED controller Model AM-WIFI-002 is all it says) set up w/ Smart Life. I do already have fully functional tuya integrations running that way. That new device did not show up in the frontend - checking the logs gave errors like:
Nov 7 08:57:01 hom-e-server hass[17315]: 2019-11-07 08:57:01 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Nov 7 08:57:01 hom-e-server hass[17315]: Traceback (most recent call last):
Nov 7 08:57:01 hom-e-server hass[17315]: File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 275, in async_update_ha_state
Nov 7 08:57:01 hom-e-server hass[17315]: self._async_write_ha_state()
Nov 7 08:57:01 hom-e-server hass[17315]: File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 316, in _async_write_ha_state
Nov 7 08:57:01 hom-e-server hass[17315]: attr.update(self.state_attributes or {})
Nov 7 08:57:01 hom-e-server hass[17315]: File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/light/__init__.py", line 477, in state_attributes
Nov 7 08:57:01 hom-e-server hass[17315]: data[ATTR_BRIGHTNESS] = self.brightness
Nov 7 08:57:01 hom-e-server hass[17315]: File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tuya/light.py", line 43, in brightness
Nov 7 08:57:01 hom-e-server hass[17315]: if self.tuya.brightness() is None:
Nov 7 08:57:01 hom-e-server hass[17315]: File "/srv/homeassistant/lib/python3.7/site-packages/tuyaha/devices/light.py", line 16, in brightness
Nov 7 08:57:01 hom-e-server hass[17315]: brightness = int(self.data.get('color').get('brightness') * 255 / 100)
Nov 7 08:57:01 hom-e-server hass[17315]: AttributeError: 'NoneType' object has no attribute 'get'
Checking the code in /srv/homeassistant/lib/python3.7/site-packages/tuyaha/devices/light.py gives:
13 def brightness(self):
14 work_mode = self.data.get('color_mode')
15 if work_mode == 'colour':
16 brightness = int(self.data.get('color').get('brightness') * 255 / 100)
17 else:
18 brightness = self.data.get('brightness')
19 return brightness
I interpret this as - if we are in color_mode we get the brightness as a subproperty of color (and convert it) .
But on this new device color obviously is handled differently, since self.data.get('color')
is None.
As a workaround I fixed this like:
15 if work_mode == 'colour' and not (self.data.get('color') is None) :
Now at least the device shows up and I can control the brightness by the slider. However the color picker does not show up in lovelace, therefore I assumed above that this device needs different color handling than my other devices.
Could you kindly point me to how to dig further? e.g. How to find out which properties that device has? Maybe I can contribute more in “the right way”…
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 33 (4 by maintainers)
Here is my modified light.py from the Tuya component.
To use this, you will need to download the Tuya component from here and add the folder to your custom_components folder, replacing light.py with my modified version.
As a bonus, here is another version which also maps brightness levels if, like me, you have Tuya bulbs which only support brightness levels of 29-255 instead of the full range of 1-255 (setting the brightness to 28 or less turns the light off). This code maps brightness levels from to 0-255 range to the 29-255 range when setting brightness and vice versa when retrieving the brightness. The mapping isn’t exact, and sometimes the retrieved brightness will not quite match what you set it to (which I think is due to the Tuya API actually using percentage brightness) but I find it is close enough.
Any progress on this? Where can I donate money to get this fixed 😂
@G33kDude Thanks for that, that allowed me to to get it working without errors. The only issue being that Home Assistant can’t keep track of what the colour is set to.
I worked around this (in a rather hacky way) by downloading the Tuya component and adding it to the custom_components folder with the following changes to light.py
Change
from . import DATA_TUYA, TuyaDevice
tofrom . import DATA_TUYA, TuyaDevice, DOMAIN
Change the hs_color property to this:
and in the turn_on method change this:
to this:
This keeps track of the current colour in hass.data. This will obviously only work if you only control the lights through Home Assistant.
Welp, rather than bothering with patching, just replaced the light.py file with yours and things are honkey dory. Color changing works and they always show up too. Pretty good patch if you ask me 😃
I am running Hassio through Hassbian, so you will just need to ssh into it, then get inside your homeassistant container:
sudo docker exec -it homeassistant /bin/bash
Then navigate to/usr/local/lib/python3.7/site-packages/tuyaha/devices/
backup the light.py that’s there and replace the light.py with G33kDude’s above. Restart HA and you are all set!Copy of my notes from duplicate issue #30007, with a patched tuya api light file (light_patch.py) to re-enable color settings for tuya lights (though it force-shows color settings for all tuya lights whether they support color or not).
If you just need the color or brightness parameter, in your customize section just add something like this:
light.8859gf59329894c: supported_features: 49 That will add color palette to your light, for other setting look this post: https://community.home-assistant.io/t/device-supported-features/168499
Le lun. 6 juill. 2020, à 13 h 12, skynet01 notifications@github.com a écrit :
There is the Status API offered by tuya.
GET /v1.0/skills/devices/{device_identity}/status
I think this is the key to retrieve the current color value…
https://docs.tuya.com/en/iot/open-api/api-list/api/skill-service?id=K95zu0id1md0g
Hello, I got this reply from tuya about the color issue…
Copy to clipboard Hello, that’s because color_mode is based on work_mode [“white”, “colour”,] to get the value. The string of code I provided to you above is to get the color: { “header”: { “name”: “colorSet”, “namespace”: “control”, “payloadVersion”: 1 }, “payload”: { “color”: { “hue”: 0, “saturation”: 0, “brightness”: 1 }, “accessToken”: “token”, “devId”: “devId” } } Does it help to understand the issue?
I applied the fixes suggested here, but HA is not able to read the original status of the light and its color (it only stores the new color when changed from HA, but it’s not able to read it if changed from somewhere else…)
I’m not using hassio, but a manual installation, so the path is relative to your base directory (which I don’t know for hassio) - to get started: if you’re connected via ssh you can try installing
locate
and try to find the installation path by:If you already know the base directory you can
cd
there and tryfind tuyaha
or directlyfind <yourbasedirectory> tuyaha
(Hint: you might have to addsudo
in front of every command, depending on the user you are connected to.) For editing via samba-shares, … sorry, I don’t know the layout, but I assume the installation (and that’s what your looking for) is not shared there.