Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Quirk for TS011F #1057

Merged
merged 20 commits into from
Oct 16, 2021
Merged

Create Quirk for TS011F #1057

merged 20 commits into from
Oct 16, 2021

Conversation

miit86
Copy link
Contributor

@miit86 miit86 commented Oct 3, 2021

Quirk for the TS011F
See Issue #1045

Quirk for the TS011F 
_TZ3000_cphmq0q7'},
_TZ3000_ew3ldmgx'},
_TZ3000_ps3dmato'},
_TZ3000_mraovvmm'},
_TZ3000_jvzvulen'},
_TZ3000_dpo1ysak'}],
@codecov-commenter
Copy link

codecov-commenter commented Oct 3, 2021

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.19%. Comparing base (ade1475) to head (be85a12).
Report is 823 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1057      +/-   ##
==========================================
+ Coverage   81.08%   81.19%   +0.10%     
==========================================
  Files         195      196       +1     
  Lines        5160     5189      +29     
==========================================
+ Hits         4184     4213      +29     
  Misses        976      976              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@coveralls
Copy link

coveralls commented Oct 3, 2021

Pull Request Test Coverage Report for Build 1338958385

  • 26 of 26 (100.0%) changed or added relevant lines in 1 file are covered.
  • 70 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.1%) to 81.191%

Files with Coverage Reduction New Missed Lines %
zhaquirks/tuya/init.py 70 72.96%
Totals Coverage Status
Change from base Build 1301303824: 0.1%
Covered Lines: 4213
Relevant Lines: 5189

💛 - Coveralls

@MattWestb
Copy link
Contributor

MattWestb commented Oct 3, 2021

Still having the unknown cluster warning in logs. can we do something about that?

Try deleting the:

                    0xE000,  # Unknown
                    0xE001,  # Unknown

in the replace part (line 106 and 107), i think then ZHA is not getting the clusters and they is going to "dev nul".

@miit86
Copy link
Contributor Author

miit86 commented Oct 3, 2021

I removed

                    0xE000,  # Unknown
                    0xE001,  # Unknown

But still same entry in logs. But Im not sure if there related.
have more tuya stuff.

@MattWestb
Copy link
Contributor

MattWestb commented Oct 3, 2021

You can trying defining it like this (from IKEA Styrbar):

WWAH_CLUSTER_ID = 0xFC57 # decimal = 64599

but 2 different name for the clusters 0xE000 and 0xE001.

Perhaps David is knowing how to do it OK ?

Copy link
Contributor

@Adminiuga Adminiuga left a comment

Choose a reason for hiding this comment

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

Run through black and isort to fix formatitng.
Test and consider the proposed changes
Otherwise looks good

zhaquirks/tuya/TS011F_plug.py Outdated Show resolved Hide resolved
zhaquirks/tuya/TS011F_plug.py Outdated Show resolved Hide resolved
zhaquirks/tuya/TS011F_plug.py Outdated Show resolved Hide resolved
zhaquirks/tuya/TS011F_plug.py Outdated Show resolved Hide resolved
Made changes but stil using ```    attributes = OnOff.attributes.copy()
    attributes.update({0x8002: ("power_on_state", PowerOnState)})```
@MattWestb
Copy link
Contributor

If you is adding this at line 25 with one empty line before and 2 after:

TUYA_CLUSTER_ID0 = 0xE000 # 57344
TUYA_CLUSTER_ID1 = 0xE001 # 57345

and changing the line 79 and 80 to this (Signature cluster 0xE000 and 0xE001) :

                    TUYA_CLUSTER_ID0
                    TUYA_CLUSTER_ID1

and deleting the line 109 and 110 (in the replacement):

                    0xE000,  # Unknown
                    0xE001,  # Unknown

Do you still getting warning in HA long from the device ?

Used TuyaZBOnOffRestorePowerCluster from INIT. PR1056
used flake 8 in VSCode
Added the missing dots
I Sort and Flake 8 fixed
@miit86
Copy link
Contributor Author

miit86 commented Oct 8, 2021

Dont know why python check fails.
Can someone help ?

@MattWestb
Copy link
Contributor

The check have not failing but is not being run then you need having getting one PR merged for have the permission for having it being run after every changes (if not having blocking change requests that i is having in some PRs).

You must waiting until the devs / maintainer is approving the test and you can see if somthing is not OK.

You can see that last tests here https://github.com/zigpy/zha-device-handlers/pull/1057/checks.

ISORT says files is OK. Using ISORT in VSCode
@miit86
Copy link
Contributor Author

miit86 commented Oct 9, 2021

Whats Wrong With ISORT ?

@MattWestb
Copy link
Contributor

ISORT like having the block from zhaquirks.const import ( after line 25 (the last import then its the last in the sorting) and before class TuyaZBMetering(CustomCluster, Metering): ;-(((

@miit86
Copy link
Contributor Author

miit86 commented Oct 9, 2021 via email

@miit86
Copy link
Contributor Author

miit86 commented Oct 12, 2021

@MattWestb

Deleting line 27,28 and 29 (they is now not needed).
how does it knows it is 0x000 or 0x001?

@Adminiuga
Do the same the the other cluster and adjust accordingly. Then in the replacement dict, use these two classes instead of the cluster id as an int, e.g. input_clusters = [... , TuyaClusterE0000, TuyaClusterE001]

only in the replacements or?

@Adminiuga
Copy link
Contributor

only in the replacements or?

Only in replacement. In the signature itself you have options like constants, same way as currently. Alternativele, in the signature you could use TuyaClusterE000.cluster_id then you don't need to define those constants. Same as you use standard ZCL clusters in the sig.

@MattWestb
Copy link
Contributor

Sorry i was wrong its reading it from the 2 deleted line (X 28 and 29) so re do it or moving it in to the class and you dont heed using the old names.

Old line 41:
  cluster_id = TUYA_CLUSTER_ID1

New line 41:
  cluster_id = 0xE001

And the same for 33 with 0xE000

But i think its better doing like Adminiuga was writing.

Final Version
@miit86
Copy link
Contributor Author

miit86 commented Oct 12, 2021

@Adminiuga

Should be OK now.

@MattWestb
Copy link
Contributor

MattWestb commented Oct 12, 2021

B I N G O O O ! ! ! :-))))

Only need releasing the the change request that was not working (the tuya power on state that is moved to tuya INI) :-))

@miit86
Copy link
Contributor Author

miit86 commented Oct 12, 2021

YEEEEH, Finally..!

@Adminiuga can you release the change request.

I really liked doing this. learned a lot about how ZHA and Quirks work.
Hope I can help other people and make ZHA working with more Zigbee Devices

:)

@miit86
Copy link
Contributor Author

miit86 commented Oct 12, 2021

Need one more change

not matching with:

TuyaClusterE000.cluster_id,
TuyaClusterE000.cluster_id,

working with:

 0xE000,  # Unknown
 0xE001,  # Unknown

@MattWestb
Copy link
Contributor

Then you is getting it loading do you getting the warning you was getting before ?
I think they shall being fixed with the right config of the cluster in the replacement.

@miit86
Copy link
Contributor Author

miit86 commented Oct 12, 2021

@MattWestb
I always see the warning about unknown cluster 57344 and 57345.

But when I use:

TuyaClusterE000.cluster_id,
TuyaClusterE000.cluster_id,

the Quirk won't load, so my device poweronstate and divides are not good.

@MattWestb
Copy link
Contributor

I think and hope its working OK then in signature having the cluster number and the new classes in the replacement and you is not getting the complains.

Testing the quirk that its loading and working OK with the last changes then black and the other tests was OK lats run.

The last changes looks OK from my point of view but its not the same it being 110% OK for the system.

I think Adminiuga is doing some "Dream Coding" (= sleeping then hi is in US TZ) so must waiting until hi is coming online.

@miit86
Copy link
Contributor Author

miit86 commented Oct 13, 2021

Feeling Stupid...!!!

                    TuyaClusterE000.cluster_id,
                    TuyaClusterE000.cluster_id,

shoud be

                    TuyaClusterE000.cluster_id,
                    TuyaClusterE001.cluster_id,

Tested and Matched.

@MattWestb
Copy link
Contributor

THAT I KNOWING !!!
Its happening my oft and its the copy and paste syndrome and the reason is that the clipboard have being corrupted in your PC operating system and you need doing one:

c: \> format c: /f

;-))

Great that you was finding it and getting it working.
If you dont have more extra line or somthing it shall pass all the tests :-))

@miit86
Copy link
Contributor Author

miit86 commented Oct 14, 2021

Hi @Adminiuga
Can you run the tests and release the change?

@lemppari
Copy link

Sorry for a newbie question but what is the release path for this change? Will this eventually be part of the Home Assistant ZHA? I really need to get power state restore working with my "TS011F by _TZ3000_mraovvmm" units. I guess for now I should install this "ZHA Device Handlers" extension? And then wait until this quirk is added to that?

@MattWestb
Copy link
Contributor

Than all is working OK is the devs merging it to dev branch and you can using it if you like and then its being released in the next HA release if all is going well.

For using it before its in HA you can installing by configuring local quirks in HA and adding the new quirk in it #693 (comment).
The problem is that this PR is needing #1056 changed in the tuya INI so it not working and you need patching the ZHA-quirks in your HA docker container as in this guide:#693 (comment) both the tuya INI and the new ts011f.py need being updated for getting it working.

If you is god in coding you can doing one version with the power state and dont need the tuya INI but its little more work.

@lemppari
Copy link

Ok, thanks. So even after this has been merged to dev branch, just installing "ZHA Device Handers" extension (and possibly copying 1056+1057 somewhere) is not enough. I am only running one HA in Hassio and don't really want to mess it up. I guess I just have to wait for this to appear in HA.

@miit86
Copy link
Contributor Author

miit86 commented Oct 15, 2021

@lemppari

  1. enable custom quirks in HA
  2. copy code below to new file TS011F.plug
  3. remove device from ZHA and Add again.
  4. Reset the Device if you want to start fresh.
"""TS011F plug."""
import zigpy.types as t
from zhaquirks.const import (
    DEVICE_TYPE,
    ENDPOINTS,
    INPUT_CLUSTERS,
    MODEL,
    OUTPUT_CLUSTERS,
    PROFILE_ID,
)
from zigpy.profiles import zha
from zigpy.quirks import CustomCluster, CustomDevice
from zigpy.zcl.clusters.general import (
    Basic,
    GreenPowerProxy,
    Groups,
    Identify,
    OnOff,
    Ota,
    Scenes,
    Time,
)
from zigpy.zcl.clusters.homeautomation import ElectricalMeasurement
from zigpy.zcl.clusters.smartenergy import Metering


class TuyaClusterE000(CustomCluster):
    """Tuya manufacturer specific cluster 57344."""

    name = "Tuya Manufacturer Specific"
    cluster_id = 0xE000
    ep_attribute = "tuya_is_pita_0"


class TuyaClusterE001(CustomCluster):
    """Tuya manufacturer specific cluster 57345."""

    name = "Tuya Manufacturer Specific"
    cluster_id = 0xE001
    ep_attribute = "tuya_is_pita_1"


class TuyaZBMetering(CustomCluster, Metering):
    """TuyaZBMetering cluster divides the KwH for tuya"""

    MULTIPLIER = 0x0301
    DIVISOR = 0x0302
    _CONSTANT_ATTRIBUTES = {MULTIPLIER: 1, DIVISOR: 100}


class TuyaZBElectricalMeasurement(CustomCluster, ElectricalMeasurement):
    """TuyaZBElectricalMeasurement cluster divides the Current for tuya"""

    AC_VOLTAGE_MULTIPLIER = 0x0600
    AC_VOLTAGE_DIVISOR = 0x0601
    _CONSTANT_ATTRIBUTES = {AC_VOLTAGE_MULTIPLIER: 1, AC_VOLTAGE_DIVISOR: 1000}


class PowerOnState(t.enum8):
    """Tuya power on state enum."""

    Off = 0x00
    On = 0x01
    LastState = 0x02


class OnOffRestorePowerCluster(CustomCluster, OnOff):
    """Tuya on off cluster with restore state."""

    attributes = OnOff.attributes.copy()
    attributes.update({0x8002: ("power_on_state", PowerOnState)})


class Plug(CustomDevice):
    """Tuya plug with restore power state support."""

    signature = {
        MODEL: "TS011F",
        ENDPOINTS: {
            # <SimpleDescriptor endpoint=1 profile=266 device_type=81
            # device_version=1
            # input_clusters=[0, 3, 4, 5, 6, 9, 1794, 2820, 57344, 57345]
            # output_clusters=[10, 25]>
            1: {
                PROFILE_ID: zha.PROFILE_ID,
                DEVICE_TYPE: zha.DeviceType.ON_OFF_PLUG_IN_UNIT,
                INPUT_CLUSTERS: [
                    Basic.cluster_id,
                    Identify.cluster_id,
                    Groups.cluster_id,
                    Scenes.cluster_id,
                    OnOff.cluster_id,
                    Metering.cluster_id,
                    ElectricalMeasurement.cluster_id,
                    TuyaClusterE000.cluster_id,
                    TuyaClusterE001.cluster_id,
                ],
                OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
            },
            # <SimpleDescriptor endpoint=242 profile=41440 device_type=97
            # device_version=1
            # input_clusters=[]
            # output_clusters=[33]>
            242: {
                PROFILE_ID: 41440,
                DEVICE_TYPE: 97,
                INPUT_CLUSTERS: [],
                OUTPUT_CLUSTERS: [GreenPowerProxy.cluster_id],
            },
        },
    }
    replacement = {
        ENDPOINTS: {
            1: {
                PROFILE_ID: zha.PROFILE_ID,
                DEVICE_TYPE: zha.DeviceType.ON_OFF_PLUG_IN_UNIT,
                INPUT_CLUSTERS: [
                    Basic.cluster_id,
                    Identify.cluster_id,
                    Groups.cluster_id,
                    Scenes.cluster_id,
                    OnOffRestorePowerCluster,
                    TuyaZBMetering,
                    TuyaZBElectricalMeasurement,
                    TuyaClusterE000,
                    TuyaClusterE001,
                ],
                OUTPUT_CLUSTERS: [Time.cluster_id, Ota.cluster_id],
            },
        },
    }

@lemppari
Copy link

Thanks @miit86 !

Luck and no luck...

I did the following:

  1. Installed the development library as described in https://github.com/zigpy/zha-device-handlers using the version 0.0.62 as it is the latest.
  2. Created the file TS011F_plug.py from your comment above and put it to /config/deps/lib/python3.9/site-packages/zhaquirks/tuya directory, emptied pycache and restarted HA.
  3. Removed and added my device again
  4. Looking at the device, it is using the quirks version"Quirk: zhaquirks.tuya.TS011F_plug.Plug"

But I cannot set the power_on mode to stick. I go to Cluster settings Clusters->OnOffRestorePowerCluster and from Cluster Attributes I select power_on_state (id: 0x8002)-> Get Zigbee attribute returns PowerOnState:Off. I try to set Manufacturer Code Override to 1 or 2 (I think they are On and LastState) but Getting the value again gives None

@miit86
Copy link
Contributor Author

miit86 commented Oct 15, 2021 via email

@lemppari
Copy link

That did the trick, thank you so much! Thanks for MattWestb too!

So next step for this in the official release path is to be included in some future version of zha-device-handlers (0.0.6?). Then from that to the HA general release. Until that I have to remember to check the status of quirk after updating HA. And after HA gets this by default I can remove zha-device-handlers (unless I want to start digging into zha myself... ;) )

@miit86
Copy link
Contributor Author

miit86 commented Oct 15, 2021 via email

@MattWestb
Copy link
Contributor

The power state in INI is merged in dev and very likely is this also going in dev very soon then all test is OK and its working.

If its working its better waiting until next release of HA it shall being inside it if all is going OK and you dont need doing and changes and done need deleting / changing any things.

@dmulcahey dmulcahey merged commit 0054ff8 into zigpy:dev Oct 16, 2021
@miit86
Copy link
Contributor Author

miit86 commented Oct 16, 2021

YEEEH..! Thanks Guys

@miit86 miit86 deleted the patch-1 branch October 16, 2021 12:09
@MattWestb
Copy link
Contributor

GREAT work done !!

And now you can doing changes in your PRs and the tests is being triggered automatic as long no change request is made :-))

Some more device in the pipe that need more hacking attention ?
And you have getting the hactoberfest tagged to your account :-)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants