leeyuentuen / localtuya

local handling for Tuya devices
GNU General Public License v3.0
72 stars 17 forks source link

Devices/Funtions dissappear from HA after gateway restart. #23

Closed tony-butchart closed 1 year ago

tony-butchart commented 1 year ago

I'm using a Tuya Bluetooth WiFi gateway along with some Bluetooth light globes. I have the lights paired up fine with the gateway and all functions work fine through the Tuya app as expected. I had some trouble getting the lights connected to HA using localtuya initially which I resolved by using the app to set Brightness/Colour Temp on the light, strangely after which it was controllable using HA/localtuya.

This issue occurs every time the gateway itself is restarted, I did some poking with tuya-cli and what appears to be happening is that the gateway is not returning any data on certain DPs after being re-powered, for example polling dp 2 (colour mode) and dp 4 (colour temp) after the gateway has been restarted:

~$ tuya-cli get --id bf370a8743a1afde73zoed --key 1234567898 --cid 0070 --dps 4 --protocol-version 3.3
undefined
$ tuya-cli get --id bf370a8743a1afde73zoed --key 1234567898 --cid 0070 --dps 2 --protocol-version 3.3
undefined

If i use tuya-cli to set the colour mode and the colour temp the data is retained and is retrievable afterwards, and further more the device becomes controllable via localtuya:

~$ tuya-cli set --set 276 --id bf370a8743a1afde73zoed --key 1234567898 --dps 4 --protocol-version 3.3 --cid 0070
Set succeeded.
~$ tuya-cli get --id bf370a8743a1afde73zoed --key 1234567898 --cid 0070 --dps 4 --protocol-version 3.3
276
~$ tuya-cli set --set white --id bf370a8743a1afde73zoed --key 1234567898 --dps 2 --protocol-version 3.3 --cid 0070
Set succeeded.
~$ tuya-cli get --id bf370a8743a1afde73zoed --key 1234567898 --cid 0070 --dps 2 --protocol-version 3.3
white

Here's the error reported when the issue is occurring:

This error originated from a custom integration.

Logger: homeassistant
Source: custom_components/localtuya/light.py:218
Integration: LocalTuya (documentation, issues)
First occurred: 9:42:57 PM (4 occurrences)
Last logged: 9:42:57 PM

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 532, in async_update_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 585, in _async_write_ha_state
    attr.update(self.state_attributes or {})
  File "/usr/src/homeassistant/homeassistant/components/light/__init__.py", line 986, in state_attributes
    color_mode = self._light_internal_color_mode
  File "/usr/src/homeassistant/homeassistant/components/light/__init__.py", line 819, in _light_internal_color_mode
    if ColorMode.COLOR_TEMP in supported and self.color_temp_kelvin is not None:
  File "/usr/src/homeassistant/homeassistant/components/light/__init__.py", line 868, in color_temp_kelvin
    if self._attr_color_temp_kelvin is None and self.color_temp:
  File "/config/custom_components/localtuya/light.py", line 218, in color_temp
    ((self._max_mired - self._min_mired) / self._upper_color_temp)
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

I also see a number of these sorts of error in the logs:

2022-12-26 21:42:54.795 WARNING (SyncWorker_2) [custom_components.localtuya.light] [00b...0b8] Entity light.bedroom_light_2 is requesting unknown DPS index 5
2022-12-26 21:42:54.795 WARNING (SyncWorker_1) [custom_components.localtuya.light] [00c...0c0] Entity light.bedroom_light_3 is requesting unknown DPS index 5
2022-12-26 21:42:54.802 WARNING (SyncWorker_0) [custom_components.localtuya.light] [006...060] Entity light.bedroom_light_1 is requesting unknown DPS index 4
2022-12-26 21:42:54.802 WARNING (SyncWorker_2) [custom_components.localtuya.light] [00b...0b8] Entity light.bedroom_light_2 is requesting unknown DPS index 4
2022-12-26 21:42:54.803 WARNING (SyncWorker_1) [custom_components.localtuya.light] [00c...0c0] Entity light.bedroom_light_3 is requesting unknown DPS index 4
2022-12-26 21:42:54.804 WARNING (SyncWorker_0) [custom_components.localtuya.light] [006...060] Entity light.bedroom_light_1 is requesting unknown DPS index 2
2022-12-26 21:42:54.805 WARNING (SyncWorker_2) [custom_components.localtuya.light] [00b...0b8] Entity light.bedroom_light_2 is requesting unknown DPS index 2
2022-12-26 21:42:54.805 WARNING (SyncWorker_2) [custom_components.localtuya.light] [00b...0b8] Entity light.bedroom_light_2 is requesting unknown DPS index 2

