Fix reloading Tuya resets switches and binary sensors to On state
Breaking change
N/A
Proposed change
This change fixes the tuya integration where the entities would change to ON state whenever the integration was reloaded or Home Assistant was restarted. The internal function calls expected a boolean, but the changes in Tuya API returned a string value with 'true' or 'false', always a truthy value, hence the on state. This has been handled and ensured that a boolean value is returned based on the value, thus ensuring correct state. This change is done for 2 entities:
- Switches
- Binary Sensor
Another change, the value of the battery returned from the door sensor is a string and not an integer. The scale_value function broke when trying to scale the value. Ensured type checking and convert to proper type before returning back to desired function.
Type of change
- [ ] Dependency upgrade
- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [ ] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
Additional information
- This PR fixes or closes issue: fixes #115280
- This PR is related to issue:
- Link to documentation pull request:
Checklist
- [x] The code change is tested and works locally.
- [x] Local tests pass. Your PR cannot be merged unless tests pass
- [x] There is no commented out code in this PR.
- [x] I have followed the development checklist
- [x] I have followed the perfect PR recommendations
- [x] The code has been formatted using Ruff (
ruff format homeassistant tests) - [ ] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for www.home-assistant.io
If the code communicates with devices, web services, or third-party tools:
- [ ] The manifest file has all fields filled out correctly.
Updated and included derived files by running:python3 -m script.hassfest. - [ ] New or updated dependencies have been added to
requirements_all.txt.
Updated by runningpython3 -m script.gen_requirements_all. - [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
To help with the load of incoming pull requests:
- [ ] I have reviewed two other open pull requests in this repository.
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:
Hey there @tuya, @zlinoliver, @frenck, mind taking a look at this pull request as it has been labeled with an integration (tuya) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of tuya can trigger bot actions by commenting:
@home-assistant closeCloses the pull request.@home-assistant rename Awesome new titleRenames the pull request.@home-assistant reopenReopen the pull request.@home-assistant unassign tuyaRemoves the current integration label and assignees on the pull request, add the integration domain after the command.@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
Checked the Tuya API to ensure string conversion is not happening elsewhere. Their API is returning a string.
@frenck can you please check this. This is issue is causing problems with too many devices, affects automations when home assistant restarts. Can you please check this on priority.
@KunalAggarwal For more information on our review process see: https://developers.home-assistant.io/docs/review-process
Please avoid pinging people for reviews (as also mentioned in the review process document). There are many PRs open and really everybody likes theirs reviewed.
../Frenck
@frenck The scale_value change has been reverted. Let me know if https://github.com/home-assistant/core/pull/126316#discussion_r1778200156 makes sense.
I have raised a support ticket with Tuya for this API Issue. I would say, let's wait a month for them to revert and then we can revisit this in November.
@frenck at last, after a long discussion with Tuya, I got their API fixed. These checks are no longer required. Closing the PR now. Thanks a lot for your advice.