PlusPlus-ua / ha_tuya_ble

Home Assistant support for Tuya BLE devices
MIT License
155 stars 98 forks source link

Integrating ZX-5442 #87

Open njoye opened 6 months ago

njoye commented 6 months ago

I would like to use this integration to control the Tuya ZX-5442 (cheap ReVolt radiator valve thermostat). However, I can't seem to figure out how to get started on that. I tried integrating through the Tuya API but first of all don't like it and second of all it doesn't work because the device is updating it's status in there only roughly once a day and the API returns outdated information. I have no problem providing a PR for this, just need a hint as to where to start. Can't even figure out how to connect to the device and get information that isn't 0's back from it through BLE at the moment. I read about there being a difference between BLE and Bluetooth Mesh devices and that only BLE devices can be integrated with this extension - if so, how can I check what category my device belongs to ?

njoye commented 6 months ago

For better understanding and a more productive discussion, this is what I know about my thermostat from the Tuya API.

General

Properties

    "functions": [
      {
        "code": "temp_set",
        "desc": "{\"unit\":\"°C\",\"min\":1,\"max\":59,\"scale\":1,\"step\":5}",
        "name": "温度设置",
        "type": "Integer",
        "values": "{\"unit\":\"°C\",\"min\":1,\"max\":59,\"scale\":1,\"step\":5}"
      },
      {
        "code": "mode",
        "desc": "{\"range\":[\"auto\",\"manual\",\"holiday\"]}",
        "name": "工作模式",
        "type": "Enum",
        "values": "{\"range\":[\"auto\",\"manual\",\"holiday\"]}"
      },
      {
        "code": "child_lock",
        "desc": "{}",
        "name": "童锁",
        "type": "Boolean",
        "values": "{}"
      },
      {
        "code": "holiday_set",
        "desc": "{\"maxlen\": 255}",
        "name": "假日模式时间设置",
        "type": "String",
        "values": "{\"maxlen\": 255}"
      }
    ],
    "status": [
      {
        "code": "temp_set",
        "name": "温度设置",
        "type": "Integer",
        "values": "{\"unit\":\"°C\",\"min\":1,\"max\":59,\"scale\":1,\"step\":5}"
      },
      {
        "code": "temp_current",
        "name": "当前温度",
        "type": "Integer",
        "values": "{\"unit\":\"°C\",\"min\":-50,\"max\":350,\"scale\":1,\"step\":5}"
      },
      {
        "code": "mode",
        "name": "工作模式",
        "type": "Enum",
        "values": "{\"range\":[\"auto\",\"manual\",\"holiday\"]}"
      },
      {
        "code": "child_lock",
        "name": "童锁",
        "type": "Boolean",
        "values": "{}"
      },
      {
        "code": "fault",
        "name": "故障告警",
        "type": "Bitmap",
        "values": "{\"label\":[\"fault1\",\"fault2\",\"fault3\"]}"
      },
      {
        "code": "battery_percentage",
        "name": "电池电量",
        "type": "Integer",
        "values": "{\"unit\":\"V\",\"min\":0,\"max\":1000,\"scale\":2,\"step\":1}"
      },
      {
        "code": "holiday_set",
        "name": "假日模式时间设置",
        "type": "String",
        "values": "{\"maxlen\": 255}"
      }
    ]
}

The device is not WiFi capable - bluetooth only, so no IP or others. If anyone is interested the official page of the manufacturer is this (German).

njoye commented 6 months ago

So, I just tried adding my device ID to the thermostats, which makes one of the 6 show up and be identified correctly. However, it seems like the authentication fails (assumption) and it get's disconnected every time it tries to authenticate. This is the same process that happens when I connect with a BLE app to the thermostat.

2023-12-03 06:01:24.232 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4F:8B:48:4F: Connecting; RSSI: -71
2023-12-03 06:01:25.900 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4F:8B:48:4F: Connected; RSSI: -71
2023-12-03 06:01:26.435 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4F:8B:48:4F: Sending device info request
2023-12-03 06:01:26.436 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4F:8B:48:4F: Sending packet: #7 FUN_SENDER_DEVICE_INFO
2023-12-03 06:02:15.917 WARNING (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4F:8B:48:4F: Device unexpectedly disconnected; RSSI: -71
2023-12-03 06:02:26.444 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4F:8B:48:4F: timeout receiving response, RSSI: -71
2023-12-03 06:02:26.445 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4F:8B:48:4F: Sending device info request failed
MarkPaxton commented 6 months ago

Hey I'm also trying to do the same thing with some similar devices Hombli device ID ngxyxyc8.

How did you get on with your set up as we can collaborate here.

When I added the component and restarted HA my devices were detected and immediately offered for set up. I put in my Tuya API credentials and they were fully configured but only with the signal strength property "disabled". Even enabling it didn't give me a value either.

I tried adding the ID to the list of devices in climate.py: https://github.com/PlusPlus-ua/ha_tuya_ble/blob/main/custom_components/tuya_ble/climate.py#L74

Unfortunately this didn't help, it started to crash HA, and if I rebooted, it did offer another 'climate' property but was in error state so it clearly wasn't working.

I assume the right thing to do here is identify the control channels for the devices and create a new profile matching them.

dimitribo commented 6 months ago

Hello

I'm also trying to do the same thing with the Hombli radiator valves (HBRT-100 ). Those have the ID 081bzoun. I already modified the code from tuya BLE. I added the id's in de different files, but all the time i also have the connection lost trouble, and i never had values....

image

MarkPaxton commented 6 months ago

It may be a deeper issue, but when I connect 9 of the devices via BLE it makes the whole of my system unstable. I'm instead trying with localtuya via the gateway, as I think it's probably a bluetooth implementation issue. It could be lots of connection attempts failing and hanging up, but makes any development on it very difficult!

The Hombli model number of this one is HBR-0109.

MarkPaxton commented 6 months ago

I have looked at the beta version of the Tuya "smart life" integration https://github.com/tuya/tuya-smart-life/tree/main

I also crawled through the debug logs. Seems like my device identifies as "wk" which smartlife configures as a Thermostat. Whereas supported Radiator thermostat devices are identified as "wkf"

I think this is all related, as if I can't get the devices to work via Tuya cloud, then locally it's going to be impossible too. First I'll look there and try and fix (hack) the smartlife code...

dimitribo commented 6 months ago

If i look to my devices, they are identified with "wk". This is also used in the different Tuya BLE configuration files, so that's seems to be ok I think, No ?

MarkPaxton commented 6 months ago

Yes, as it turns out, it's listed here in the Tuya docs: https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq#:~:text=wk,Thermostat

njoye commented 6 months ago

@MarkPaxton For integration with the Tuya API I think TinyTuya can help a lot if you're a little familiar with Python. My thermostats do work with the Tuya Integration in HA through the WiFi Gateway. However, I've found that the manufacturer's implementation really sucks and only updates the values in the API once a day through the WiFi gateway and for some reason only, when the Tuya App is opened, not even when an API call to retrieve the data is made. I had already settled on building my own API integration and using it like that, but with the poor implementation, I couldn't use the WiFi connection to retrieve/set the data.

@dimitribo @MarkPaxton I have no clue what specific steps I need to take next to integrate my thermostat. Since we all seem to be at a similar step in the process maybe we can help each other out. I believe the disconnects are happening because authentication doesn't happen in the required amount of time, so the device disconnects and frees the connection again for someone else to connect & authenticate. This makes sense, however - I assumed that the existing Tuya BLE code would take care of the authentication with my device. As of right now, the debug output also doesn't really help, as it just tells me that Tuya BLE is trying to connect to my thermostat, connects and then get's disconnected. Part of that issue seems to be that Sending packet: #7 FUN_SENDER_DEVICE_INFO never seems to get an answer, but I think that is secondary to the authentication issue. The disconnect happens in the same way if I just connect with a BLE app from my iPhone. The connection works, but I get thrown out after a few seconds most likely because of the authentication issue.

So the real question is: Where is authentication handled in this integration and why does it fail with my/our devices? I guess that's the first step for now, correct me if I'm wrong.

njoye commented 6 months ago

@MarkPaxton @dimitribo My radiator thermostat also gets classified as "wk" from what I can see from my logs.

njoye commented 6 months ago

@MarkPaxton Btw, if you have a BLE-only thermostat you won't be able to use LocalTuya with it, as it doesn't get it's own IP from what I understand. I attempted that as well.

MarkPaxton commented 6 months ago

I have a couple of BLE to WiFi gateways so I can try and connect via that (though Tuya-local integration does not work with this).

Happy to try and build a BLE component directly though, seems the best solution.

I have the same experience with the FUN message failing. Perhaps the multiple streams are what is crashing my system. I tried a second Bluetooth adapter just in case it was a driver/firmware thing. No change.

At some point, I got a full status message back with all the Tuya documented values in it. I forgot where it came from but I have a copy of a log message. Possibly the main Tuya integration debug log.

Most of the time my devices report “features” as a very small set of data, nothing useful though other than “switch”.

dimitribo commented 6 months ago

@MarkPaxton For integration with the Tuya API I think TinyTuya can help a lot if you're a little familiar with Python. My thermostats do work with the Tuya Integration in HA through the WiFi Gateway. However, I've found that the manufacturer's implementation really sucks and only updates the values in the API once a day through the WiFi gateway and for some reason only, when the Tuya App is opened, not even when an API call to retrieve the data is made. I had already settled on building my own API integration and using it like that, but with the poor implementation, I couldn't use the WiFi connection to retrieve/set the data.

@dimitribo @MarkPaxton I have no clue what specific steps I need to take next to integrate my thermostat. Since we all seem to be at a similar step in the process maybe we can help each other out. I believe the disconnects are happening because authentication doesn't happen in the required amount of time, so the device disconnects and frees the connection again for someone else to connect & authenticate. This makes sense, however - I assumed that the existing Tuya BLE code would take care of the authentication with my device. As of right now, the debug output also doesn't really help, as it just tells me that Tuya BLE is trying to connect to my thermostat, connects and then get's disconnected. Part of that issue seems to be that Sending packet: #7 FUN_SENDER_DEVICE_INFO never seems to get an answer, but I think that is secondary to the authentication issue. The disconnect happens in the same way if I just connect with a BLE app from my iPhone. The connection works, but I get thrown out after a few seconds most likely because of the authentication issue.

So the real question is: Where is authentication handled in this integration and why does it fail with my/our devices? I guess that's the first step for now, correct me if I'm wrong.

I would like to help with the integration, but i really don't now at this moment why it is not working. I allready see in the "pull requests" that those people easely get other things working ... So why "wk" is not working, i really don't know. If i must test things, i can, but what to modifiy at this moment ???

MarkPaxton commented 6 months ago

I'm going to start (when I have time) trying to access devices via tinytuya. I have an IP light which I will try first, then I will try to connect either via BLE directly or via the BLE/WiFi hub... not sure where yet. But the approach is keep going down the stack and find the smallest possible operation that works, then build from it.

dimitribo commented 6 months ago

Hello, This evening did a fresh install of the Tuya BLE app and did e reprogrammation for my devices. I get those entities now, but still without values:

image

Those are the errors that i get in the log file:

xx:xx:xx:xx:xx:xx: Sending device info request failed 21:49:04 – (FOUT) Tuya BLE (aangepaste integratie) - bericht kwam voor het eerst om 21:28:42 en verschijnt 14 keer xx:xx:xx:xx:xx:xx: timeout receiving response, RSSI: -78 21:49:04 – (FOUT) Tuya BLE (aangepaste integratie) - bericht kwam voor het eerst om 21:28:42 en verschijnt 14 keer xx:xx:xx:xx:xx:xx: Device unexpectedly disconnected; RSSI: -70 21:48:32 – (WAARSCHUWING) Tuya BLE (aangepaste integratie) - bericht kwam voor het eerst om 21:28:11 en verschijnt 21 keer xx:xx:xx:xx:xx:xx: device not found, not in range, or poor RSSI: -127 21:44:14 – (FOUT) Tuya BLE (aangepaste integratie) - bericht kwam voor het eerst om 21:27:13 en verschijnt 8 keer

njoye commented 6 months ago

@MarkPaxton @dimitribo Seems like we're all having the same issue with our thermostats. Let's each try to figure out a solution and get back to this when we know more. I didn't really have time to dive deeper into this issue since my first post, unfortunately. Maybe Christmas will provide some time buffer to look into it.

njoye commented 6 months ago

Maybe @airy10 knows where this might come from or has seen this while working on the light integration ?

MarkPaxton commented 6 months ago

I’ve been raising the issues with tinytuya as well as right now I can’t even communicate with a light or a gateway over IP so I’ve got to cover the basics before I can think about BLE :(

MarkPaxton commented 6 months ago

I got my devices to respond via the wifi/BLE gateway using tinytuya scan -f, I then created a profile for the device in tuya-local https://github.com/make-all/tuya-local/pull/1424/files

The problem here is that the BLE gateway only supports 3 concurrent IP connections, and each sub-device gets it's own connection. Improvements on that might be made but clearly the way forward is still direct BLE communication.

njoye commented 5 months ago

@MarkPaxton How did you get that connection through the BLE gateway? When I try to scan, even after setting up the wizard - all I get is this output


Scanning local network for Tuya devices...
    1 local devices discovered                     

Polling local devices...
    [Heizung Arbeitszimmer    ] Error: No IP found
    [Heizung Bad              ] Error: No IP found
    [Heizung Balkontür        ] Error: No IP found
    [Heizung Dachfenster      ] Error: No IP found
    [Heizung Esszimmer        ] Error: No IP found
    [Heizung Schlafzimmer     ] Error: No IP found
    [ZX-5012                  ] 192.168.178.81     - DPS: {'124': False}

>> Saving device snapshot data to snapshot.json

So while I can find the ZX-5012 (BLE Gateway) I am unable to find the the radiator valves through the WiFi Gateway - it appears that they just don't get an IP in my case.

MarkPaxton commented 5 months ago

They won’t get an IP, essentially you have to treat them as a sub device of the gateway and include the uuid in a request using the gateway ip/id/key. In the python code, set up the gateway device and make sure you can receive the status, then set up the BLE device but specify the gateway as parent.

I’ll try to post the code later but might have already above. (Mobile currently…)

On Sat, 23 Dec 2023 at 11:57, Timo Mueller @.***> wrote:

@MarkPaxton https://github.com/MarkPaxton How did you get that connection through the BLE gateway? When I try to scan, even after setting up the wizard - all I get is this output

Scanning local network for Tuya devices... 1 local devices discovered

Polling local devices... [Heizung Arbeitszimmer ] Error: No IP found [Heizung Bad ] Error: No IP found [Heizung Balkontür ] Error: No IP found [Heizung Dachfenster ] Error: No IP found [Heizung Esszimmer ] Error: No IP found [Heizung Schlafzimmer ] Error: No IP found [ZX-5012 ] 192.168.178.81 - DPS: {'124': False}

Saving device snapshot data to snapshot.json

So while I can find the ZX-5012 (BLE Gateway) I am unable to find the the radiator valves through the WiFi Gateway - it appears that they just don't get an IP in my case.

— Reply to this email directly, view it on GitHub https://github.com/PlusPlus-ua/ha_tuya_ble/issues/87#issuecomment-1868268442, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACDG46VRPKVYNE42SFCVWLYK22IDAVCNFSM6AAAAAA75FOFWGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNRYGI3DQNBUGI . You are receiving this because you were mentioned.Message ID: @.***>

MarkPaxton commented 5 months ago

Have a look here for more details:

https://github.com/jasonacox/tinytuya/issues/435

njoye commented 5 months ago

@MarkPaxton I wasn't aware that it was possible to access the BLE device through the WiFi gateway with TinyTuya. I tried getting a status from my gateway but it appears I'm doing something wrong here.

(base) ➜  ble python3 tt.py
{'Error': 'Check device key or version', 'Err': '914', 'Payload': None}

This is my current script for getting the status

import tinytuya
gateway_config = {
    'dev_id': '******',
    'key': "******",
    'ip': '192.168.178.81'
}

gateway = tinytuya.Device(gateway_config['dev_id'], gateway_config['ip'], gateway_config['key'])
gateway.set_version(3.4)
print(gateway.status())

All values are directly copied from the result I'm getting through python3 -m tinytuya scan -f - I would have expected to be able to just connect to the gateway without problems. Did you experience something similar ?

njoye commented 5 months ago

It appears that there is a timeout in the communication with my device, even though the session key is properly negotiated ?

DEBUG:Timeout in _send_receive() - retry 5 / 5
DEBUG:sending payload quick
DEBUG:final payload: b'0123456789abcdef'
DEBUG:payload encrypted=b'000055aa0000001300000003000000444b3a4171d273236c4386d35e9a20fe1ba448b54f65efa9f98bd23cb6ecf2d0b4c51164e169d9db42f0d8d87a0a10862d4d2eaf42210e0dd478db2a3153ac91a10000aa55'
DEBUG:received data=b'000055aa0000f921000000040000006800000000dd7b7a5e08ba98bce47a80388036f2ebd4310ab7b18cf63caddae8f6a8fe917c71d28c9a6180536c915a9c86a3e7d89ca448b54f65efa9f98bd23cb6ecf2d0b42f73f9d3954b162b7d19c78381f38c33bbd0d4a27c1017c19658ec1b28f5cfb90000aa55'
DEBUG:decrypting=b'\xdd{z^\x08\xba\x98\xbc\xe4z\x808\x806\xf2\xeb\xd41\n\xb7\xb1\x8c\xf6<\xad\xda\xe8\xf6\xa8\xfe\x91|q\xd2\x8c\x9aa\x80Sl\x91Z\x9c\x86\xa3\xe7\xd8\x9c\xa4H\xb5Oe\xef\xa9\xf9\x8b\xd2<\xb6\xec\xf2\xd0\xb4'
DEBUG:decrypted session key negotiation step 2 payload=b'725d24ca9fcd4d74\xe8#\xb3\xc9O\xe0N\x1a*\xd1o\x94\x1d&\x1e\xc5 \xa9|s\x93\xa8\x1d\x10\xbab\xf7\x17\xdf\xb6\nh'
DEBUG:payload type = <class 'bytes'> len = 48
DEBUG:session local nonce: b'0123456789abcdef' remote nonce: b'725d24ca9fcd4d74'
DEBUG:sending payload quick
DEBUG:final payload: b'Z\xdc\xfb,\x90\x01\xc2Z`!s\x08\xd9\xd0\xc7\xb3\xa8=&B\xd1FY\xe6&\xec>"\\@\xa9\x99'
DEBUG:payload encrypted=b'000055aa0000001400000005000000546ff12d01ce1cbc8fab4fafe7e3510d3e8557378d61f32bcfe89987e5d037e39aa448b54f65efa9f98bd23cb6ecf2d0b4102764f28fa9e9bd56a5416e336dce110d0742ae0df5c193cfdc6b8408a208440000aa55'
DEBUG:Session nonce XOR'd: b'\x07\x03\x07W\x06\x01UV\x01_\x02\x06W\x00RR'
DEBUG:Session key negotiate success! session key: b']\xee\xe0\xb4\xe99\x08\xc4\x95W\xff\xe0~\x95\xc1\xa4'
DEBUG:sending payload
DEBUG:final payload: b'3.4\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00{"devId":"bfb728ad4c03b7259dbyto","uid":"bfb728ad4c03b7259dbyto","t":"1703351360","dps":{"1":null}}'
DEBUG:payload encrypted=b'000055aa000000150000000d000000a40402561a5897b1ef3cfa080279c997f3ff8a8517fce992ae0733b28f82e4d18f69c4b8800893f436f8ceeb7d4e0e86727cce870b72ebbf4eaec6eb13b65455f8420a78442a1cbf8f4f47fcc2f86c7aa326b8d4999b0c3a28c6595fc248907209a846af87450e2aa130e91432f28bd3f76fea7eb0533e5116a17e20fde2bc1a5944a71b7b22182104532b1b6d13ca35bd9679dd7513c1943e62952999592262d40000aa55'
DEBUG:Timeout in _send_receive() - retry 6 / 5
DEBUG:Exceeded tinytuya retry limit (5)
DEBUG:ERROR Check device key or version - 914 - payload: null
njoye commented 5 months ago

@MarkPaxton Actually, since I didn't the polling through tinytuya scan -f failed, I think I don't need to expect the device to return values for the .status() call. However, even trying to query subdevices or detecting available datapoints seems to fail (or not return any values that make sense). I don't really know where to start debugging this or if I'm even on the right track.

njoye commented 5 months ago

Also double checked the local key, device ID and IP address. All seem correct.

njoye commented 5 months ago

With a lot of help from @uzlonewolf (thanks!) I'm now able to read & update the data from my thermostat through TinyTuya. Also the DP mapping now makes a lot more sense. Most of the progress is documented in https://github.com/jasonacox/tinytuya/issues/435 .

njoye commented 5 months ago

I assume that this is device-specific, but it appears that my thermostat is not controlled through DP 2, but rather through DP 107 (auto_temp) when in auto mode.

The currently set temperature in manual mode set_temp only reports the correct currently set temperature when the device is in manual mode - in auto mode it reports another value, probably the last one that was set.

I don't know why, but I seem to be unable to set the temperature of my thermostat (when in manual mode) through DP 2. DP 2 (set_temp) as a result just returns the current battery state. However, switching back to auto mode and trying to set the temperature through DP ID 107 again (which also didn't work in auto mode) had a reaction time of < 2s.

njoye commented 5 months ago

@MarkPaxton Now that I know how to control this thermostat, what was your process for integrating it into LocalTuya? I think that would be a lot more fruitful than debugging and reverse-engineering the BLE protocol at this point.

njoye commented 5 months ago

For anyone interested, this was the last script I tested the reaction of my thermostat with

#tinytuya.set_debug(True)
gateway = tinytuya.Device(gateway_config['dev_id'], gateway_config['ip'], gateway_config['key'], )
gateway.set_version(3.4)
subdevices = gateway.subdev_query()["data"]["online"]

print(f"Found {len(subdevices)} subdevices")
for subdevice in subdevices:
    print(subdevice)

thermostat = tinytuya.Device(thermostat_config['dev_id'], parent=gateway, cid=thermostat_config['cid'])
thermostat.set_version(3.4)

print("Updating status")
thermostat.updatedps([2, 3, 7, 107])

print("Getting status")
status = thermostat.status()
print(status)

current_temp = status['dps']['3'] / 10
temp_set = status['dps']['2'] 
auto_temp_set = status['dps']['107'] / 2
dev_id = status['device'].id
current_mode = status['dps']['4']

print("Current temp: " + str(current_temp), "Set temp: " + str(temp_set/2))
print("Potential new temp set: " + str(auto_temp_set))
print(f"Device ID: {dev_id}, CID: {thermostat_config['cid']}")
print("Child lock: " + str(status['dps']['7']))
print("Current mode: " + str(current_mode))

if thermostat_config['cid'] in subdevices:
    print("Device is online")

    val = input("Set new temperature to: ") 
    if val != "n":
        response = thermostat.set_value('107', int(val)*2)
        print(response)
        print("New temp set to: " + str(val))
dimitribo commented 5 months ago

@njoye woow, looked you got some succes with the thermostat! I want this morning to try the same code for my Hombli thermostats, but i allreaudy have a first issue.

I try to install tinytyya in Home Assistant and get following error ... <Integration jasonacox/tinytuya> Repository structure for v1.13.1 is not compliant Somebody know more about the installation ?

And alos, where do i have to put hte code then in Home assistant to get/ask the informations please ?

Kind regards !

njoye commented 5 months ago

@dimitribo TinyTuya is a python module you can install through pip. It really helped me to get the small script I had to run before I tried integrating with TuyaLocal. Try connecting to your gateway first with the TinyTuya python module and then try to retrieve and set some values of your thermostats - after that you should be ready to integrate your thermostats into HA (which I'm working on at the moment).

MarkPaxton commented 5 months ago

Yep. you don't need to 'install' tinytuya for HA integration, it's a tool to help debug / test the connection before using tuya-local.

If you want to try it out, first install tuya local following the instructions carefully https://github.com/make-all/tuya-local

After that, for the Hombli devices, I've been working on the configuration profile for them and started a pull request for it here: https://github.com/make-all/tuya-local/pull/1424

I've not had time to respond to all the comments on it yet, but at least if you start with adding that profile you'll get partial functionality. Assuming you could already connect with tinytuya.

Any additions to my PR will also be very welcome, I've not had time to work on it yet.

dimitribo commented 5 months ago

@dimitribo TinyTuya is a python module you can install through pip. It really helped me to get the small script I had to run before I tried integrating with TuyaLocal. Try connecting to your gateway first with the TinyTuya python module and then try to retrieve and set some values of your thermostats - after that you should be ready to integrate your thermostats into HA (which I'm working on at the moment).

Hello, I looked how i can do it true pip but i didn't find a working methode at the moment to do it in home assistant ... Is this a tool/something outside home assistant ? I also read something to add "python_script:" in my configuration.yaml and also add a folder with python scripts, but nothing is running for this ...

Where can i find the "hwo to" for the tiny tuya so that i can test more please ?

njoye commented 5 months ago

@dimitribo Yes, the method is "outside" Home Assistant. TinyTuya is a Python package that you install on your computer and then write some code in a code editor (I use Visual Studio Code) that you can then run. You will need to install Python on your computer locally and then run your script where you use TinyTuya to connect to your Tuya device on your own computer first. That will give you a lot of information which you will need for integrating your thermostat in home assistant later.

If all of that is too technical, you can create an issue in the TuyaLocal repository and provide all the necessary information (the description on how to find that is in the readme of the repo https://github.com/make-all/tuya-local) .

dimitribo commented 5 months ago

@dimitribo Yes, the method is "outside" Home Assistant. TinyTuya is a Python package that you install on your computer and then write some code in a code editor (I use Visual Studio Code) that you can then run. You will need to install Python on your computer locally and then run your script where you use TinyTuya to connect to your Tuya device on your own computer first. That will give you a lot of information which you will need for integrating your thermostat in home assistant later.

If all of that is too technical, you can create an issue in the TuyaLocal repository and provide all the necessary information (the description on how to find that is in the readme of the repo https://github.com/make-all/tuya-local) .

Thanks! That was the info that i needed. I was looking in HA to integrate or install it but didn't find anything. That's why i asked if this is a external thing to install ! Thanks, i wil givethis a try ;)

Kind regards !

MarkPaxton commented 5 months ago

Just got my thermostat config merged into Tuya local. Will be a good template to start any other TRV integration.

The problem is the gateways are just too unreliable. So - back to BLE (and/or improved gateway support in Tuya local…).

dimitribo commented 4 months ago

For anyone interested, this was the last script I tested the reaction of my thermostat with

#tinytuya.set_debug(True)
gateway = tinytuya.Device(gateway_config['dev_id'], gateway_config['ip'], gateway_config['key'], )
gateway.set_version(3.4)
subdevices = gateway.subdev_query()["data"]["online"]

print(f"Found {len(subdevices)} subdevices")
for subdevice in subdevices:
    print(subdevice)

thermostat = tinytuya.Device(thermostat_config['dev_id'], parent=gateway, cid=thermostat_config['cid'])
thermostat.set_version(3.4)

print("Updating status")
thermostat.updatedps([2, 3, 7, 107])

print("Getting status")
status = thermostat.status()
print(status)

current_temp = status['dps']['3'] / 10
temp_set = status['dps']['2'] 
auto_temp_set = status['dps']['107'] / 2
dev_id = status['device'].id
current_mode = status['dps']['4']

print("Current temp: " + str(current_temp), "Set temp: " + str(temp_set/2))
print("Potential new temp set: " + str(auto_temp_set))
print(f"Device ID: {dev_id}, CID: {thermostat_config['cid']}")
print("Child lock: " + str(status['dps']['7']))
print("Current mode: " + str(current_mode))

if thermostat_config['cid'] in subdevices:
    print("Device is online")

    val = input("Set new temperature to: ") 
    if val != "n":
        response = thermostat.set_value('107', int(val)*2)
        print(response)
        print("New temp set to: " + str(val))

Hi, I'm busy to test and modifing the code for my thermostats. I allreadye changed/added my ID's, passes, .. but it's al the time crashing on:

subdevices = gateway.subdev_query()["data"]["online"]

My error is : Traceback (most recent call last): File "c:\Users\Gebruiker\Documents\Python\111_TinyTuya_thermostaat.py", line 11, in subdevices = gateway.subdev_query()["data"]["online"]


KeyError: 'data'

But on the internet i don't find very much about the subdev_query() function ... 
Is there maybe possbile to get a little bit op help please ?

thanks !!!

BTW, is there allready any solution for the integration in the Tuya BLE ... ???