This is the only gateway device i have to test with so it's unclear to me whether the return of null values is normal behavior for gateway devices and is expected to be handled by localtuya or whether that is the cause of the issue. I know once the initial problem is solved as long as the gateway stays on continuously it seems to retain the DP data and remains in a working state within home assistant.

Happy to do additional testing or logging if its needed for further information, just let me know.

Note: I've had to add my gateway via yaml as adding via the GUI results in a "Failed to authenticate with device. Verify that device id and local key are correct." error and the corresponding error in the log file:

Traceback (most recent call last):
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 696, in detect_available_dps
    status = await self.status()
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 596, in status
    status = await self.exchange(ACTION_STATUS)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 570, in exchange
    payload = self._decode_payload(msg.payload)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 782, in _decode_payload
    return json.loads(payload)
  File "/usr/local/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
leeyuentuen commented 1 year ago

is this happened on every version of localtuya? or did it works on previous version?

tony-butchart commented 1 year ago

is this happened on every version of localtuya? or did it works on previous version?

The two releases I have tested are 3.6.1 and 3.6.2-beta.3. The same issue occured on both.

Is it worth testing prior versions to 3.6.1?

leeyuentuen commented 1 year ago

ow wait, i see this. i'll take a look

  File "/config/custom_components/localtuya/light.py", line 218, in color_temp
    ((self._max_mired - self._min_mired) / self._upper_color_temp)
TypeError: unsupported operand type(s) for *: 'float' and '
leeyuentuen commented 1 year ago

can you check if this beta 4 solved this problem? 3.6.2-beta.4

tony-butchart commented 1 year ago

Just gave it a test, restarted the gateway and restarted my HA instance after installing. I still get all lights offline but a different error now:

This error originated from a custom integration.

Logger: homeassistant
Source: custom_components/localtuya/light.py:97
Integration: LocalTuya (documentation, issues)
First occurred: 11:39:05 AM (12 occurrences)
Last logged: 11:39:05 AM

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 532, in async_update_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 585, in _async_write_ha_state
    attr.update(self.state_attributes or {})
  File "/usr/src/homeassistant/homeassistant/components/light/__init__.py", line 1004, in state_attributes
    data[ATTR_BRIGHTNESS] = self.brightness
  File "/config/custom_components/localtuya/light.py", line 189, in brightness
    return map_range(
  File "/config/custom_components/localtuya/light.py", line 97, in map_range
    mapped = (value - from_lower) * (to_upper - to_lower) / (
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

Here's the full debug log as well if that helps: home-assistant_2022-12-31T00-44-00.467Z.log

Here's output from tinytuya directly on the status of a Bulb before restarting the gateway:

>>> d.detect_available_dps()
{'1': True, '2': 'colour', '3': 50, '4': 1000, '5': '0000019B0006'}

And what it looks like straight after restarting the GW:

>>> d.detect_available_dps()
{'1': True}
leeyuentuen commented 1 year ago

have a try on a newer version? 3.6.2-beta.5

tony-butchart commented 1 year ago

Initial testing looks like the issue is resolved, thanks! I'll spend some more time with it today checking all the functions.

leeyuentuen commented 1 year ago

Initial testing looks like the issue is resolved, thanks! I'll spend some more time with it today checking all the functions.

nice, if everything goes well, the i will put this beta to release. unless there are more issue came up

felipecrs commented 1 year ago

I have one feedback related to _gateway_s: when setting up a gateway from UI, some text boxes have no hint:

image

But the main problem is that, even when enabling Is Gateway option, the next page will still ask me to setup entities and there's no way to skip:

chrome_UlYssGgUa4

leeyuentuen commented 1 year ago

I have one feedback related to _gateway_s: when setting up a gateway from UI, some text boxes have no hint:

image

But the main problem is that, even when enabling Is Gateway option, the next page will still ask me to setup entities and there's no way to skip:

chrome_UlYssGgUa4 chrome_UlYssGgUa4

nice catch, I'll create a new issue to fix this.