Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for BLE TRV #28

Merged
merged 22 commits into from
Jun 24, 2023
Merged

Fix support for BLE TRV #28

merged 22 commits into from
Jun 24, 2023

Conversation

forabi
Copy link
Contributor

@forabi forabi commented Jun 6, 2023

Related to #18

This fixes a couple issues with the experimental TRV support, although things are still not perfect.

It adds:

  • Support for preset away/none
  • Proper value for "current running state" (heating or idle) depending on whether the target temp is larger than the current one.
  • Fix multiplier for target and current temperature (e.g. 10 degrees used to show as 100, now fixed)
  • Fixes On/Off switch DP ID
  • Adds translation keys

Still not working properly/missing (need help with those)

  • The connection does not seem to be stable. There are multiple issues that show randomly:
    • after a while, the device may stop responding completely, the values do not update, if I try changing the temp for example, it resets in a second to the latest know value.
    • after a while, the device may still accept writing new values from HA (e.g. if I set the temp, it does change on the device), BUT, Home Assistant still resets the target temp to the previous value even if the device does update in reality.
    • I need to implement a binary sensor for "low battery" state.
    • The entitie names are not showing (e.g. Child Lock) even though I have added the translations
    • The entities for switches and other entities do not have specific prefixes (e.g. they all have switch_trv_xxx_1 instead of e.g. switch_trv_xxx_child_lock)

@PlusPlus-ua
Copy link
Owner

Hi, thank you for contribution,

You have made a lot of formatting changes, it's hard to separate formatting changes and all others. Which formatter with which settings have you used?

  • Device stops/disconnects. Usually it's caused by some unhandled exceptions. Do you have any logs for the problems?
  • Translation issues. Try to delete your TRV config entity from HA using GUI, then check ".storage" directory for devices or entities related to TRV and delete it as well. Then readd the device to HA.

@forabi
Copy link
Contributor Author

forabi commented Jun 7, 2023

I am using the dev container from the HA Development guide. It seems to auto format on save using the default Python extension.

As for the errors, I could not find anything useful other than a log message about the device unexpectedly disconnecting.

What I found more weird is the state where the device state updates following changes in HA UI but the UI reverts to the old state, making it out of sync with the device. I believe something is wrong in handle_coordinator_update or somewhere similar.

@forabi
Copy link
Contributor Author

forabi commented Jun 7, 2023

Are you okay with me submitting a PR that formats the whole repo based on VS Code default settings? After that, this PR can be debased and the changes would be minimal.

@PlusPlus-ua
Copy link
Owner

Are you okay with me submitting a PR that formats the whole repo based on VS Code default settings? After that, this PR can be debased and the changes would be minimal.

Yes, it would be great.

@forabi
Copy link
Contributor Author

forabi commented Jun 9, 2023

Device stops/disconnects. Usually it's caused by some unhandled exceptions. Do you have any logs for the problems?

It seems like I can get a lot more logs by enabling "Enable device logging" option in HA for this integration. I'll look more to see if I can find something. I also have the CubeTouch S1 which seems to stay reliably connected no matter how long the integration is running.

@forabi
Copy link
Contributor Author

forabi commented Jun 9, 2023

Translation issues. Try to delete your TRV config entity from HA using GUI, then check ".storage" directory for devices or entities related to TRV and delete it as well. Then readd the device to HA.

This did not help. I had to add a line in device.py which generates a unique ID from a helper exported by HA.

@forabi
Copy link
Contributor Author

forabi commented Jun 10, 2023

Logs below. It seems like after the device disconnects, the integration continues to successfully send commands to it, but it stops reading any updates. The HA state of the UI seems stuck, but the device is actually executing the updates (like e.g. setting the temperature or the preset mode)

2023-06-09 23:38:06.318 WARNING (MainThread) [homeassistant.config_entries] Config entry 'Thermostatic Radiator Valve 349022' for tuya_ble integration not ready yet: Could not find Tuya BLE device with address DC:23:4D:34:90:22; Retrying in background
2023-06-09 23:38:14.936 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Unexpcted packet (number 2) in notifications, expected 3
2023-06-09 23:38:14.968 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Missing packet (number 0) in notifications, received 2
2023-06-09 23:38:14.970 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Missing packet (number 0) in notifications, received 3
2023-06-09 23:38:15.067 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Unexpcted packet (number 0) in notifications, expected 1
2023-06-09 23:38:15.486 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Unexpcted packet (number 0) in notifications, expected 3
2023-06-09 23:38:15.853 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Missing packet (number 1) in notifications, received 2
2023-06-09 23:38:15.855 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Missing packet (number 0) in notifications, received 3
2023-06-09 23:38:16.047 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Unexpcted packet (number 0) in notifications, expected 3
2023-06-09 23:38:16.194 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Missing packet (number 0) in notifications, received 3
2023-06-09 23:38:16.954 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Missing packet (number 0) in notifications, received 1
2023-06-09 23:38:16.971 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Missing packet (number 0) in notifications, received 2
DC:23:4D:34:90:22: Sending datapoint update, id: 108, type: DT_BOOL: value: False
DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 295
DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 285
2023-06-09 23:44:21.310 WARNING (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Device unexpectedly disconnected; RSSI: -57
2023-06-09 23:48:21.100 WARNING (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Device unexpectedly disconnected; RSSI: -70
2023-06-09 23:48:41.412 WARNING (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Device unexpectedly disconnected; RSSI: -70
2023-06-09 23:52:17.371 WARNING (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Device unexpectedly disconnected; RSSI: -66
2023-06-09 23:55:24.632 WARNING (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Device unexpectedly disconnected; RSSI: -66
DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 275
DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 270
DC:23:4D:34:90:22: Sending datapoint update, id: 106, type: DT_BOOL: value: True
DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 300
2023-06-09 23:58:48.279 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 300
2023-06-09 23:58:48.282 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Sending packet: #7 FUN_SENDER_DPS
2023-06-09 23:58:48.726 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Packet received: 00213005da9a8e57a1e15ae49c2b10af8082b62e
2023-06-09 23:58:48.727 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Packet received: 01efc2199f19165b9f123e9405be1ab503
2023-06-09 23:58:48.730 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Received: #7 FUN_SENDER_DPS, response to #7
2023-06-09 23:58:48.730 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Received expected response to #7, result: 0
DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 295
2023-06-10 00:02:14.081 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 295
2023-06-10 00:02:14.085 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Sending packet: #8 FUN_SENDER_DPS
2023-06-10 00:02:14.444 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Packet received: 002130056f9dc99b67770703a4dc82e4cf01a19d
2023-06-10 00:02:14.444 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Packet received: 01500515bb052ee65e4a420466c3f5acf0
2023-06-10 00:02:14.444 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Received: #8 FUN_SENDER_DPS, response to #8
2023-06-10 00:02:14.445 DEBUG (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Received expected response to #8, result: 0

@forabi
Copy link
Contributor Author

forabi commented Jun 10, 2023

Support looks good overall, now only if I could figure out the reason for the disconnections and the names for the entities (the IDs are fixed, but the UI names are not, they are the same as the name of the device, while they should read like "Child Lock", "Programming Mode", etc.)

Screenshot 2023-06-10 at 3 49 24 AM Screenshot 2023-06-10 at 3 49 28 AM

@Yokosa
Copy link
Contributor

Yokosa commented Jun 11, 2023

@PlusPlus-ua @forabi Thanks for your work on handling TRV. I was able to add the correct entity name support, also I added support for my thermostat head which has id: "nhj2j7su". I did the fixes on your branch @forabi, for now I have it all locally but I will do a pull if your current changes are uploaded from "pull request" on the main branch.
Zrzut ekranu 2023-06-11 184348

Also, I am having problems updating status and temperature. I have 2 of these thermostatic heads and alternately I always have problems with one.

@forabi
Copy link
Contributor Author

forabi commented Jun 11, 2023

@Yokosa You could start a branch that targets my PR as a base and then it can be merged after mine. Unfortunately I haven't found the reason for the intermittent disconnect yet. My observation is that the device continues to respond to changes from HA, but HA stops reading the new changes from the device. This continues for hours and the only fix is to either reload the integration or restart HA entirely.

@forabi forabi marked this pull request as ready for review June 13, 2023 01:08
@forabi
Copy link
Contributor Author

forabi commented Jun 13, 2023

I think this can be reviewed and merged despite the remaining issues, which I can try to fix in a follow-up PR. @Yokosa feel free to open a new PR with your changes too! Thanks!

@Yokosa
Copy link
Contributor

Yokosa commented Jun 15, 2023

@forabi Something is wrong after the merge of recent changes. The buttons are not detected and the set temperature is wrongly displayed.

Zrzut ekranu 2023-06-15 080033

@forabi
Copy link
Contributor Author

forabi commented Jun 15, 2023

@Yokosa Try removing and re-adding the device

@Yokosa
Copy link
Contributor

Yokosa commented Jun 15, 2023

@Yokosa Try removing and re-adding the device

It is still not working properly. In climate.py is missing target_temperature_coefficient=10.0, switchs is just analyzing.

@forabi
Copy link
Contributor Author

forabi commented Jun 18, 2023

@PlusPlus-ua Continuing with the debugging, it looks like _handle_coorinator_update just stops being called after the device disconnects:

DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 180
TuyaBLEClimate._handle_coordinator_update
2023-06-18 07:42:41.171 WARNING (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] DC:23:4D:34:90:22: Device unexpectedly disconnected; RSSI: -53
DC:23:4D:34:90:22: Sending datapoint update, id: 103, type: DT_VALUE: value: 195
(  NO NEW CALL to  TuyaBLEClimate._handle_coordinator_update )

This is why the UI states never update, even though the device continues to receive update commands and executing them succesfully. Any hints why this method may not be called? Any way to force a reconnect or something?

@PlusPlus-ua PlusPlus-ua merged commit a07568d into PlusPlus-ua:main Jun 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants