Skip to content

Improve Tuya EnchantedDevice implementation #2271

Open
@TheJulianJES

Description

@TheJulianJES

Description

The implementation for the "Tuya magic spell" is currently implemented in EnchantedDevice like this:

class EnchantedDevice(CustomDevice):
"""Class for enchanted Tuya devices which needs to be unlocked by casting a 'spell'."""
def __init__(self, *args, **kwargs):
"""Initialize with task."""
super().__init__(*args, **kwargs)
self._init_device_task = asyncio.create_task(self.spell())
async def spell(self) -> None:
"""Initialize device so that all endpoints become available."""
attr_to_read = [4, 0, 1, 5, 7, 0xFFFE]
basic_cluster = self.endpoints[1].in_clusters[0]
await basic_cluster.read_attributes(attr_to_read)

Starting to read attribute reads from __init__ isn't ideal, as this theoretically happens every time as ZHA reloads, but those attribute reads seemingly do not actually make it to the network.
We also can't really "re-cast" the magic spell (from a reconfiguration for example).

It would be best if this implementation could be improved. One possibility might be to call it from a bind() function from some cluster (that's registered as a channel in ZHA, so the function actually gets called). This makes sure it's called "from a proper place" and also re-casts the device when doing a reconfiguration.
Maybe something like a BasicTuyaSpell cluster that casts then spell would work?
Edit: See discussion: #1427
Apparently bind() is not called on the Basic cluster (disabled here).

Some comments from #2227 include more information on this.

I'll try to look into this soon-ish.

Metadata

Metadata

Assignees

No one assigned

    Labels

    TuyaRequest/PR regarding a Tuya deviceenhancementImprove an existing quirk

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions