Beispiel #1
0
    def from_cluster_data(self,
                          data: TuyaClusterData) -> Optional[TuyaCommand]:
        """Convert from cluster data to a tuya data payload."""

        dp, mapping = self.get_dp_mapping(data.endpoint_id, data.cluster_attr)
        self.debug("from_cluster_data: %s, %s", dp, mapping)
        if dp:
            cmd_payload = TuyaCommand()
            cmd_payload.status = 0
            cmd_payload.tsn = self.endpoint.device.application.get_sequence()
            cmd_payload.dp = dp
            cmd_payload.data = TuyaData()
            datapoint_type = mapping.dp_type
            cmd_payload.data.dp_type = datapoint_type
            cmd_payload.data.function = 0
            val = data.attr_value
            if mapping.dp_converter:
                val = mapping.dp_converter(val)
                self.debug("converted: %s", val)
            if datapoint_type.ztype:
                val = datapoint_type.ztype(val)
                self.debug("ztype: %s", val)
            val = Data.from_value(val)
            self.debug("from_value: %s", val)
            cmd_payload.data.raw = t.LVBytes.deserialize(val)[0]
            self.debug("raw: %s", cmd_payload.data.raw)
            return cmd_payload
        else:
            self.warning(
                "No cluster_dp found for %s, %s",
                data.endpoint_id,
                data.cluster_attr,
            )
            return None
def test_tuya_data_bitmap_invalid():
    """Test tuya Bitmap datatype."""

    data = b"\x05\x00\x03\x4012"
    extra = b"extra data"

    r, rest = TuyaData.deserialize(data + extra)
    assert rest == extra

    with pytest.raises(ValueError):
        r.payload
def test_tuya_data_bitmap():
    """Test tuya Bitmap datatype."""

    data = b"\x05\x00\x01\x40"
    extra = b"extra data"

    r, rest = TuyaData.deserialize(data + extra)
    assert rest == extra

    assert r.dp_type == 5
    assert r.raw == b"\x40"
    assert r.payload == 0x40

    data = b"\x05\x00\x02\x40\x02"
    r, _ = TuyaData.deserialize(data)
    r.payload == 0x4002

    data = b"\x05\x00\x04\x40\x02\x80\x01"
    r, _ = TuyaData.deserialize(data)
    r.payload == 0x40028001
def test_tuya_data_string():
    """Test tuya String datatype."""

    data = b"\x03\x00\x04Tuya"
    extra = b"extra data"

    r, rest = TuyaData.deserialize(data + extra)
    assert rest == extra

    assert r.dp_type == 3
    assert r.raw == b"Tuya"
    assert r.payload == "Tuya"
def test_tuya_data_enum():
    """Test tuya Enum datatype."""

    data = b"\x04\x00\x01\x40"
    extra = b"extra data"

    r, rest = TuyaData.deserialize(data + extra)
    assert rest == extra

    assert r.dp_type == 4
    assert r.raw == b"\x40"
    assert r.payload == 0x40
def test_tuya_data_value():
    """Test tuya "Value" datatype."""

    data = b"\x02\x00\x04\x00\x00\x02\xdb"
    extra = b"extra data"

    r, rest = TuyaData.deserialize(data + extra)
    assert rest == extra

    assert r.dp_type == 2
    assert r.raw == b"\xdb\x02\x00\x00"
    assert r.payload == 731
def test_tuya_data_bool():
    """Test tuya Bool datatype."""

    data = b"\x01\x00\x01\x00"
    extra = b"extra data"

    r, rest = TuyaData.deserialize(data + extra)
    assert rest == extra

    assert r.dp_type == 1
    assert r.raw == b"\x00"
    assert not r.payload

    data = b"\x01\x00\x01\x01"
    extra = b"extra data"

    r, rest = TuyaData.deserialize(data + extra)
    assert rest == extra

    assert r.dp_type == 1
    assert r.raw == b"\x01"
    assert r.payload
    extra = b"extra data"

    r, rest = TuyaData.deserialize(data + extra)
    assert rest == extra

    with pytest.raises(ValueError):
        r.payload


@pytest.mark.parametrize(
    "cmd_id, handler_name, args",
    (
        (
            TUYA_GET_DATA,
            "handle_get_data",
            (TuyaCommand(0, 2, 2, TuyaData(1, 0, b"\x01\x01")),),
        ),
        (
            TUYA_SET_DATA_RESPONSE,
            "handle_set_data_response",
            (TuyaCommand(0, 2, 2, TuyaData(1, 0, b"\x01\x01")),),
        ),
        (
            TUYA_ACTIVE_STATUS_RPT,
            "handle_active_status_report",
            (TuyaCommand(0, 2, 2, TuyaData(1, 0, b"\x01\x01")),),
        ),
        (TUYA_SET_TIME, "handle_set_time_request", (0x1234,)),
    ),
)
@mock.patch("zhaquirks.tuya.TuyaNewManufCluster.send_default_rsp")