Skip to content

Improve Tuya data validation#157968

Merged
epenet merged 4 commits into
devfrom
epenet-20251204-1555
Dec 4, 2025
Merged

Improve Tuya data validation#157968
epenet merged 4 commits into
devfrom
epenet-20251204-1555

Conversation

@epenet
Copy link
Copy Markdown
Contributor

@epenet epenet commented Dec 4, 2025

Proposed change

  • Move data validation from wrapper classes to type information classes
  • Add logging for invalid boolean values (previously converted to None with logging)
  • Add min/max validation of integer values (previously accepted value out of range)
  • Make type information classes generics (process_raw_value is now typed)

Type of change

  • Dependency upgrade
  • 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 #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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:

@home-assistant
Copy link
Copy Markdown
Contributor

home-assistant Bot commented Dec 4, 2025

Hey there @tuya, @zlinoliver, 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.

@epenet epenet marked this pull request as ready for review December 4, 2025 15:04
Copilot AI review requested due to automatic review settings December 4, 2025 15:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the Tuya integration's data validation by moving validation logic from wrapper classes into type information classes, improving code organization and type safety. The changes make TypeInformation classes generic with a typed process_raw_value() method, add validation for boolean and integer values, and centralize the duplicate warning suppression logic.

Key changes:

  • Added BooleanTypeInformation class with validation that rejects non-boolean values
  • Enhanced IntegerTypeInformation.process_raw_value() to validate values are within min/max range
  • Enhanced EnumTypeInformation.process_raw_value() to validate values are in the allowed range
  • Moved DEVICE_WARNINGS dictionary and _should_log_warning() from models.py to type_information.py
  • Removed redundant read_device_status() methods from wrapper classes, now using base implementation

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
homeassistant/components/tuya/type_information.py Adds validation logic to type information classes, makes classes generic, and includes warning deduplication
homeassistant/components/tuya/models.py Removes validation logic from wrapper classes and moves DEVICE_WARNINGS to type_information.py
homeassistant/components/tuya/fan.py Removes unnecessary # type: ignore[override] comment now that return type matches base class
homeassistant/components/tuya/diagnostics.py Updates import to reference moved DEVICE_WARNINGS location
tests/components/tuya/snapshots/test_climate.ambr Updates test snapshots where invalid out-of-range temperature values now correctly return None instead of invalid scaled values

Comment thread homeassistant/components/tuya/type_information.py Outdated
Comment on lines +207 to +220
if not isinstance(raw_value, int) or not (self.min <= raw_value <= self.max):
if _should_log_warning(
device.id, f"integer_out_range|{self.dpcode}|{raw_value}"
):
LOGGER.warning(
"Found invalid integer value `%s` for datapoint `%s` in product "
"id `%s`, expected value between %s and %s; please report this "
"defect to Tuya support",
raw_value,
self.dpcode,
device.product_id,
self.min,
self.max,
)
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation logic combines type checking and range checking in a single condition. If raw_value is not an integer (e.g., a float or string), the warning message will incorrectly suggest the issue is with the range rather than the type. Consider separating these checks:

if not isinstance(raw_value, int):
    if _should_log_warning(...):
        LOGGER.warning("Found invalid type `%s` for integer datapoint `%s`...", type(raw_value).__name__, ...)
    return None
if not (self.min <= raw_value <= self.max):
    if _should_log_warning(...):
        LOGGER.warning("Found integer value `%s` out of range for datapoint `%s`...", raw_value, ...)
    return None

Copilot uses AI. Check for mistakes.
Comment thread homeassistant/components/tuya/type_information.py
Comment thread homeassistant/components/tuya/type_information.py
Comment thread homeassistant/components/tuya/type_information.py
Comment thread homeassistant/components/tuya/type_information.py
'supported_features': <ClimateEntityFeature: 401>,
'target_temp_step': 1.0,
'temperature': 2.3,
'temperature': None,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This device reports temp_set with status 23, but min 50 / max 350

'min_temp': 12,
'target_temp_step': 1.0,
'temperature': 4,
'temperature': None,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (same) device reports temp_set_f with status 41, but min 122 / max 662

Comment thread homeassistant/components/tuya/models.py
epenet and others added 2 commits December 4, 2025 16:30
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@epenet epenet merged commit 4e8a31a into dev Dec 4, 2025
36 checks passed
@epenet epenet deleted the epenet-20251204-1555 branch December 4, 2025 21:20
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants