rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.84k stars 545 forks source link

Error while setting up switch #76

Closed 9myzwb4eh closed 3 years ago

9myzwb4eh commented 3 years ago

I am seeing the below error in log after adding config in yaml file.

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 231, in async_setup
    result = await component.async_setup_entry(hass, self)  # type: ignore
  File "/config/custom_components/localtuya/__init__.py", line 135, in async_setup_entry
    device = TuyaDevice(hass, entry.data)
  File "/config/custom_components/localtuya/common.py", line 98, in __init__
    self._interface = pytuya.TuyaInterface(
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 205, in __init__
    self.cipher = AESCipher(self.local_key)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 156, in __init__
    self.cipher = Cipher(algorithms.AES(key), modes.ECB(), default_backend())
  File "/usr/local/lib/python3.8/site-packages/cryptography/hazmat/primitives/ciphers/algorithms.py", line 35, in __init__
    self.key = _verify_key_size(self, key)
  File "/usr/local/lib/python3.8/site-packages/cryptography/hazmat/primitives/ciphers/algorithms.py", line 20, in _verify_key_size
    raise ValueError("Invalid key size ({}) for {}.".format(
ValueError: Invalid key size (40) for AES.

My config is as follows. I am using the ip address of the smart plug..not sur eif this should be the ip of home assistant host?

localtuya:
  - host: 192.168.1.26
    device_id: XXXXX
    local_key: XXXX
    friendly_name: SmartPlug_Office
    protocol_version: "3.3"
    entities:
      - platform: switch
        friendly_name: SmartPlug_Office
        id: 1
        current: 18
        current_consumption: 19
        voltage: 20
postlund commented 3 years ago

According to the error, the length of your local_key is wrong. Verify that the key is correct.

postlund commented 3 years ago

@9myzwb4eh Did you manage to solve this?

9myzwb4eh commented 3 years ago

Hi, I had bit of success where i didnt do anything (the initial error message for wrong deice id went away automatically) and it start showing up in entities (YAML entry), but the moment IP address changes ot becomes useless as i have to change ip on configuraiton. So i tried this mornign by setting it up from integration and it did show up but i think i am cofused about adding entities. The only entitity i add it as is switch and the values tha ti see in drop down are 0 or 11 (Screensshot attached here). I dont understand what DP means really in this context. Alos when i try to turn it on and off from entity nothing happens and the switch goes back to off position after few seconds. Please suggest.

Screenshot 2020-10-20 at 10 49 15

postlund commented 3 years ago

Looks like it worked 👍 A DP is a short for "datapoint" and a Tuya device is basically a set of datapoints (DPS). Each datapoint (which is just a number) has a value assigned to it, e.g. a boolean like true or false, an integer like 500 or a string like "open". What each DP actually means/does depends on the hardware and can also be manufacturer specific. But there are standard sets with datapoints and what they are used for which is helpful.

In your case your device has two datapoints: 1 and 11. The first datapoint, 1, is normally the general state of the device. Since it's a plug in your case, it would signal if it's on or off (which indeed seems likely because it has a boolean value: False). What the other datapoint is for I don't know (but it's not related to power, they usually use 18-20).

So I think you have just configured your entity wrong and you can unfortunately not change the main ID of an entity. So I suggest that you remove your device, add it again, when you have picked to add a switch entity just fill in a friendly name and pick 1 as "ID". Leave the other fields empty. You don't have to add any more entities after that. Hopefully your switch should work now.

postlund commented 3 years ago

Oh, yeah. Forgot to talk about the IP problem. I have another PR open that will detect IP changes and solve that problem, have a look at #86. But you can also reserve an IP in your router.

9myzwb4eh commented 3 years ago

Thanks for your response and explaination. I have tried setting both 1 or 11 on entitiy and none seems to turn switch off or on. Even if i am able to turn the netitiy sign on in lovelace nothing really happens. The log entires shows this.

2020-10-20 13:02:08 ERROR (SyncWorker_42) [custom_components.localtuya.common] Failed to set status of device 192.168.1.34 2020-10-20 13:09:41 ERROR (SyncWorker_42) [custom_components.localtuya.common] Failed to set status of device 192.168.1.34 2020-10-20 13:09:47 ERROR (SyncWorker_28) [custom_components.localtuya.common] Failed to set status of device 192.168.1.34 2020-10-20 13:09:54 ERROR (SyncWorker_8) [custom_components.localtuya.common] Failed to update status of device 192.168.1.34 2020-10-20 13:10:05 ERROR (SyncWorker_58) [custom_components.localtuya.common] Failed to set status of device 192.168.1.34 2020-10-20 13:12:40 ERROR (SyncWorker_60) [custom_components.localtuya.common] Failed to set status of device 192.168.1.34

9myzwb4eh commented 3 years ago

Ignore my comment above..i deleted and re did the config (i think the entitiy id was set it to 11) and this time i set it to 1 and it works now. Thanks a lot for your help and time here....much appreciated.