Skip to main content

Tuya quirks library

PyPI Python Version License

Tests Codecov OpenSSF Scorecard Open in Dev Containers

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.

If function, status_range and status are all empty, the cloud told Home Assistant nothing about the device and it gets no entities. See Finding the datapoints to get them from the device's thing model instead.

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

New to this and not a programmer? For a fully worked, copy-paste walkthrough of one of the most common fixes — a device sending ENUM values (e.g. extra mode presets) that Home Assistant rejects as Found invalid ENUM value ... — see Recipe: add missing ENUM values. It covers writing the quirk, testing it live, and opening a PR end to end.

Entities missing — all of them, or just some? A quirk can only rewrite a device's datapoints; turning them into entities is Home Assistant's job, and it can only do that for a category and a DPCode it knows. If a datapoint is absent from the diagnostics, a quirk can add it; if it's present but never becomes an entity, the fix belongs in core. See Recipe: missing entities.

Cover position backwards? A closed blind shows as open, or the open button closes it. See Recipe: inverted covers.

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 the warning Loaded custom quirks. Please contribute them to https://github.com/home-assistant-libs/tuya-device-handlers. If the import fails, the traceback is logged. The per-file Loading custom quirk module … lines are only logged with debug logging on for tuya_device_handlers.

  3. Verify the device's entities reflect your changes. Download diagnostics again: quirk shows the quirk file that applied, and function / status_range show the patched maps.

  4. To see reports and commands as they happen, turn on debug logging by calling the logger.set_level action from Developer tools → Actions. (Enable debug logging on the Tuya integration page covers the integration and tuya_sharing, but not this library, so it won't show quirk loading.)

    action: logger.set_level
    data:
      tuya_device_handlers: debug
      tuya_sharing: debug
      homeassistant.components.tuya: debug
    

    Reload the Tuya integration again, so that the quirk loading is logged too. Then use the device from its app or its buttons, and try each entity from Home Assistant. Useful lines to look for (the device ID is the id field in your diagnostics):

    Log line Meaning
    Loading custom quirk module … Your quirk file was imported (an import error is logged here).
    mq _on_device_report unknown dpId: … A report arrived for a dpid with no mapping: see Finding the datapoints.
    mq _on_device_report after strategy convert code=…,value=… A report arrived and your quirk mapped its dpid to a code.
    Received update for device <id> … {'<code>': <value>} Home Assistant applied the report to the device.
    Sending commands for device <id>: [{'code': …, 'value': …}] Home Assistant sent a command.
    response before decrypt ret = {'code': '2008', … 'success': False} The Tuya cloud rejected that command: a quirk can't fix that, see Commands rejected by the cloud.

    Set the log levels back afterwards (warning, or whatever your configuration.yaml sets), or restart Home Assistant.

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 uv sync.

  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. Build it from your Home Assistant diagnostics download: keep only the contents of the top-level data property (the captured device payload), then remove its id, terminal_id, and home_assistant keys. Name the file from the payload's own category and product_id fields. For example:

    python3 -c "
    import json
    d = json.load(open('diagnostics.json'))['data']
    for k in ('id', 'terminal_id', 'home_assistant'):
        d.pop(k, None)
    name = f\"tests/fixtures/devices/{d['category']}_{d['product_id']}.json\"
    with open(name, 'w') as f:
        json.dump(d, f, indent=2, ensure_ascii=False)
        f.write('\n')
    print(name)
    "
    
  4. Add a test under tests/devices/<category>/ covering the patched behaviour.

  5. Run the test suite locally:

    uv run pytest --cov tuya_device_handlers tests
    
  6. Open a pull request.

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.

Device quirks are not written on request: if your device needs one, write it yourself following Writing a quirk, then contribute it. Issues and pull requests without activity are marked as stale and then closed automatically; comment to keep them open.

Metadata

Release files for tuya-device-handlers 2026.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tuya-device-handlers 2026.12.0
File Size Uploaded Provenance Actions
tuya_device_handlers-2026.12.0.tar.gz 70.6 kB With provenance Details for tuya_device_handlers-2026.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tuya-device-handlers 2026.12.0
File Interpreter ABI Platform Actions
tuya_device_handlers-2026.12.0-py3-none-any.whl Python 3 none any Details for tuya_device_handlers-2026.12.0-py3-none-any.whl

Total release size: 198.0 kB

Release files / tuya_device_handlers-2026.12.0.tar.gz

Download URL tuya_device_handlers-2026.12.0.tar.gz
Size 70.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5c1007ae3ab4da48c9daa5a451469d3a4cf43f2c21968dcea95f87f9ccf1114a
BLAKE2b-256 checksum
How to use checksums
e75ed62b63e1a88f03b3f97db48007a54279920cc9027a3c5d7afae62bdc327e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Release files / tuya_device_handlers-2026.12.0-py3-none-any.whl

Download URL tuya_device_handlers-2026.12.0-py3-none-any.whl
Size 127.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72fd8945b6cdc978734ead932239df8965fc4bfb1450c681fc77f29e19157ece
BLAKE2b-256 checksum
How to use checksums
777dd9b219e24c079048b8fc4dafe6fd20574af1850eb41193241b98f26c4534
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Security for tuya-device-handlers 2026.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tuya_device_handlers-2026.12.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2026.12.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page