zha-device-handlers icon indicating copy to clipboard operation
zha-device-handlers copied to clipboard

Add support for Third Reality plug private cluster ID

Open hwzolin opened this issue 2 years ago • 8 comments

Proposed change

Add support for the "reset summation delivered" attribute for Third Reality plugs.

Additional information

Checklist

  • [x] The changes are tested and work correctly
  • [x] pre-commit checks pass / the code has been formatted using Black
  • [ ] Tests have been added to verify that the new code works

hwzolin avatar Mar 29 '24 02:03 hwzolin

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 88.20%. Comparing base (9ccb69a) to head (15f4ed7). Report is 40 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #3078      +/-   ##
==========================================
+ Coverage   87.90%   88.20%   +0.29%     
==========================================
  Files         300      301       +1     
  Lines        9222     9412     +190     
==========================================
+ Hits         8107     8302     +195     
+ Misses       1115     1110       -5     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Mar 29 '24 02:03 codecov[bot]

This code adds the private cluster ID to the Third Reality plug. Please help with it, thank you.

hwzolin avatar Mar 29 '24 03:03 hwzolin

@dmulcahey @TheJulianJES Could you help to review the merge require, thanks.

weihuan1111 avatar Apr 16 '24 07:04 weihuan1111

Hello, please check it out again, thanks.

hwzolin avatar Apr 26 '24 09:04 hwzolin

@TheJulianJES Hello, what we added to our code this time is the ability to clear the summation delivered of the plug. The cluster_id of this function is 0xFF03, and the attribute_id is 0x0000, and we need to send a value of 1 to achieve the effect of zeroing. So far, I've taken care of some formatting issues as you've requested. As for what you said about implementing the function of a button through write_attr_button, I don't know how to call this function, can you please help me deal with it?

hwzolin avatar Apr 28 '24 08:04 hwzolin

@TheJulianJES Hello, what we added to our code this time is the ability to clear the summation delivered of the plug. The cluster_id of this function is 0xFF03, and the attribute_id is 0x0000, and we need to send a value of 1 to achieve the effect of zeroing. So far, I've taken care of some formatting issues as you've requested. As for what you said about implementing the function of a button through write_attr_button, I don't know how to call this function, can you please help me deal with it?

@TheJulianJES @dmulcahey

hwzolin avatar May 09 '24 06:05 hwzolin

There's an example in the quirks v2 docs PR and/or similar in the open Bosch PR. I can help you better soon.

TheJulianJES avatar May 09 '24 20:05 TheJulianJES

@TheJulianJES @dmulcahey Hello, I would like to use the zha quirk v2 implementation to zero out the value of summation delivered by clicking the button on the page. But there are relatively few projects that can be referred to on the Internet, and I didn't find the open PR code you said earlier, so the result is as shown in the gif, although the button was clicked, but the program reported an error “Failed to call service button/press. 'reset_summation_delivered'”.

clear summation delivered

I put the code below. Could you please help me see if the code is written wrong? Thank you!

"""Third Reality Plug devices."""

from typing import Final

from zigpy.profiles import zha
from zigpy.quirks import CustomDevice
import zigpy.types as t
from zigpy.zcl.clusters.general import (
    Basic,
    GreenPowerProxy,
    Groups,
    Identify,
    OnOff,
    Ota,
    Scenes,
)
from zigpy.zcl.clusters.homeautomation import ElectricalMeasurement
from zigpy.zcl.clusters.smartenergy import Metering
from zigpy.zcl.foundation import BaseAttributeDefs, ZCLAttributeDef
from zigpy.quirks.v2 import add_to_registry_v2,CustomDeviceV2,EntityMetadata
from zigpy.quirks.registry import DeviceRegistry
from zhaquirks import CustomCluster
from zigpy.quirks.v2 import ClusterType
from zhaquirks.const import (
    DEVICE_TYPE,
    ENDPOINTS,
    INPUT_CLUSTERS,
    MODELS_INFO,
    OUTPUT_CLUSTERS,
    PROFILE_ID,
)
from zhaquirks.thirdreality import THIRD_REALITY

THIRD_REALITY_PLUG_CLUSTER_ID = 0xFF03
RESET_SUMMATION_DELIVERED_ATTR_ID = 0x0000

class ControlMode(t.uint8_t):
    """Reset mode for clear summation delivered."""

    CLEAR = 1


class ThirdRealityPlugCluster(CustomCluster):
    """ThirdReality Acceleration Cluster."""

    cluster_id = THIRD_REALITY_PLUG_CLUSTER_ID
    

    class AttributeDefs(BaseAttributeDefs):
        """Attribute definitions."""

        reset_summation_delivered: Final = ZCLAttributeDef(
            id=RESET_SUMMATION_DELIVERED_ATTR_ID,
            type=ControlMode,
            is_manufacturer_specific=True,
        )
    
registry = DeviceRegistry()
(
    add_to_registry_v2(
        THIRD_REALITY, "3RSP02028BZ"
    )
    .adds(ThirdRealityPlugCluster.cluster_id)
    .write_attr_button(
        attribute_name=ThirdRealityPlugCluster.AttributeDefs.reset_summation_delivered.name,
        attribute_value=RESET_SUMMATION_DELIVERED_ATTR_ID,
        cluster_id=ThirdRealityPlugCluster.cluster_id,
        cluster_type=ClusterType.Server,
        endpoint_id=1
    )
)

quirked_device = registry.get_device()
write_attribute_button: EntityMetadata = quirked_device.exposes_metadata[
    (1, ThirdRealityPlugCluster.cluster_id, ClusterType.Server)
][0]

hwzolin avatar May 17 '24 01:05 hwzolin

Replacing .adds(ThirdRealityPlugCluster.cluster_id) with .adds(ThirdRealityPlugCluster) creates a button for me:

"""Third Reality Plug devices."""

from typing import Final

import zigpy.types as t
from zigpy.zcl.foundation import BaseAttributeDefs, ZCLAttributeDef
from zigpy.quirks.v2 import add_to_registry_v2
from zhaquirks import CustomCluster
from zigpy.quirks.v2 import ClusterType
from zhaquirks.thirdreality import THIRD_REALITY


class ThirdRealityPlugCluster(CustomCluster):
    cluster_id = 0xFF03

    class AttributeDefs(BaseAttributeDefs):
        reset_summation_delivered: Final = ZCLAttributeDef(
            id=0x0000,
            type=t.uint8_t,
            is_manufacturer_specific=True,
        )


(
    add_to_registry_v2(THIRD_REALITY, "3RSP02028BZ")
    .adds(ThirdRealityPlugCluster)
    .write_attr_button(
        attribute_name=ThirdRealityPlugCluster.AttributeDefs.reset_summation_delivered.name,
        attribute_value=0x01,
        cluster_id=ThirdRealityPlugCluster.cluster_id,
        cluster_type=ClusterType.Server,
        endpoint_id=1,
        translation_key="reset_summation_delivered"
    )
)

puddly avatar May 21 '24 15:05 puddly

@puddly Thank you for your patience, now the code works. But I still have a question, that is, how to set the default name of the button control that appears on the page? As you can see, the current name is "Third Reality, Inc 3RSP0202...". image

hwzolin avatar May 22 '24 07:05 hwzolin

@hwzolin A translation key (default: attribute name) for Home Assistant/ZHA itself needs to be added. This will be done when we merge the "quirks bump PR" into Home Assistant/ZHA. We just need to know the translation keys and default English translations. As an example for another quirk, see: https://github.com/matt-sullivan/home-assistant-core/commit/1dff8811cc4d3ce397f2ec605e6e3fbe3c45bb7f

TheJulianJES avatar May 28 '24 00:05 TheJulianJES

@hwzolin A translation key (default: attribute name) for Home Assistant/ZHA itself needs to be added. This will be done when we merge the "quirks bump PR" into Home Assistant/ZHA. We just need to know the translation keys and default English translations. As an example for another quirk, see: matt-sullivan/home-assistant-core@1dff881

@TheJulianJES Do you mean like this: https://github.com/hwzolin/core/blob/dev/homeassistant/components/zha/strings.json? image

hwzolin avatar May 28 '24 02:05 hwzolin

@TheJulianJES Hello, I have updated the code about zha quirk v2, please check it out. If there is no problem, please merge it.

hwzolin avatar May 28 '24 07:05 hwzolin

@TheJulianJES Hello, due to the busy work of the previous project, this project has been put on hold for the time being. I didn't quite understand the solution you were offering at the time. Can you provide a complete project example of implementing a private cluster that supports devices with Quirks v2, I'd like to refer to it.

hwzolin avatar Sep 09 '24 01:09 hwzolin

@TheJulianJES Hello, due to the busy work of the previous project, this project has been put on hold for the time being. I didn't quite understand the solution you were offering at the time. Can you provide a complete project example of implementing a private cluster that supports devices with Quirks v2, I'd like to refer to it.

@TheJulianJES

hwzolin avatar Sep 13 '24 09:09 hwzolin