core icon indicating copy to clipboard operation
core copied to clipboard

Fix reloading Tuya resets switches and binary sensors to On state

Open KunalAggarwal opened this issue 1 year ago • 4 comments

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:

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 running python3 -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:

KunalAggarwal avatar Sep 20 '24 10:09 KunalAggarwal

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:

Learn more about our pull request process.

home-assistant[bot] avatar Sep 20 '24 10:09 home-assistant[bot]

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 close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign tuya Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

home-assistant[bot] avatar Sep 20 '24 10:09 home-assistant[bot]

Checked the Tuya API to ensure string conversion is not happening elsewhere. Their API is returning a string.

image

KunalAggarwal avatar Sep 20 '24 11:09 KunalAggarwal

@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 avatar Sep 22 '24 07:09 KunalAggarwal

@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 avatar Sep 27 '24 07:09 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.

KunalAggarwal avatar Sep 27 '24 09:09 KunalAggarwal

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.

KunalAggarwal avatar Sep 29 '24 05:09 KunalAggarwal

@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.

KunalAggarwal avatar Oct 11 '24 14:10 KunalAggarwal