Skip to main content

Tuya quirks library

Project description

Tuya quirks library

PyPI Python Version License

Read the documentation at https://tuya-device-handlers.readthedocs.io/ Tests Codecov

pre-commit ruff

What is this?

tuya-device-handlers is a "quirks" library used by Home Assistant's Tuya integration to fix or normalise misbehaving Tuya devices. A quirk is matched against a device's product_id and patches its datapoints (function/status_range/local_strategy) before Home Assistant builds entities from them.

The library is shipped to end users via Home Assistant — you do not install it directly.

Writing a quirk

1. Find your device's product_id and datapoints

In Home Assistant: Settings → Devices & services → Tuya → your device → Download diagnostics. The JSON contains the product_id, the cloud-reported function / status_range maps, and the current status values. Use these to decide what needs patching.

2. Create a quirk file

Drop a Python file into your Home Assistant config folder at <config>/tuya_quirks/<category>_<product_id_lowercased>.py. The <category> prefix follows Tuya's official category codes (e.g. cz for plug/socket, wk for thermostat, cl for curtain).

Quirks are built using a fluent DeviceQuirk builder. Minimal example — redefine one datapoint and remove another:

from tuya_device_handlers import TUYA_QUIRKS_REGISTRY
from tuya_device_handlers.builder import DeviceQuirk
from tuya_device_handlers.const import DPMode

(
    DeviceQuirk()
    .applies_to(product_id="abcdEFGHijkl1234")
    .add_dpid_integer(
        dpid=18,
        dpcode="cur_current",
        dpmode=DPMode.READ,
        unit="mA", min=0, max=30000, scale=0, step=1,
    )
    .remove_dpid(dpid=22, dpcode="phantom_dp")
    .register(TUYA_QUIRKS_REGISTRY)
)

Each quirk file should contain exactly one DeviceQuirk()...register(...) chain at module top level — the file path is captured for reload tracking. Available builder methods include add_dpid_boolean, add_dpid_bitmap, add_dpid_enum, add_dpid_integer, and remove_dpid. For more complex needs (custom value scaling, platform-specific definitions), see the in-tree examples under src/tuya_device_handlers/devices/.

3. Test it inside Home Assistant

  1. Restart the Tuya integration (Settings → Devices & services → Tuya → ⋮ → Reload). Quirks under <config>/tuya_quirks/ are reloaded each time, so you don't need to restart Home Assistant itself.
  2. Watch the logs — you should see Loading custom quirk module … followed by Loaded custom quirks. Please contribute them to https://github.com/home-assistant-libs/tuya-device-handlers. If the import fails, the traceback is logged.
  3. Verify the device's entities reflect your changes (download diagnostics again to confirm the patched function/status_range maps).

Contributing your quirk

Once your quirk works, please open a pull request so other Home Assistant users benefit.

  1. Fork and clone this repository, then run poetry install.

  2. Move your quirk file from <config>/tuya_quirks/ to src/tuya_device_handlers/devices/<category>/. The filename should match <category>_<product_id_lowercased>.py.

  3. Add a device fixture JSON at tests/fixtures/devices/<category>_<product_id>.json. The easiest source is the function/status_range/status blocks from your Home Assistant diagnostics download.

  4. Add a test under tests/devices/<category>/ covering the patched behaviour.

  5. Run the test suite locally:

    nox --session=tests
    
  6. Open a pull request. Coverage must stay at 100% and CI runs pre-commit, mypy, tests, typeguard, and docs-build.

For broader contributor guidelines (issue reporting, dev setup, pre-commit hooks), see the Contributor Guide.

License

Distributed under the terms of the MIT license, Tuya quirks library is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated from @cjolowicz's Hypermodern Python Cookiecutter template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tuya_device_handlers-0.0.19.tar.gz (35.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tuya_device_handlers-0.0.19-py3-none-any.whl (53.6 kB view details)

Uploaded Python 3

File details

Details for the file tuya_device_handlers-0.0.19.tar.gz.

File metadata

  • Download URL: tuya_device_handlers-0.0.19.tar.gz
  • Upload date:
  • Size: 35.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tuya_device_handlers-0.0.19.tar.gz
Algorithm Hash digest
SHA256 b5e8a13c7fd47815fb523b41cd503b4d54d9e41bf9a0529af7964c9c27248d51
MD5 ce0b61d3da8aa418e61db166f2991326
BLAKE2b-256 7e15e81466642c659be8df4f927bc78be610a71ca3f4d015a6c49c5867f768d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuya_device_handlers-0.0.19.tar.gz:

Publisher: release.yml on home-assistant-libs/tuya-device-handlers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuya_device_handlers-0.0.19-py3-none-any.whl.

File metadata

File hashes

Hashes for tuya_device_handlers-0.0.19-py3-none-any.whl
Algorithm Hash digest
SHA256 17e8bc9cc7d8849a30d1abf14cb21c5b5daf548e70137d632add595da483daa2
MD5 c347e833698f8442d58656c285a7b9ff
BLAKE2b-256 df579b083fd1b6f19ac504de8d5d2c873f1da0782078504d7b28e4b98cb4f584

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuya_device_handlers-0.0.19-py3-none-any.whl:

Publisher: release.yml on home-assistant-libs/tuya-device-handlers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page