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

Update Tuya builder DP attribute_name type to match underlying DPToAttributeMapping class #3811

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

jeverley
Copy link
Contributor

@jeverley jeverley commented Jan 30, 2025

Proposed change

This amends the attribute_name argument type in tuya.builder methods tuya_dp and tuya_dp_attribute to support str and tuple to match the underlying DPToAttributeMapping class.

This is used in DPToAttributeMapping definitions to map a data point to multiple cluster attributes.

Additional information

Class that the attribute_name is used to populate:

@dataclasses.dataclass
class DPToAttributeMapping:
    """Container for datapoint to cluster attribute update mapping."""

    ep_attribute: str
    attribute_name: Union[str, tuple]
    converter: Optional[
        Callable[
            [
                Any,
            ],
            Any,
        ]
    ] = None
    endpoint_id: Optional[int] = None

This would be used in a v2 quirk as below:

    .tuya_dp(
        dp_id=6,
        ep_attribute=TuyaElectricalMeasurement.ep_attribute,
        attribute_name=(
            TuyaElectricalMeasurement.AttributeDefs.rms_voltage.name,
            TuyaElectricalMeasurement.AttributeDefs.rms_current.name,
            TuyaElectricalMeasurement.AttributeDefs.active_power.name,
        ),
        converter=lambda x: TuyaPowerPhase.variant_3(x),
    )



class TuyaPowerPhase:
    """Extract values from a Tuya power phase datapoint."""

    @staticmethod
    def variant_3(value) -> Tuple[t.uint_t, t.uint_t, int]:
        voltage = (value[0] << 8) | value[1]
        current = (value[2] << 16) | (value[3] << 8) | value[4]
        power = (value[5] << 16) | (value[6] << 8) | value[7]
        return voltage, current, power * 1

Checklist

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

Copy link

codecov bot commented Jan 30, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.91%. Comparing base (730f0a8) to head (a0ab027).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #3811   +/-   ##
=======================================
  Coverage   90.91%   90.91%           
=======================================
  Files         325      325           
  Lines       10567    10567           
=======================================
  Hits         9607     9607           
  Misses        960      960           

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

@jeverley jeverley marked this pull request as ready for review January 30, 2025 11:09
@jeverley jeverley closed this Jan 30, 2025
@jeverley jeverley deleted the patch-1 branch January 30, 2025 11:11
@jeverley jeverley restored the patch-1 branch January 30, 2025 11:18
@jeverley jeverley reopened this Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant